-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(coverage): remove empty coverage folder on test failure too (#6547)
- Loading branch information
1 parent
8723242
commit 1371ca6
Showing
7 changed files
with
140 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,37 @@ | ||
import { existsSync, readdirSync } from 'node:fs' | ||
import { expect } from 'vitest' | ||
import { coverageTest, normalizeURL, runVitest, test } from '../utils' | ||
import { existsSync } from 'node:fs' | ||
import { beforeEach, expect } from 'vitest' | ||
import { captureStdout, coverageTest, normalizeURL, runVitest, test } from '../utils' | ||
import { sum } from '../fixtures/src/math' | ||
|
||
beforeEach(() => { | ||
return captureStdout() | ||
}) | ||
|
||
test('empty coverage directory is cleaned after tests', async () => { | ||
await runVitest({ | ||
include: [normalizeURL(import.meta.url)], | ||
testNamePattern: 'passing test', | ||
coverage: { reporter: 'text', all: false }, | ||
}) | ||
|
||
if (existsSync('./coverage')) { | ||
if (readdirSync('./coverage').length !== 0) { | ||
throw new Error('Test case expected coverage directory to be empty') | ||
} | ||
expect(existsSync('./coverage')).toBe(false) | ||
}) | ||
|
||
test('empty coverage directory is cleaned after failing test run', async () => { | ||
const { exitCode } = await runVitest({ | ||
include: [normalizeURL(import.meta.url)], | ||
testNamePattern: 'failing test', | ||
coverage: { reporter: 'text', all: false }, | ||
}, { throwOnError: false }) | ||
|
||
throw new Error('Empty coverage directory was not cleaned') | ||
} | ||
expect(existsSync('./coverage')).toBe(false) | ||
expect(exitCode).toBe(1) | ||
}) | ||
|
||
coverageTest('cover some lines', () => { | ||
coverageTest('passing test', () => { | ||
expect(sum(2, 3)).toBe(5) | ||
}) | ||
|
||
coverageTest('failing test', () => { | ||
expect(sum(2, 3)).toBe(6) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { expect } from 'vitest' | ||
import { captureStdout, coverageTest, isV8Provider, normalizeURL, runVitest, test } from '../utils' | ||
import { sum } from '../fixtures/src/math' | ||
|
||
test('report is not generated when tests fail', async () => { | ||
const stdout = captureStdout() | ||
|
||
const { exitCode } = await runVitest({ | ||
include: [normalizeURL(import.meta.url)], | ||
coverage: { | ||
all: false, | ||
include: ['**/fixtures/src/math.ts'], | ||
reporter: 'text', | ||
}, | ||
}, { throwOnError: false }) | ||
|
||
expect(stdout()).toBe('') | ||
expect(exitCode).toBe(1) | ||
}) | ||
|
||
test('report is generated when tests fail and { reportOnFailure: true }', async () => { | ||
const stdout = captureStdout() | ||
|
||
const { exitCode } = await runVitest({ | ||
include: [normalizeURL(import.meta.url)], | ||
coverage: { | ||
all: false, | ||
include: ['**/fixtures/src/math.ts'], | ||
reporter: 'text', | ||
reportOnFailure: true, | ||
}, | ||
}, { throwOnError: false }) | ||
|
||
if (isV8Provider()) { | ||
expect(stdout()).toMatchInlineSnapshot(` | ||
"----------|---------|----------|---------|---------|------------------- | ||
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ||
----------|---------|----------|---------|---------|------------------- | ||
All files | 50 | 100 | 25 | 50 | | ||
math.ts | 50 | 100 | 25 | 50 | 6-7,10-11,14-15 | ||
----------|---------|----------|---------|---------|------------------- | ||
" | ||
`) | ||
} | ||
else { | ||
expect(stdout()).toMatchInlineSnapshot(` | ||
"----------|---------|----------|---------|---------|------------------- | ||
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ||
----------|---------|----------|---------|---------|------------------- | ||
All files | 25 | 100 | 25 | 25 | | ||
math.ts | 25 | 100 | 25 | 25 | 6-14 | ||
----------|---------|----------|---------|---------|------------------- | ||
" | ||
`) | ||
} | ||
|
||
expect(exitCode).toBe(1) | ||
}) | ||
|
||
coverageTest('failing test', () => { | ||
expect(sum(1, 2)).toBe(4) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters