Skip to content

Commit 4aed867

Browse files
authored
fix: should exclude test files from coverage collection (#559)
1 parent 8f6c612 commit 4aed867

File tree

14 files changed

+187
-143
lines changed

14 files changed

+187
-143
lines changed

cspell.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
'node_modules',
1919
'pnpm-lock.yaml',
2020
'LICENSE.md',
21-
'fixtures',
21+
'e2e/**',
2222
],
2323
flagWords: banWords,
2424
dictionaries: ['dictionary'],

e2e/projects/coverage.test.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { runRstestCli } from '../scripts';
77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = dirname(__filename);
99

10-
describe('test projects coverage', () => {
10+
// TODO: swc-plugin-coverage-instrument `unstableExclude` option should works in windows
11+
describe.skipIf(process.platform === 'win32')('test projects coverage', () => {
1112
it('should run projects correctly with coverage', async () => {
1213
const { cli, expectExecSuccess } = await runRstestCli({
1314
command: 'rstest',
@@ -23,20 +24,12 @@ describe('test projects coverage', () => {
2324
const logs = cli.stdout.split('\n').filter(Boolean);
2425

2526
expect(
26-
logs.find(
27-
(log) =>
28-
log.includes('All files') &&
29-
log.replaceAll(' ', '').includes('100|100|100|100'),
30-
),
31-
).toBeTruthy();
27+
logs.find((log) => log.includes('All files'))?.replaceAll(' ', ''),
28+
).toMatchInlineSnapshot(`"Allfiles|100|100|100|100|"`);
3229

3330
expect(
34-
logs.find(
35-
(log) =>
36-
log.includes('client/test') &&
37-
log.replaceAll(' ', '').includes('100|100|100|100'),
38-
),
39-
).toBeTruthy();
31+
logs.find((log) => log.includes('client/src'))?.replaceAll(' ', ''),
32+
).toMatchInlineSnapshot(`"client/src|100|100|100|100|"`);
4033

4134
expect(
4235
logs.find(

e2e/test-coverage/fixtures/rstest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export default defineConfig({
55
enabled: true,
66
provider: 'istanbul',
77
},
8+
setupFiles: ['./rstest.setup.ts'],
89
});

e2e/test-coverage/fixtures/rstest.reportsDirectory.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export default defineConfig({
66
provider: 'istanbul',
77
reportsDirectory: 'test-temp-coverage',
88
},
9+
setupFiles: ['./rstest.setup.ts'],
910
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { beforeAll } from '@rstest/core';
2+
3+
beforeAll(() => {
4+
process.env.rstest = '1';
5+
});

e2e/test-coverage/fixtures/rstest.skipFull.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export default defineConfig({
66
provider: 'istanbul',
77
reporters: [['text', { skipFull: true }]],
88
},
9+
setupFiles: ['./rstest.setup.ts'],
910
});

e2e/test-coverage/fixtures/test/index.test.ts renamed to e2e/test-coverage/fixtures/src/index.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from '@rstest/core';
2-
import { sayHi } from '../src/index';
2+
import { sayHi } from './index';
33

44
describe('Index', () => {
55
it('should add two numbers correctly', () => {
@@ -9,4 +9,8 @@ describe('Index', () => {
99
it('should test source code correctly', () => {
1010
expect(sayHi()).toBe('hi');
1111
});
12+
13+
it('should test env correctly', () => {
14+
expect(process.env.rstest).toBe('1');
15+
});
1216
});

e2e/test-coverage/index.test.ts

Lines changed: 112 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,128 @@
11
import { join } from 'node:path';
2-
import { expect, it } from '@rstest/core';
2+
import { describe, expect, it } from '@rstest/core';
33
import fs from 'fs-extra';
44
import { runRstestCli } from '../scripts';
55

6-
it('coverage-istanbul', async () => {
7-
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
8-
command: 'rstest',
9-
args: ['run'],
10-
options: {
11-
nodeOptions: {
12-
cwd: join(__dirname, 'fixtures'),
6+
// TODO: swc-plugin-coverage-instrument `unstableExclude` option should works in windows
7+
describe.skipIf(process.platform === 'win32')('test coverage-istanbul', () => {
8+
it('coverage-istanbul', async () => {
9+
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
10+
command: 'rstest',
11+
args: ['run'],
12+
options: {
13+
nodeOptions: {
14+
cwd: join(__dirname, 'fixtures'),
15+
},
1316
},
14-
},
17+
});
18+
19+
await expectExecSuccess();
20+
21+
const logs = cli.stdout.split('\n').filter(Boolean);
22+
23+
expectLog('Coverage enabled with istanbul', logs);
24+
25+
// test coverage
26+
expect(
27+
logs.find(
28+
(log) =>
29+
log.includes('index.test.ts') &&
30+
log.replaceAll(' ', '').includes('100|100|100|100'),
31+
),
32+
).toBeFalsy();
33+
34+
expect(
35+
logs.find(
36+
(log) =>
37+
log.includes('rstest.setup.ts') &&
38+
log.replaceAll(' ', '').includes('100|100|100|100'),
39+
),
40+
).toBeFalsy();
41+
42+
expect(
43+
logs.find(
44+
(log) =>
45+
log.includes('index.ts') &&
46+
log.replaceAll(' ', '').includes('100|100|100|100'),
47+
),
48+
).toBeTruthy();
49+
expect(
50+
logs.find((log) => log.includes('string.ts'))?.replaceAll(' ', ''),
51+
).toMatchInlineSnapshot(`"string.ts|93.75|100|83.33|92.85|7"`);
52+
53+
expect(
54+
logs.find((log) => log.includes('All files'))?.replaceAll(' ', ''),
55+
).toMatchInlineSnapshot(`"Allfiles|98.33|100|94.44|98.21|"`);
56+
57+
// text reporter
58+
expectLog('% Stmts', logs);
59+
60+
// html reporter
61+
expect(
62+
fs.existsSync(join(__dirname, 'fixtures/coverage/index.html')),
63+
).toBeTruthy();
64+
65+
// clover reporter
66+
expect(
67+
fs.existsSync(join(__dirname, 'fixtures/coverage/clover.xml')),
68+
).toBeTruthy();
69+
70+
// json reporter
71+
expect(
72+
fs.existsSync(join(__dirname, 'fixtures/coverage/coverage-final.json')),
73+
).toBeTruthy();
1574
});
1675

17-
await expectExecSuccess();
18-
19-
const logs = cli.stdout.split('\n').filter(Boolean);
20-
21-
expectLog('Coverage enabled with istanbul', logs);
22-
23-
// test coverage
24-
expect(
25-
logs.find(
26-
(log) =>
27-
log.includes('index.ts') &&
28-
log.replaceAll(' ', '').includes('100|100|100|100'),
29-
),
30-
).toBeTruthy();
31-
expect(
32-
logs.find(
33-
(log) =>
34-
log.includes('string.ts') &&
35-
log.replaceAll(' ', '').includes('93.75|100|83.33|92.85|7'),
36-
),
37-
).toBeTruthy();
38-
// TODO: should not include test files
39-
expect(
40-
logs.find(
41-
(log) =>
42-
log.includes('All files') &&
43-
log.replaceAll(' ', '').includes('99.43|100|98.68|99.41'),
44-
),
45-
).toBeTruthy();
46-
47-
// text reporter
48-
expectLog('% Stmts', logs);
49-
50-
// html reporter
51-
expect(
52-
fs.existsSync(join(__dirname, 'fixtures/coverage/index.html')),
53-
).toBeTruthy();
54-
55-
// clover reporter
56-
expect(
57-
fs.existsSync(join(__dirname, 'fixtures/coverage/clover.xml')),
58-
).toBeTruthy();
59-
60-
// json reporter
61-
expect(
62-
fs.existsSync(join(__dirname, 'fixtures/coverage/coverage-final.json')),
63-
).toBeTruthy();
64-
});
65-
66-
it('coverage-istanbul with custom options', async () => {
67-
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
68-
command: 'rstest',
69-
args: ['run', '-c', 'rstest.skipFull.config.ts'],
70-
options: {
71-
nodeOptions: {
72-
cwd: join(__dirname, 'fixtures'),
76+
it('coverage-istanbul with custom options', async () => {
77+
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
78+
command: 'rstest',
79+
args: ['run', '-c', 'rstest.skipFull.config.ts'],
80+
options: {
81+
nodeOptions: {
82+
cwd: join(__dirname, 'fixtures'),
83+
},
7384
},
74-
},
75-
});
85+
});
7686

77-
await expectExecSuccess();
78-
79-
const logs = cli.stdout.split('\n').filter(Boolean);
80-
81-
expectLog('Coverage enabled with istanbul', logs);
82-
83-
// test coverage
84-
expect(
85-
logs.find(
86-
(log) =>
87-
log.includes('index.ts') &&
88-
log.replaceAll(' ', '').includes('100|100|100|100'),
89-
),
90-
).toBeFalsy();
91-
expect(
92-
logs.find(
93-
(log) =>
94-
log.includes('string.ts') &&
95-
log.replaceAll(' ', '').includes('93.75|100|83.33|92.85|7'),
96-
),
97-
).toBeTruthy();
98-
99-
// text reporter
100-
expectLog('% Stmts', logs);
101-
102-
expect(
103-
fs.existsSync(join(__dirname, 'fixtures/coverage/index.html')),
104-
).toBeFalsy();
105-
});
87+
await expectExecSuccess();
10688

107-
it('coverage-istanbul with custom reportsDirectory', async () => {
108-
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
109-
command: 'rstest',
110-
args: ['run', '-c', 'rstest.reportsDirectory.config.ts'],
111-
options: {
112-
nodeOptions: {
113-
cwd: join(__dirname, 'fixtures'),
114-
},
115-
},
89+
const logs = cli.stdout.split('\n').filter(Boolean);
90+
91+
expectLog('Coverage enabled with istanbul', logs);
92+
93+
// test coverage
94+
expect(logs.find((log) => log.includes('index.ts'))).toBeFalsy();
95+
expect(
96+
logs.find((log) => log.includes('string.ts'))?.replaceAll(' ', ''),
97+
).toMatchInlineSnapshot(`"string.ts|93.75|100|83.33|92.85|7"`);
98+
99+
// text reporter
100+
expectLog('% Stmts', logs);
101+
102+
expect(
103+
fs.existsSync(join(__dirname, 'fixtures/coverage/index.html')),
104+
).toBeFalsy();
116105
});
117106

118-
await expectExecSuccess();
107+
it('coverage-istanbul with custom reportsDirectory', async () => {
108+
const { expectExecSuccess, expectLog, cli } = await runRstestCli({
109+
command: 'rstest',
110+
args: ['run', '-c', 'rstest.reportsDirectory.config.ts'],
111+
options: {
112+
nodeOptions: {
113+
cwd: join(__dirname, 'fixtures'),
114+
},
115+
},
116+
});
117+
118+
await expectExecSuccess();
119119

120-
const logs = cli.stdout.split('\n').filter(Boolean);
120+
const logs = cli.stdout.split('\n').filter(Boolean);
121121

122-
expectLog('Coverage enabled with istanbul', logs);
122+
expectLog('Coverage enabled with istanbul', logs);
123123

124-
expect(
125-
fs.existsSync(join(__dirname, 'fixtures/test-temp-coverage/index.html')),
126-
).toBeTruthy();
124+
expect(
125+
fs.existsSync(join(__dirname, 'fixtures/test-temp-coverage/index.html')),
126+
).toBeTruthy();
127+
});
127128
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"devDependencies": {
3333
"@biomejs/biome": "^2.2.2",
3434
"@changesets/cli": "^2.29.6",
35+
"@rstest/coverage-istanbul": "workspace:*",
3536
"@rsdoctor/rspack-plugin": "^1.2.3",
3637
"@rslint/core": "^0.1.12",
3738
"@rstest/core": "workspace:*",

packages/core/src/config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ const createDefaultConfig = (): NormalizedConfig => ({
112112
printConsoleTrace: false,
113113
disableConsoleIntercept: false,
114114
coverage: {
115+
exclude: [
116+
'**/node_modules/**',
117+
'**/dist/**',
118+
'**/test/**',
119+
'**/__tests__/**',
120+
'**/__mocks__/**',
121+
// This option accepts an array of wax(https://crates.io/crates/wax)-compatible glob patterns
122+
// not support `?()`: '**/*.{test,spec}.?(c|m)[jt]s?(x)',
123+
'**/*.{test,spec}.[jt]s',
124+
'**/*.{test,spec}.[c|m][jt]s',
125+
'**/*.{test,spec}.[jt]sx',
126+
'**/*.{test,spec}.[c|m][jt]sx',
127+
],
115128
enabled: false,
116129
provider: 'istanbul',
117130
reporters: ['text', 'html', 'clover', 'json'],

0 commit comments

Comments
 (0)