README
ts-junit
use junit descrator with typescript
jest 支持 ts 需要引入babelts-jest 直接支持ts,测试语法上是jest语法,suite/test或describe/it- ts-junit 使用junit 5的装饰器进行封装,成熟,使用于熟悉OO的开发,尤其对Java开发更友好。
- ts-junit 默认使用uvu,同时提供各个常见测试框架的支持,比如jest、mocha、ava、tape、qunit、jasmine等。
示例
import assert from 'assert'
import { BeforeAll, BeforeEach, Disabled, Test, AfterEach, AfterAll } from 'ts-junit'
export default class MyFirstJUnitJupiterTests {
@BeforeAll
static void initAll() {
}
@BeforeEach
void init() {
}
@Test
void succeedingTest() {
}
@Test
void failingTest() {
assert.fail("a failing test");
}
@Test
@Disabled("for demonstration purposes")
void skippedTest() {
// not executed
}
@Test
void abortedTest() {
assert.assumeTrue("abc".contains("Z"));
assert.fail("test should have been aborted");
}
@AfterEach
void tearDown() {
}
@AfterAll
static void tearDownAll() {
}
}
结合 https://github.com/midwayjs/injection 更简单(暂未实现)
class Test {
@Inject()
helloTest: IHelloTest;
@Inject()
helloService: IHelloService;
@Before()
before() {
mock(helloTest, 'sayhello', () => {
return 'mocked'
});
}
@Test()
async test() {
expect(this.helloTest.sayhello()).eq('mocked');
expect(this.helloService.sayhello('test')).eq('hello test');
}
}
Quick start
安装
$ npm i --save ts-juint
开始编写 测试 代码 test/index.ts
创建编译时的 tsconfig.json 文件
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./src",
"typeRoots": [],
"types": [],
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": ["node_modules"],
"include": ["./src/**/*.ts", "./test/**/*.ts"]
}
编辑 package.json 的启动和编译脚本
{
"scripts": {
"test": "NODE_ENV=dev ts-node --project tsconfig.json --files src/index.ts",
"build": "tsc"
}
}
启动服务
npm test
> NODE_ENV=dev ts-node --project tsconfig.json --files src/index.ts
[2020-9-1 19:52:12] [debug] [init] [router] get - /
装饰器
- 参考junit5的文档 https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations
- 进度
7/20
Annotation | Description | isSupported |
---|---|---|
|
Denotes that a method is a test method. Unlike JUnit 4’s |
✅ |
|
Denotes that a method is a parameterized test. Such methods are inherited unless they are overridden. |
❌ |
|
Denotes that a method is a test template for a repeated test. Such methods are inherited unless they are overridden. |
❌ |
|
Denotes that a method is a test factory for dynamic tests. Such methods are inherited unless they are overridden. |
❌ |
|
Denotes that a method is a template for test cases designed to be invoked multiple times depending on the number of invocation contexts returned by the registered providers. Such methods are inherited unless they are overridden. |
❌ |
|
Used to configure the test method
execution order for the annotated test class; similar to JUnit 4’s
|
❌ |
|
Used to configure the test instance lifecycle for the annotated test class. Such annotations are inherited. |
❌ |
|
Declares a custom display name for the test class or test method. Such annotations are not inherited. |
✅ |
|
Declares a custom display name generator for the test class. Such annotations are inherited. |
❌ |
|
Denotes that the annotated method should be executed before
each |
✅ |
|
Denotes that the annotated method should be executed after
each |
✅ |
|
Denotes that the annotated method should be executed before
all |
✅ |
|
Denotes that the annotated method should be executed after
all |
✅ |
|
Denotes that the annotated class is a non-static nested test class. |
❌ |
|
Used to declare tags for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are inherited at the class level but not at the method level. |
❌ |
|
Used to disable a test class or test
method; analogous to JUnit 4’s |
✅ |
|
Used to fail a test, test factory, test template, or lifecycle method if its execution exceeds a given duration. Such annotations are inherited. |
❌ |
|
Used to register extensions declaratively. Such annotations are inherited. |
❌ |
|
Used to register extensions programmatically via fields. Such fields are inherited unless they are shadowed. |
❌ |
|
Used to supply a temporary directory via field
injection or parameter injection in a lifecycle method or test method; located in the
|
❌ |
cli
.option('-b, --bail', 'Exit on first failure')
.option('-i, --ignore', 'Any file patterns to ignore')
.option('-r, --require', 'Additional module(s) to preload')
.option('-C, --cwd', 'The current directory to resolve from', '.')
.option('-c, --color', 'Print colorized output', true)