Skip to content

Commit 5de9af2

Browse files
authoredJul 31, 2023
fix(browser): multi-suite coverage (#3806)
1 parent dc4faf6 commit 5de9af2

File tree

8 files changed

+363
-3
lines changed

8 files changed

+363
-3
lines changed
 

‎packages/browser/src/client/runner.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl
3939
}
4040
}
4141

42-
async onAfterRunSuite() {
43-
await super.onAfterRunSuite?.()
42+
async onAfterRun() {
43+
await super.onAfterRun?.()
4444
const coverage = await coverageModule?.takeCoverage?.()
4545
await rpc().onAfterSuiteRun({ coverage })
4646
}

‎test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ exports[`custom json report 1`] = `
2222
"<process-cwd>/src/implicitElse.ts",
2323
"<process-cwd>/src/importEnv.ts",
2424
"<process-cwd>/src/index.mts",
25+
"<process-cwd>/src/multi-suite.ts",
2526
"<process-cwd>/src/utils.ts",
2627
],
2728
}

‎test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap

+83
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,89 @@ exports[`istanbul json report 1`] = `
13421342
},
13431343
},
13441344
},
1345+
"<process-cwd>/src/multi-suite.ts": {
1346+
"b": {},
1347+
"branchMap": {},
1348+
"f": {
1349+
"0": 1,
1350+
"1": 1,
1351+
},
1352+
"fnMap": {
1353+
"0": {
1354+
"decl": {
1355+
"end": {
1356+
"column": 8,
1357+
"line": 2,
1358+
},
1359+
"start": {
1360+
"column": 2,
1361+
"line": 2,
1362+
},
1363+
},
1364+
"loc": {
1365+
"end": {
1366+
"column": null,
1367+
"line": 4,
1368+
},
1369+
"start": {
1370+
"column": 21,
1371+
"line": 2,
1372+
},
1373+
},
1374+
"name": "(anonymous_0)",
1375+
},
1376+
"1": {
1377+
"decl": {
1378+
"end": {
1379+
"column": 8,
1380+
"line": 6,
1381+
},
1382+
"start": {
1383+
"column": 2,
1384+
"line": 6,
1385+
},
1386+
},
1387+
"loc": {
1388+
"end": {
1389+
"column": null,
1390+
"line": 8,
1391+
},
1392+
"start": {
1393+
"column": 21,
1394+
"line": 6,
1395+
},
1396+
},
1397+
"name": "(anonymous_1)",
1398+
},
1399+
},
1400+
"path": "<process-cwd>/src/multi-suite.ts",
1401+
"s": {
1402+
"0": 1,
1403+
"1": 1,
1404+
},
1405+
"statementMap": {
1406+
"0": {
1407+
"end": {
1408+
"column": null,
1409+
"line": 3,
1410+
},
1411+
"start": {
1412+
"column": 4,
1413+
"line": 3,
1414+
},
1415+
},
1416+
"1": {
1417+
"end": {
1418+
"column": null,
1419+
"line": 7,
1420+
},
1421+
"start": {
1422+
"column": 4,
1423+
"line": 7,
1424+
},
1425+
},
1426+
},
1427+
},
13451428
"<process-cwd>/src/untested-file.ts": {
13461429
"b": {
13471430
"0": [

‎test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap

+223
Original file line numberDiff line numberDiff line change
@@ -3305,6 +3305,229 @@ exports[`v8 json report 1`] = `
33053305
},
33063306
},
33073307
},
3308+
"<process-cwd>/src/multi-suite.ts": {
3309+
"all": false,
3310+
"b": {
3311+
"0": [
3312+
1,
3313+
],
3314+
"1": [
3315+
1,
3316+
],
3317+
},
3318+
"branchMap": {
3319+
"0": {
3320+
"line": 2,
3321+
"loc": {
3322+
"end": {
3323+
"column": 4,
3324+
"line": 4,
3325+
},
3326+
"start": {
3327+
"column": 2,
3328+
"line": 2,
3329+
},
3330+
},
3331+
"locations": [
3332+
{
3333+
"end": {
3334+
"column": 4,
3335+
"line": 4,
3336+
},
3337+
"start": {
3338+
"column": 2,
3339+
"line": 2,
3340+
},
3341+
},
3342+
],
3343+
"type": "branch",
3344+
},
3345+
"1": {
3346+
"line": 6,
3347+
"loc": {
3348+
"end": {
3349+
"column": 4,
3350+
"line": 8,
3351+
},
3352+
"start": {
3353+
"column": 2,
3354+
"line": 6,
3355+
},
3356+
},
3357+
"locations": [
3358+
{
3359+
"end": {
3360+
"column": 4,
3361+
"line": 8,
3362+
},
3363+
"start": {
3364+
"column": 2,
3365+
"line": 6,
3366+
},
3367+
},
3368+
],
3369+
"type": "branch",
3370+
},
3371+
},
3372+
"f": {
3373+
"0": 1,
3374+
"1": 1,
3375+
},
3376+
"fnMap": {
3377+
"0": {
3378+
"decl": {
3379+
"end": {
3380+
"column": 4,
3381+
"line": 4,
3382+
},
3383+
"start": {
3384+
"column": 2,
3385+
"line": 2,
3386+
},
3387+
},
3388+
"line": 2,
3389+
"loc": {
3390+
"end": {
3391+
"column": 4,
3392+
"line": 4,
3393+
},
3394+
"start": {
3395+
"column": 2,
3396+
"line": 2,
3397+
},
3398+
},
3399+
"name": "func1",
3400+
},
3401+
"1": {
3402+
"decl": {
3403+
"end": {
3404+
"column": 4,
3405+
"line": 8,
3406+
},
3407+
"start": {
3408+
"column": 2,
3409+
"line": 6,
3410+
},
3411+
},
3412+
"line": 6,
3413+
"loc": {
3414+
"end": {
3415+
"column": 4,
3416+
"line": 8,
3417+
},
3418+
"start": {
3419+
"column": 2,
3420+
"line": 6,
3421+
},
3422+
},
3423+
"name": "func2",
3424+
},
3425+
},
3426+
"path": "<process-cwd>/src/multi-suite.ts",
3427+
"s": {
3428+
"0": 1,
3429+
"1": 1,
3430+
"2": 1,
3431+
"3": 1,
3432+
"4": 1,
3433+
"5": 1,
3434+
"6": 1,
3435+
"7": 1,
3436+
"8": 1,
3437+
},
3438+
"statementMap": {
3439+
"0": {
3440+
"end": {
3441+
"column": 16,
3442+
"line": 1,
3443+
},
3444+
"start": {
3445+
"column": 0,
3446+
"line": 1,
3447+
},
3448+
},
3449+
"1": {
3450+
"end": {
3451+
"column": 22,
3452+
"line": 2,
3453+
},
3454+
"start": {
3455+
"column": 0,
3456+
"line": 2,
3457+
},
3458+
},
3459+
"2": {
3460+
"end": {
3461+
"column": 15,
3462+
"line": 3,
3463+
},
3464+
"start": {
3465+
"column": 0,
3466+
"line": 3,
3467+
},
3468+
},
3469+
"3": {
3470+
"end": {
3471+
"column": 4,
3472+
"line": 4,
3473+
},
3474+
"start": {
3475+
"column": 0,
3476+
"line": 4,
3477+
},
3478+
},
3479+
"4": {
3480+
"end": {
3481+
"column": 0,
3482+
"line": 5,
3483+
},
3484+
"start": {
3485+
"column": 0,
3486+
"line": 5,
3487+
},
3488+
},
3489+
"5": {
3490+
"end": {
3491+
"column": 22,
3492+
"line": 6,
3493+
},
3494+
"start": {
3495+
"column": 0,
3496+
"line": 6,
3497+
},
3498+
},
3499+
"6": {
3500+
"end": {
3501+
"column": 15,
3502+
"line": 7,
3503+
},
3504+
"start": {
3505+
"column": 0,
3506+
"line": 7,
3507+
},
3508+
},
3509+
"7": {
3510+
"end": {
3511+
"column": 4,
3512+
"line": 8,
3513+
},
3514+
"start": {
3515+
"column": 0,
3516+
"line": 8,
3517+
},
3518+
},
3519+
"8": {
3520+
"end": {
3521+
"column": 1,
3522+
"line": 9,
3523+
},
3524+
"start": {
3525+
"column": 0,
3526+
"line": 9,
3527+
},
3528+
},
3529+
},
3530+
},
33083531
"<process-cwd>/src/untested-file.ts": {
33093532
"all": true,
33103533
"b": {

‎test/coverage-test/coverage-report-tests/istanbul.report.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,16 @@ test('ignored code is excluded from the report', async () => {
3737
const utils = await import('../src/utils')
3838
expect(utils[functionName]).toBeTypeOf('function')
3939
})
40+
41+
test('tests with multiple suites are covered', async () => {
42+
const coverageMap = await readCoverageJson()
43+
44+
const filename = '<process-cwd>/src/multi-suite.ts'
45+
const fileCoverage = coverageMap[filename]
46+
47+
// Assert that all functions are covered
48+
expect(fileCoverage.f).toMatchObject({
49+
0: 1,
50+
1: 1,
51+
})
52+
})

‎test/coverage-test/coverage-report-tests/v8.report.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,16 @@ test('ignored code is marked as covered in the report', async () => {
2929
const utils = await import('../src/utils')
3030
expect(utils[functionName]).toBeTypeOf('function')
3131
})
32+
33+
test('tests with multiple suites are covered', async () => {
34+
const coverageMap = await readCoverageJson()
35+
36+
const filename = '<process-cwd>/src/multi-suite.ts'
37+
const fileCoverage = coverageMap[filename]
38+
39+
// Assert that all functions are covered
40+
expect(fileCoverage.f).toMatchObject({
41+
0: 1,
42+
1: 1,
43+
})
44+
})

‎test/coverage-test/src/multi-suite.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
func1(data: any[]) {
3+
return data
4+
},
5+
6+
func2(data: any[]) {
7+
return data
8+
},
9+
}

‎test/coverage-test/test/coverage.test.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from 'vitest'
1+
import { describe, expect, test } from 'vitest'
22

33
// @ts-expect-error -- untyped virtual file provided by custom plugin
44
import virtualFile1 from 'virtual:vitest-custom-virtual-file-1'
@@ -7,6 +7,7 @@ import { implicitElse } from '../src/implicitElse'
77
import { useImportEnv } from '../src/importEnv'
88
import { second } from '../src/function-count'
99
import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files'
10+
import MultiSuite from '../src/multi-suite'
1011

1112
// @ts-expect-error -- untyped virtual file provided by custom plugin
1213
import virtualFile2 from '\0vitest-custom-virtual-file-2'
@@ -40,6 +41,23 @@ test('cover function counts', () => {
4041
expect(second()).toBe(2)
4142
})
4243

44+
describe('Multiple test suites', () => {
45+
describe('func1()', () => {
46+
test('func1', () => {
47+
const data = ['a', 'b']
48+
const val = MultiSuite.func1(data)
49+
expect(val).toEqual(data)
50+
})
51+
})
52+
describe('func2()', () => {
53+
test('func2', () => {
54+
const data = ['c', 'd']
55+
const val = MultiSuite.func2(data)
56+
expect(val).toEqual(data)
57+
})
58+
})
59+
})
60+
4361
test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => {
4462
await runDynamicFileESM()
4563
})

0 commit comments

Comments
 (0)
Please sign in to comment.