/
failures.test.ts
161 lines (124 loc) · 6.38 KB
/
failures.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { expect, test } from 'vitest'
import type { UserConfig } from 'vitest/config'
import { version } from 'vitest/package.json'
import { normalize, resolve } from 'pathe'
import * as testUtils from '../../test-utils'
function runVitest(config: NonNullable<UserConfig['test']> & { shard?: any }) {
return testUtils.runVitest(config, ['fixtures/test/'])
}
function runVitestCli(...cliArgs: string[]) {
return testUtils.runVitestCli('run', 'fixtures/test/', ...cliArgs)
}
test('shard cannot be used with watch mode', async () => {
const { stderr } = await runVitest({ watch: true, shard: '1/2' })
expect(stderr).toMatch('Error: You cannot use --shard option with enabled watch')
})
test('shard must be positive number', async () => {
const { stderr } = await runVitest({ shard: '-1' })
expect(stderr).toMatch('Error: --shard <count> must be a positive number')
})
test('shard index must be smaller than count', async () => {
const { stderr } = await runVitest({ shard: '2/1' })
expect(stderr).toMatch('Error: --shard <index> must be a positive number less then <count>')
})
test('inspect requires changing pool and singleThread/singleFork', async () => {
const { stderr } = await runVitest({ inspect: true })
expect(stderr).toMatch('Error: You cannot use --inspect without "--no-file-parallelism", "poolOptions.threads.singleThread" or "poolOptions.forks.singleFork"')
})
test('inspect cannot be used with multi-threading', async () => {
const { stderr } = await runVitest({ inspect: true, pool: 'threads', poolOptions: { threads: { singleThread: false } } })
expect(stderr).toMatch('Error: You cannot use --inspect without "--no-file-parallelism", "poolOptions.threads.singleThread" or "poolOptions.forks.singleFork"')
})
test('inspect-brk cannot be used with multi processing', async () => {
const { stderr } = await runVitest({ inspect: true, pool: 'forks', poolOptions: { forks: { singleFork: false } } })
expect(stderr).toMatch('Error: You cannot use --inspect without "--no-file-parallelism", "poolOptions.threads.singleThread" or "poolOptions.forks.singleFork"')
})
test('c8 coverage provider is not supported', async () => {
// @ts-expect-error -- check for removed API option
const { stderr } = await runVitest({ coverage: { enabled: true, provider: 'c8' } })
expect(stderr).toMatch('Error: "coverage.provider: c8" is not supported anymore. Use "coverage.provider: v8" instead')
})
test('v8 coverage provider cannot be used with browser', async () => {
const { stderr } = await runVitest({ coverage: { enabled: true }, browser: { enabled: true, name: 'chrome' } })
expect(stderr).toMatch('Error: @vitest/coverage-v8 does not work with --browser. Use @vitest/coverage-istanbul instead')
})
test('v8 coverage provider cannot be used with browser in workspace', async () => {
const { stderr } = await runVitest({ coverage: { enabled: true }, workspace: './fixtures/workspace/browser/workspace-with-browser.ts' })
expect(stderr).toMatch('Error: @vitest/coverage-v8 does not work with --browser. Use @vitest/coverage-istanbul instead')
})
test('coverage reportsDirectory cannot be current working directory', async () => {
const { stderr } = await runVitest({
coverage: {
enabled: true,
reportsDirectory: './',
// Additional options to make sure this test doesn't accidentally remove whole vitest project
clean: false,
cleanOnRerun: false,
provider: 'custom',
customProviderModule: 'non-existing-provider-so-that-reportsDirectory-is-not-removed',
},
})
const directory = normalize(resolve('./'))
expect(stderr).toMatch(`Error: You cannot set "coverage.reportsDirectory" as ${directory}. Vitest needs to be able to remove this directory before test run`)
})
test('coverage reportsDirectory cannot be root', async () => {
const { stderr } = await runVitest({
root: './fixtures',
coverage: {
enabled: true,
reportsDirectory: './',
// Additional options to make sure this test doesn't accidentally remove whole vitest project
clean: false,
cleanOnRerun: false,
provider: 'custom',
customProviderModule: 'non-existing-provider-so-that-reportsDirectory-is-not-removed',
},
})
const directory = normalize(resolve('./fixtures'))
expect(stderr).toMatch(`Error: You cannot set "coverage.reportsDirectory" as ${directory}. Vitest needs to be able to remove this directory before test run`)
})
test('version number is printed when coverage provider fails to load', async () => {
const { stderr, stdout } = await runVitest({
coverage: {
enabled: true,
provider: 'custom',
customProviderModule: './non-existing-module.ts',
},
})
expect(stdout).toMatch(`RUN v${version}`)
expect(stderr).toMatch('Error: Failed to load custom CoverageProviderModule from ./non-existing-module.ts')
})
test('coverage.autoUpdate cannot update thresholds when configuration file doesnt define them', async () => {
const { stderr } = await runVitest({
coverage: {
enabled: true,
thresholds: {
autoUpdate: true,
lines: 0,
},
},
})
expect(stderr).toMatch('Error: Unable to parse thresholds from configuration file: Expected config.test.coverage.thresholds to be an object')
})
test('boolean flag 100 should not crash CLI', async () => {
const { stderr } = await runVitestCli('--coverage.enabled', '--coverage.thresholds.100')
expect(stderr).toMatch('ERROR: Coverage for lines (0%) does not meet global threshold (100%)')
expect(stderr).toMatch('ERROR: Coverage for functions (0%) does not meet global threshold (100%)')
expect(stderr).toMatch('ERROR: Coverage for statements (0%) does not meet global threshold (100%)')
expect(stderr).toMatch('ERROR: Coverage for branches (0%) does not meet global threshold (100%)')
})
test('nextTick cannot be mocked inside child_process', async () => {
const { stderr } = await runVitest({
pool: 'forks',
fakeTimers: { toFake: ['nextTick'] },
include: ['./fixtures/test/fake-timers.test.ts'],
})
expect(stderr).toMatch('Error: vi.useFakeTimers({ toFake: ["nextTick"] }) is not supported in node:child_process. Use --pool=threads if mocking nextTick is required.')
})
test('nextTick can be mocked inside worker_threads', async () => {
const { stderr } = await runVitest({
fakeTimers: { toFake: ['nextTick'] },
include: ['./fixtures/test/fake-timers.test.ts'],
})
expect(stderr).not.toMatch('Error')
})