Skip to content

Feat/support test name pattern #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
.cache

/node_modules
/dist
Expand Down
Empty file removed a.ts
Empty file.
4 changes: 0 additions & 4 deletions assembly/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ export namespace assertResult {
export declare function registerTestFunction(index: u32): void;


@external("__unittest_framework_env","finishTestFunction")
export declare function finishTestFunction(): void;


@external("__unittest_framework_env","collectCheckResult")
export declare function collectCheckResult(
result: bool,
Expand Down
7 changes: 2 additions & 5 deletions assembly/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ export function describeImpl(
testsFunction();
assertResult.removeDescription();
}
export function testImpl(description: string, testFunction: () => void): void {
assertResult.addDescription(description);
export function testImpl(name: string, testFunction: () => void): void {
assertResult.addDescription(name);
assertResult.registerTestFunction(testFunction.index);
testFunction();
assertResult.finishTestFunction();
assertResult.removeDescription();
mockFunctionStatus.clear();
}

export function mockImpl<T extends Function>(
Expand Down
6 changes: 3 additions & 3 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export function describe(description: string, testsFunction: () => void): void {

/**
* run a test
* @param description test description
* @param name test name
* @param testFunction main function of test
*/
export function test(description: string, testFunction: () => void): void {
testImpl(description, testFunction);
export function test(name: string, testFunction: () => void): void {
testImpl(name, testFunction);
}

/**
Expand Down
51 changes: 25 additions & 26 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@ import { validatArgument, start_unit_test } from "../dist/index.js";
const program = new Command();
program
.option("--config <config file>", "path of config file", "as-test.config.js")
.option("--testcase <testcases...>", "only run specified test cases")
.option("--temp <path>", "test template file folder")
.option("--output <path>", "coverage report output folder")
.option("--mode <output mode>", "test result output format")
.option("--mode <output mode>", "coverage report output format")
.option("--coverageLimit [error warning...]", "set warn(yellow) and error(red) upper limit in coverage report")
.option("--testNamePattern <test name pattern>", "run only tests with a name that matches the regex pattern");
.option("--testcase <testcases...>", "run only specified test cases")
.option("--testNamePattern <test name pattern>", "run only tests with a name that matches the regex pattern")
.option("--collectCoverage <boolean>", "whether to collect coverage information and report");

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

if (options.config === undefined) {
console.error(chalk.redBright("Miss config file") + "\n");
console.error(program.helpInformation());
exit(-1);
}
const configPath = resolve(".", options.config);
if (!fs.pathExistsSync(configPath)) {
console.error(chalk.redBright("Miss config file") + "\n");
Expand All @@ -35,33 +31,36 @@ if (!fs.pathExistsSync(configPath)) {
}
const config = (await import(pathToFileURL(configPath))).default;

let includes = config.include;
const includes = config.include;
if (includes === undefined) {
console.error(chalk.redBright("Miss include in config file") + "\n");
exit(-1);
}
let excludes = config.exclude || [];
let testcases = options.testcase;

let flags = config.flags || "";
let imports = config.imports || null;
const excludes = config.exclude || [];
validatArgument(includes, excludes);

let mode = options.mode || config.mode || "table";
// if enabled testcase or testNamePattern, disable collectCoverage by default
const collectCoverage =
Boolean(options.collectCoverage) || config.collectCoverage || (!options.testcase && !options.testNamePattern);

let tempFolder = options.temp || config.temp || "coverage";
let outputFolder = options.output || config.output || "coverage";
const testOption = {
includes,
excludes,
testcases: options.testcase,
testNamePattern: options.testNamePattern,
collectCoverage,

let errorLimit = options.coverageLimit?.at(0);
let warnLimit = options.coverageLimit?.at(1);
flags: config.flags || "",
imports: config.imports || undefined,

let testNamePattern = options.testNamePattern;
tempFolder: options.temp || config.temp || "coverage",
outputFolder: options.output || config.output || "coverage",
mode: options.mode || config.mode || "table",
warnLimit: Number(options.coverageLimit?.at(1)),
errorLimit: Number(options.coverageLimit?.at(0)),
};

validatArgument(includes, excludes);
start_unit_test(
{ includes, excludes, testcases, testNamePattern },
{ flags, imports },
{ tempFolder, outputFolder, mode, warnLimit, errorLimit }
)
start_unit_test(testOption)
.then((success) => {
if (!success) {
console.error(chalk.redBright("Test Failed") + "\n");
Expand Down
2 changes: 1 addition & 1 deletion docs/api-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ A comprehensive AssemblyScript testing solution, offering developers a robust su

- Function Mocking
- Coverage statistics
- Expectations
- Matchers
1 change: 0 additions & 1 deletion docs/api-documents/mock-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ test("getTime error handle", () => {
expect(getTime()).equal(false); // success
expect(fn.calls).equal(1); // success
});
endTest();
```

mock API can temporary change the behavior of function, effective scope is each test.
Expand Down
49 changes: 39 additions & 10 deletions docs/api-documents/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ There are several ways to run partial test cases:

#### Partial Test Files

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

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

#### Partial Tests

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

::: details

```
- a.test.ts
|- case_1
|- case_2
- b.test.ts
|- case_A
- c.test.ts
|- case_4
describe("groupA", () => {
test("case_1", () => {
...
});
test("case_2", () => {
...
});
test("case_3", () => {
...
});
});

describe("groupB", () => {
test("case_A", () => {
...
});
test("case_B", () => {
...
});
test("case_C", () => {
...
});
});
```

run `as-test --testNamePattern "case_\d"` will match `case 1`, `case 2`, `case 4`
run `as-test --testNamePattern "groupA case_\d"` will run `case_1`, `case_2`, `case_3`.

::: tip
The framework join `DescriptionName` and `TestName` with `" "` by default, e.g. `groupA case_1` is the fullTestCaseName of `case_1`.

:::

### Whether collect coverage information

```
--collectCoverage <boolean> whether to collect coverage information and report
```

The framework collects coverage and generates reports by default, but it will be disablea while running partial test cases by `--testcase` or `--testNamePattern`.

You can control the coverage collection manually with `--collectCoverage` option.
28 changes: 20 additions & 8 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ export function add(a: i32, b: i32): i32 {
Then, create a file named `tests/sum.test.ts`. This will contain our actual test:

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

test("sum", () => {
expect(add(1, 2)).equal(3);
expect(add(1, 1)).equal(3);
});
endTest(); // Don't forget it!
```

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

```
transform source/sum.ts => build/source/sum.ts.cov
transform build/source/sum.ts.cov => build/source/sum.ts
transform tests/sum.test.ts => build/tests/sum.test.ts
(node:489815) ExperimentalWarning: WASI is an experimental feature. This feature could change at any time
> [email protected] test
> as-test

(node:144985) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
code analysis: OK
compile testcases: OK
instrument: OK
execute testcases: OK

test case: 1/2 (success/total)

Error Message:
sum:
tests/sum.test.ts:6:3 (6:3, 6:29)
sum:
tests/sum.test.ts:6:2 value: 2 expect: = 3
---------|---------|----------|---------|--------
File | % Stmts | % Branch | % Funcs | % Lines
---------|---------|----------|---------|--------
source | 100 | 100 | 100 | 100
sum.ts | 100 | 100 | 100 | 100
---------|---------|----------|---------|--------

Test Failed
```

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