Skip to content

Commit 7b829a2

Browse files
Add ability to filter tests that run
1 parent f948338 commit 7b829a2

File tree

22 files changed

+320
-101
lines changed

22 files changed

+320
-101
lines changed

.github/workflows/npm.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ jobs:
2828
- name: Publish
2929
uses: JS-DevTools/npm-publish@v1
3030
with:
31-
token: ${{ secrets.NPM_TOKEN }}
31+
token: ${{ secrets.NPM_TOKEN }}
32+
check-version: false

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ You can also run `xunit.ts` from a script in your `package.json`:
8585
}
8686
```
8787

88+
#### Filtering tests
89+
90+
The `xunit` command can take one or more `--filter` flags (`-f` alias) followed by a regular expression to match `TestSuiteName.TestMethodName`. See the [full documentation](https://ecoAPM.github.io/xunit.ts) for more details.
91+
8892
## Output
8993

9094
### Console
@@ -100,10 +104,10 @@ My Test Suite
100104
Passed: 1
101105
Total: 1
102106
103-
~/example $ _
107+
~/example $ _
104108
```
105109

106-
See the [full documentation](https://ecoAPM.github.io/xunit.ts) for a list of all available output options.
110+
Results can also be output in JUnit and Sonar XML formats, for import into other systems. See the [full documentation](https://ecoAPM.github.io/xunit.ts) for a list of all available output options.
107111

108112
## Assertions
109113

TSDoc/Generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class Generator {
3737
const md = Generator.getMarkdown(filename, doc);
3838

3939
if (md !== null) {
40-
const out = path.join(out_path, file.substr(base.length + 1)).replace(/\.ts$/, ".md");
40+
const out = path.join(out_path, file.substring(base.length + 1)).replace(/\.ts$/, ".md");
4141
await this.fs_promises.mkdir(path.join(out_path, dir), { recursive: true });
4242
await this.fs_promises.writeFile(out, md);
4343
}

compiler-tests/CompilerTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default class CompilerTests extends TestSuite {
44
@Test()
55
async TestsAreFound() {
66
//act
7-
const tests = this.getTests();
7+
const tests = this.getTests([]);
88

99
//assert
1010
this.assert.count(1, Object.values(tests));

compiler-tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"parcel": "parcel build *.ts -d dist/parcel -t node --no-minify --log-level 2 && xunit dist/parcel",
1616
"rollup": "rollup -c && xunit dist/rollup",
1717
"typescript": "tsc && xunit dist/typescript",
18-
"vite": "vite build . && xunit dist/vite"
18+
"vite": "vite build . && xunit dist/vite",
19+
"all": "yarn typescript && yarn vite && yarn rollup && yarn parcel"
1920
}
2021
}

docs/index.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ At a minimum, your `tsconfig.json` will require the following:
2727
```json
2828
{
2929
"compilerOptions": {
30-
"target": "ES5",
31-
//or "ES6"
30+
"target": "ES5", //or "ES6"
3231
"module": "CommonJS",
3332
"experimentalDecorators": true
3433
}
@@ -76,4 +75,17 @@ You can also run `xunit.ts` from a script in your `package.json`:
7675
"test": "tsc --outDir compiled_tests_dir && xunit compiled_tests_dir"
7776
}
7877
}
79-
```
78+
```
79+
80+
### Filtering tests
81+
82+
If you don't want to run your entire test suite, you can pass one or more `--filter` flags to the `xunit` command.
83+
84+
Filters are regular expressions that will match against the string `{TestSuiteName}.{TestMethodName}`.
85+
86+
Using our example above, of a test suite named `MyTestSuite` with a test method named `MyFirstTest`, we could use any of the following filters to include that test in our test run:
87+
88+
- `MyTestSuite`
89+
- `MyFirstTest`
90+
- `MyTestSuite.MyFirstTest`
91+
- `^MyTestSuite\.MyFirstTest$`

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xunit.ts",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "A unit testing framework for TypeScript, following standard xUnit patterns",
55
"main": "dist/xunit.js",
66
"author": "ecoAPM LLC",
@@ -36,6 +36,7 @@
3636
"clean": "rm -rf dist",
3737
"build": "tsc",
3838
"test": "node dist/cli.js dist/tests",
39+
"xunit": "ts-node cli.ts",
3940
"tsdoc": "cp -r node_modules/notosans/* docs/assets && ts-node tsdoc.ts"
4041
},
4142
"bin": {

src/CLI.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export default class CLI {
1515
typeLabel: "<directory>",
1616
description: "The path where tests to run are located (-d/--dir flag optional)"
1717
},
18+
{
19+
name: "filter",
20+
alias: "f",
21+
type: String,
22+
multiple: true,
23+
typeLabel: "<regex>",
24+
description: "A regular expression to filter against TestSuiteName.TestMethodName()"
25+
},
1826
{
1927
name: "junit",
2028
alias: "j",
@@ -33,7 +41,7 @@ export default class CLI {
3341
name: "quiet",
3442
alias: "q",
3543
type: Boolean,
36-
description: "Do not print test results to stdout"
44+
description: "Do not print individual test results to stdout"
3745
},
3846
{
3947
name: "help",
@@ -51,16 +59,18 @@ export default class CLI {
5159
{
5260
header: "Usage",
5361
content: [
54-
"<npm run | yarn> xunit [-j|--junit [filename]] [-s|--sonar [filename]]",
55-
"[-q|--quiet] [-d|--dir] <directory>"
62+
"<npm run | yarn> xunit [-d|--dir] <directory>",
63+
"[-q|--quiet] [-j|--junit [filename]] [-s|--sonar [filename]] [-f|--filter regex]"
5664
]
5765
},
5866
{
5967
header: "Examples",
6068
content: [
6169
"npm run xunit dist/tests",
6270
"yarn xunit --junit results.xml --dir dist/tests --quiet",
63-
"yarn xunit -q -s -d dist/tests"
71+
"yarn xunit -q -s -d dist/tests",
72+
"yarn xunit dist/tests --filter MyTestSuite",
73+
"yarn xunit dist/tests -f MyTestSuite.JustOneTest",
6474
]
6575
},
6676
{
@@ -75,7 +85,7 @@ export default class CLI {
7585
}
7686

7787
async run(): Promise<boolean> {
78-
const args = Args(CLI.options, { argv: this.process.argv });
88+
const args = Args(CLI.options, {argv: this.process.argv});
7989

8090
if (args.help) {
8191
this.process.stdout.write(CLI.usage);
@@ -86,7 +96,8 @@ export default class CLI {
8696
const runner = this.runnerFactory(args);
8797

8898
try {
89-
const results = await runner.runAll(args.dir);
99+
const filters = args.filter ?? [];
100+
const results = await runner.runAll(args.dir, filters.map((f: string) => new RegExp(f)));
90101
return Runner.allTestsPassed(results);
91102
} catch (error) {
92103
if (error instanceof Error) {

src/Framework/TestSuite.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ export default abstract class TestSuite {
2626
this.tests = tests;
2727
}
2828

29-
getTests() {
30-
return this.tests;
29+
getTests(filters: RegExp[]) {
30+
return filters.length > 0
31+
? this.filteredTests(filters)
32+
: this.tests;
33+
}
34+
35+
filteredTests(filters: RegExp[]) {
36+
const filtered: Record<string, TestInfo> = {};
37+
const keys = Object.keys(this.tests).filter(k => filters.map(f => f.test(`${this.constructor.name}.${this.tests[k].value?.name}`)).filter(m => m).length > 0);
38+
keys.forEach(k => {
39+
return filtered[k] = this.tests[k];
40+
});
41+
return filtered;
3142
}
3243
}

src/Reporters/SonarReporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class SonarReporter extends XMLReporter {
1313
file: [
1414
{
1515
_attr: {
16-
path: file.substr(file.split(path.sep)[0].length + 1).replace(/\.js$/, ".ts"),
16+
path: file.substring(file.split(path.sep)[0].length + 1).replace(/\.js$/, ".ts"),
1717
}
1818
},
1919
...Object.keys(results.results)

0 commit comments

Comments
 (0)