Skip to content

Commit 5a6b779

Browse files
authored
feat: support test name pattern (#50)
1 parent 03e2738 commit 5a6b779

30 files changed

+399
-240
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode
2+
.cache
23

34
/node_modules
45
/dist

a.ts

Whitespace-only changes.

assembly/env.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export namespace assertResult {
1212
export declare function registerTestFunction(index: u32): void;
1313

1414

15-
@external("__unittest_framework_env","finishTestFunction")
16-
export declare function finishTestFunction(): void;
17-
18-
1915
@external("__unittest_framework_env","collectCheckResult")
2016
export declare function collectCheckResult(
2117
result: bool,

assembly/implement.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ export function describeImpl(
99
testsFunction();
1010
assertResult.removeDescription();
1111
}
12-
export function testImpl(description: string, testFunction: () => void): void {
13-
assertResult.addDescription(description);
12+
export function testImpl(name: string, testFunction: () => void): void {
13+
assertResult.addDescription(name);
1414
assertResult.registerTestFunction(testFunction.index);
15-
testFunction();
16-
assertResult.finishTestFunction();
1715
assertResult.removeDescription();
18-
mockFunctionStatus.clear();
1916
}
2017

2118
export function mockImpl<T extends Function>(

assembly/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export function describe(description: string, testsFunction: () => void): void {
2020

2121
/**
2222
* run a test
23-
* @param description test description
23+
* @param name test name
2424
* @param testFunction main function of test
2525
*/
26-
export function test(description: string, testFunction: () => void): void {
27-
testImpl(description, testFunction);
26+
export function test(name: string, testFunction: () => void): void {
27+
testImpl(name, testFunction);
2828
}
2929

3030
/**

bin/cli.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,17 @@ import { validatArgument, start_unit_test } from "../dist/index.js";
1212
const program = new Command();
1313
program
1414
.option("--config <config file>", "path of config file", "as-test.config.js")
15-
.option("--testcase <testcases...>", "only run specified test cases")
1615
.option("--temp <path>", "test template file folder")
1716
.option("--output <path>", "coverage report output folder")
18-
.option("--mode <output mode>", "test result output format")
17+
.option("--mode <output mode>", "coverage report output format")
1918
.option("--coverageLimit [error warning...]", "set warn(yellow) and error(red) upper limit in coverage report")
20-
.option("--testNamePattern <test name pattern>", "run only tests with a name that matches the regex pattern");
19+
.option("--testcase <testcases...>", "run only specified test cases")
20+
.option("--testNamePattern <test name pattern>", "run only tests with a name that matches the regex pattern")
21+
.option("--collectCoverage <boolean>", "whether to collect coverage information and report");
2122

2223
program.parse(process.argv);
2324
const options = program.opts();
2425

25-
if (options.config === undefined) {
26-
console.error(chalk.redBright("Miss config file") + "\n");
27-
console.error(program.helpInformation());
28-
exit(-1);
29-
}
3026
const configPath = resolve(".", options.config);
3127
if (!fs.pathExistsSync(configPath)) {
3228
console.error(chalk.redBright("Miss config file") + "\n");
@@ -35,33 +31,36 @@ if (!fs.pathExistsSync(configPath)) {
3531
}
3632
const config = (await import(pathToFileURL(configPath))).default;
3733

38-
let includes = config.include;
34+
const includes = config.include;
3935
if (includes === undefined) {
4036
console.error(chalk.redBright("Miss include in config file") + "\n");
4137
exit(-1);
4238
}
43-
let excludes = config.exclude || [];
44-
let testcases = options.testcase;
45-
46-
let flags = config.flags || "";
47-
let imports = config.imports || null;
39+
const excludes = config.exclude || [];
40+
validatArgument(includes, excludes);
4841

49-
let mode = options.mode || config.mode || "table";
42+
// if enabled testcase or testNamePattern, disable collectCoverage by default
43+
const collectCoverage =
44+
Boolean(options.collectCoverage) || config.collectCoverage || (!options.testcase && !options.testNamePattern);
5045

51-
let tempFolder = options.temp || config.temp || "coverage";
52-
let outputFolder = options.output || config.output || "coverage";
46+
const testOption = {
47+
includes,
48+
excludes,
49+
testcases: options.testcase,
50+
testNamePattern: options.testNamePattern,
51+
collectCoverage,
5352

54-
let errorLimit = options.coverageLimit?.at(0);
55-
let warnLimit = options.coverageLimit?.at(1);
53+
flags: config.flags || "",
54+
imports: config.imports || undefined,
5655

57-
let testNamePattern = options.testNamePattern;
56+
tempFolder: options.temp || config.temp || "coverage",
57+
outputFolder: options.output || config.output || "coverage",
58+
mode: options.mode || config.mode || "table",
59+
warnLimit: Number(options.coverageLimit?.at(1)),
60+
errorLimit: Number(options.coverageLimit?.at(0)),
61+
};
5862

59-
validatArgument(includes, excludes);
60-
start_unit_test(
61-
{ includes, excludes, testcases, testNamePattern },
62-
{ flags, imports },
63-
{ tempFolder, outputFolder, mode, warnLimit, errorLimit }
64-
)
63+
start_unit_test(testOption)
6564
.then((success) => {
6665
if (!success) {
6766
console.error(chalk.redBright("Test Failed") + "\n");

docs/api-documents.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ A comprehensive AssemblyScript testing solution, offering developers a robust su
22

33
- Function Mocking
44
- Coverage statistics
5-
- Expectations
5+
- Matchers

docs/api-documents/mock-function.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ test("getTime error handle", () => {
3131
expect(getTime()).equal(false); // success
3232
expect(fn.calls).equal(1); // success
3333
});
34-
endTest();
3534
```
3635

3736
mock API can temporary change the behavior of function, effective scope is each test.

docs/api-documents/options.md

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ There are several ways to run partial test cases:
3333

3434
#### Partial Test Files
3535

36-
Providing file path to `--testcase`. it can specify a certain group of files for testing.
36+
Providing file path to `--testcase`, it can specify a certain group of files for testing.
3737

3838
::: tip
3939
`--testcase` can accept multiple file paths.
@@ -57,20 +57,49 @@ run `as-test --testcase a.test.ts b.test.ts` will match all tests in `a.test.ts`
5757

5858
#### Partial Tests
5959

60-
Providing regex which can match targeted test name to `--testNamePattern`. it can specify a certain group of tests for testing.
60+
Providing regex which can match targeted test name to `--testNamePattern`, it can specify a certain group of tests for testing.
6161

6262
::: details
6363

6464
```
65-
- a.test.ts
66-
|- case_1
67-
|- case_2
68-
- b.test.ts
69-
|- case_A
70-
- c.test.ts
71-
|- case_4
65+
describe("groupA", () => {
66+
test("case_1", () => {
67+
...
68+
});
69+
test("case_2", () => {
70+
...
71+
});
72+
test("case_3", () => {
73+
...
74+
});
75+
});
76+
77+
describe("groupB", () => {
78+
test("case_A", () => {
79+
...
80+
});
81+
test("case_B", () => {
82+
...
83+
});
84+
test("case_C", () => {
85+
...
86+
});
87+
});
7288
```
7389

74-
run `as-test --testNamePattern "case_\d"` will match `case 1`, `case 2`, `case 4`
90+
run `as-test --testNamePattern "groupA case_\d"` will run `case_1`, `case_2`, `case_3`.
91+
92+
::: tip
93+
The framework join `DescriptionName` and `TestName` with `" "` by default, e.g. `groupA case_1` is the fullTestCaseName of `case_1`.
7594

7695
:::
96+
97+
### Whether collect coverage information
98+
99+
```
100+
--collectCoverage <boolean> whether to collect coverage information and report
101+
```
102+
103+
The framework collects coverage and generates reports by default, but it will be disablea while running partial test cases by `--testcase` or `--testNamePattern`.
104+
105+
You can control the coverage collection manually with `--collectCoverage` option.

docs/quick-start.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ export function add(a: i32, b: i32): i32 {
1919
Then, create a file named `tests/sum.test.ts`. This will contain our actual test:
2020

2121
```Typescript
22-
import { test, expect, endTest } from "assemblyscript-unittest-framework/assembly";
22+
import { test, expect } from "assemblyscript-unittest-framework/assembly";
2323
import { add } from "../source/sum";
2424

2525
test("sum", () => {
2626
expect(add(1, 2)).equal(3);
2727
expect(add(1, 1)).equal(3);
2828
});
29-
endTest(); // Don't forget it!
3029
```
3130

3231
Create a config file in project root `as-test.config.js`:
@@ -60,16 +59,29 @@ Add the following section to your `package.json`
6059
Finally, run `npm run test` and as-test will print this message:
6160

6261
```
63-
transform source/sum.ts => build/source/sum.ts.cov
64-
transform build/source/sum.ts.cov => build/source/sum.ts
65-
transform tests/sum.test.ts => build/tests/sum.test.ts
66-
(node:489815) ExperimentalWarning: WASI is an experimental feature. This feature could change at any time
62+
63+
> as-test
64+
65+
(node:144985) ExperimentalWarning: WASI is an experimental feature and might change at any time
66+
(Use `node --trace-warnings ...` to show where the warning was created)
67+
code analysis: OK
68+
compile testcases: OK
69+
instrument: OK
70+
execute testcases: OK
6771
6872
test case: 1/2 (success/total)
6973
7074
Error Message:
71-
sum:
72-
tests/sum.test.ts:6:3 (6:3, 6:29)
75+
sum:
76+
tests/sum.test.ts:6:2 value: 2 expect: = 3
77+
---------|---------|----------|---------|--------
78+
File | % Stmts | % Branch | % Funcs | % Lines
79+
---------|---------|----------|---------|--------
80+
source | 100 | 100 | 100 | 100
81+
sum.ts | 100 | 100 | 100 | 100
82+
---------|---------|----------|---------|--------
83+
84+
Test Failed
7385
```
7486

7587
You can also use `npx as-test -h` for more information to control detail configurations

0 commit comments

Comments
 (0)