Skip to content

Commit 6352420

Browse files
committed
feat: collect test location
1 parent 87cc4f3 commit 6352420

File tree

19 files changed

+361
-48
lines changed

19 files changed

+361
-48
lines changed

e2e/list/fixtures/c.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, it } from '@rstest/core';
2+
3+
describe.each([0])('test c describe each %#', () => {});
4+
5+
describe.for([0])('test c describe for %#', () => {});
6+
7+
describe.runIf(true)('test c describe runIf', () => {});
8+
9+
describe.skipIf(false)('test c describe skipIf', () => {});
10+
11+
it.each([0])('test c it each %#', () => {});
12+
13+
it.for([0])('test c it for %#', () => {});
14+
15+
it.runIf(true)('test c it runIf', () => {});
16+
17+
it.skipIf(false)('test c it skipIf', () => {});

e2e/list/index.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@ describe('test list command', () => {
2424

2525
expect(logs).toMatchInlineSnapshot(`
2626
[
27+
"a.test.ts > test a",
2728
"a.test.ts > test a > test a-1",
2829
"a.test.ts > test a-2",
30+
"b.test.ts > test b",
2931
"b.test.ts > test b > test b-1",
3032
"b.test.ts > test b-2",
33+
"c.test.ts > test c describe each 0",
34+
"c.test.ts > test c describe for 0",
35+
"c.test.ts > test c describe runIf",
36+
"c.test.ts > test c describe skipIf",
37+
"c.test.ts > test c it each 0",
38+
"c.test.ts > test c it for 0",
39+
"c.test.ts > test c it runIf",
40+
"c.test.ts > test c it skipIf",
3141
]
3242
`);
3343
});
@@ -50,6 +60,7 @@ describe('test list command', () => {
5060

5161
expect(logs).toMatchInlineSnapshot(`
5262
[
63+
"a.test.ts > test a",
5364
"a.test.ts > test a > test a-1",
5465
"a.test.ts > test a-2",
5566
]
@@ -73,8 +84,14 @@ describe('test list command', () => {
7384

7485
expect(logs).toMatchInlineSnapshot(`
7586
[
87+
"a.test.ts > test a",
7688
"a.test.ts > test a > test a-1",
7789
"a.test.ts > test a-2",
90+
"c.test.ts > test c describe each 0",
91+
"c.test.ts > test c describe for 0",
92+
"c.test.ts > test c describe runIf",
93+
"c.test.ts > test c describe skipIf",
94+
"c.test.ts > test c it each 0",
7895
]
7996
`);
8097
});
@@ -99,6 +116,44 @@ describe('test list command', () => {
99116
[
100117
"a.test.ts",
101118
"b.test.ts",
119+
"c.test.ts",
120+
]
121+
`);
122+
});
123+
124+
it('should list tests correctly with printLocation', async () => {
125+
const { cli, expectExecSuccess } = await runRstestCli({
126+
command: 'rstest',
127+
args: ['list', '--printLocation'],
128+
options: {
129+
nodeOptions: {
130+
cwd: join(__dirname, 'fixtures'),
131+
},
132+
},
133+
});
134+
135+
await expectExecSuccess();
136+
137+
const logs = cli.stdout?.split('\n').filter(Boolean);
138+
139+
// rspack transpiles describe() to (0,rstest.describe)(), so the location is end of the callee
140+
// FIXME rspack trasnpiles describe.for to describe["for"] so the location is different from describe.each
141+
expect(logs).toMatchInlineSnapshot(`
142+
[
143+
"a.test.ts:3:9 > test a",
144+
"a.test.ts:4:5 > test a > test a-1",
145+
"a.test.ts:9:3 > test a-2",
146+
"b.test.ts:3:9 > test b",
147+
"b.test.ts:4:5 > test b > test b-1",
148+
"b.test.ts:9:3 > test b-2",
149+
"c.test.ts:3:1 > test c describe each 0",
150+
"c.test.ts:5:13 > test c describe for 0",
151+
"c.test.ts:7:1 > test c describe runIf",
152+
"c.test.ts:9:1 > test c describe skipIf",
153+
"c.test.ts:11:1 > test c it each 0",
154+
"c.test.ts:13:7 > test c it for 0",
155+
"c.test.ts:15:1 > test c it runIf",
156+
"c.test.ts:17:1 > test c it skipIf",
102157
]
103158
`);
104159
});

e2e/list/json.test.ts

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,129 @@ describe('test list command with --json', () => {
2828
"[",
2929
" {",
3030
" "file": "<ROOT>/e2e/list/fixtures/a.test.ts",",
31-
" "name": "test a > test a-1"",
31+
" "name": "test a",",
32+
" "location": {",
33+
" "line": 3,",
34+
" "column": 9",
35+
" },",
36+
" "type": "suite"",
3237
" },",
3338
" {",
3439
" "file": "<ROOT>/e2e/list/fixtures/a.test.ts",",
35-
" "name": "test a-2"",
40+
" "name": "test a > test a-1",",
41+
" "location": {",
42+
" "line": 4,",
43+
" "column": 5",
44+
" },",
45+
" "type": "case"",
46+
" },",
47+
" {",
48+
" "file": "<ROOT>/e2e/list/fixtures/a.test.ts",",
49+
" "name": "test a-2",",
50+
" "location": {",
51+
" "line": 9,",
52+
" "column": 3",
53+
" },",
54+
" "type": "case"",
3655
" },",
3756
" {",
3857
" "file": "<ROOT>/e2e/list/fixtures/b.test.ts",",
39-
" "name": "test b > test b-1"",
58+
" "name": "test b",",
59+
" "location": {",
60+
" "line": 3,",
61+
" "column": 9",
62+
" },",
63+
" "type": "suite"",
4064
" },",
4165
" {",
4266
" "file": "<ROOT>/e2e/list/fixtures/b.test.ts",",
43-
" "name": "test b-2"",
67+
" "name": "test b > test b-1",",
68+
" "location": {",
69+
" "line": 4,",
70+
" "column": 5",
71+
" },",
72+
" "type": "case"",
73+
" },",
74+
" {",
75+
" "file": "<ROOT>/e2e/list/fixtures/b.test.ts",",
76+
" "name": "test b-2",",
77+
" "location": {",
78+
" "line": 9,",
79+
" "column": 3",
80+
" },",
81+
" "type": "case"",
82+
" },",
83+
" {",
84+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
85+
" "name": "test c describe each 0",",
86+
" "location": {",
87+
" "line": 3,",
88+
" "column": 1",
89+
" },",
90+
" "type": "suite"",
91+
" },",
92+
" {",
93+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
94+
" "name": "test c describe for 0",",
95+
" "location": {",
96+
" "line": 5,",
97+
" "column": 13",
98+
" },",
99+
" "type": "suite"",
100+
" },",
101+
" {",
102+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
103+
" "name": "test c describe runIf",",
104+
" "location": {",
105+
" "line": 7,",
106+
" "column": 1",
107+
" },",
108+
" "type": "suite"",
109+
" },",
110+
" {",
111+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
112+
" "name": "test c describe skipIf",",
113+
" "location": {",
114+
" "line": 9,",
115+
" "column": 1",
116+
" },",
117+
" "type": "suite"",
118+
" },",
119+
" {",
120+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
121+
" "name": "test c it each 0",",
122+
" "location": {",
123+
" "line": 11,",
124+
" "column": 1",
125+
" },",
126+
" "type": "case"",
127+
" },",
128+
" {",
129+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
130+
" "name": "test c it for 0",",
131+
" "location": {",
132+
" "line": 13,",
133+
" "column": 7",
134+
" },",
135+
" "type": "case"",
136+
" },",
137+
" {",
138+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
139+
" "name": "test c it runIf",",
140+
" "location": {",
141+
" "line": 15,",
142+
" "column": 1",
143+
" },",
144+
" "type": "case"",
145+
" },",
146+
" {",
147+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
148+
" "name": "test c it skipIf",",
149+
" "location": {",
150+
" "line": 17,",
151+
" "column": 1",
152+
" },",
153+
" "type": "case"",
44154
" }",
45155
"]",
46156
]
@@ -66,10 +176,16 @@ describe('test list command with --json', () => {
66176
[
67177
"[",
68178
" {",
69-
" "file": "<ROOT>/e2e/list/fixtures/a.test.ts"",
179+
" "file": "<ROOT>/e2e/list/fixtures/a.test.ts",",
180+
" "type": "file"",
181+
" },",
182+
" {",
183+
" "file": "<ROOT>/e2e/list/fixtures/b.test.ts",",
184+
" "type": "file"",
70185
" },",
71186
" {",
72-
" "file": "<ROOT>/e2e/list/fixtures/b.test.ts"",
187+
" "file": "<ROOT>/e2e/list/fixtures/c.test.ts",",
188+
" "type": "file"",
73189
" }",
74190
"]",
75191
]

e2e/reporter/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe.concurrent('reporters', () => {
7676
cli.stdout.match(/\[custom reporter\] onTestCaseStart/g)?.length,
7777
).toBe(3);
7878
expect(cli.stdout).toContain('[custom reporter] onTestFileStart');
79+
expect(cli.stdout).toContain('[custom reporter] onTestFileReady');
7980

8081
expect(
8182
cli.stdout.match(/\[custom reporter\] onTestCaseResult/g)?.length,

e2e/reporter/rstest.customReporterConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class MyReporter implements Reporter {
1414
reporterResult.push('[custom reporter] onTestFileStart');
1515
}
1616

17+
onTestFileReady(_file: TestFileInfo) {
18+
reporterResult.push('[custom reporter] onTestFileReady');
19+
}
20+
1721
onTestCaseStart(_test: TestCaseInfo) {
1822
reporterResult.push('[custom reporter] onTestCaseStart');
1923
}

e2e/rstest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig } from '@rstest/core';
22

33
export default defineConfig({
44
setupFiles: ['../scripts/rstest.setup.ts'],
5-
testTimeout: process.env.CI ? 10_000 : 5_000,
5+
testTimeout: 10_000,
66
slowTestThreshold: 2_000,
77
output: {
88
externals: {

packages/core/src/cli/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export function setupCommands(): void {
176176
.command('list [...filters]', 'lists all test files that Rstest will run')
177177
.option('--filesOnly', 'only list the test files')
178178
.option('--json [boolean/path]', 'print tests as JSON or write to a file')
179+
.option('--printLocation', 'print test case location')
179180
.action(
180181
async (
181182
filters: string[],
@@ -193,6 +194,7 @@ export function setupCommands(): void {
193194
await rstest.listTests({
194195
filesOnly: options.filesOnly,
195196
json: options.json,
197+
printLocation: options.printLocation,
196198
});
197199
} catch (err) {
198200
logger.error('Failed to run Rstest list.');

packages/core/src/core/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export function createRstest(
3838
await runTests(context);
3939
};
4040

41-
const listTests = async (options: ListCommandOptions): Promise<void> => {
41+
const listTests = async (options: ListCommandOptions) => {
4242
const { listTests } = await import('./listTests');
43-
await listTests(context, options);
43+
return listTests(context, options);
4444
};
4545

4646
return {

0 commit comments

Comments
 (0)