/
runner.test.mjs
68 lines (58 loc) · 3.29 KB
/
runner.test.mjs
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
import assert from 'node:assert'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
import { execa } from 'execa'
const browser = process.env.BROWSER || (process.env.PROVIDER === 'playwright' ? 'chromium' : 'chrome')
const { stderr, stdout } = await execa('npx', ['vitest', '--run', `--browser.name=${browser}`, '--browser.headless'], {
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
reject: false,
})
await test('tests are actually running', async () => {
const browserResult = await readFile('./browser.json', 'utf-8')
const browserResultJson = JSON.parse(browserResult)
const passedTests = browserResultJson.testResults.filter(result => result.status === 'passed')
const failedTests = browserResultJson.testResults.filter(result => result.status === 'failed')
assert.ok(browserResultJson.testResults.length === 8, 'Not all the tests have been run')
assert.ok(passedTests.length === 7, 'Some tests failed')
assert.ok(failedTests.length === 1, 'Some tests have passed but should fail')
assert.doesNotMatch(stderr, /Unhandled Error/, 'doesn\'t have any unhandled errors')
})
await test('correctly prints error', () => {
assert.match(stderr, /expected 1 to be 2/, 'prints failing error')
assert.match(stderr, /- 2\s+\+ 1/, 'prints failing diff')
})
await test('logs are redirected to stdout', async () => {
assert.match(stdout, /stdout | test\/logs.test.ts > logging to stdout/)
assert.match(stdout, /hello from console.log/, 'prints console.log')
assert.match(stdout, /hello from console.info/, 'prints console.info')
assert.match(stdout, /hello from console.debug/, 'prints console.debug')
assert.match(stdout, /{ hello: 'from dir' }/, 'prints console.dir')
assert.match(stdout, /{ hello: 'from dirxml' }/, 'prints console.dixml')
// safari logs the stack files with @https://...
assert.match(stdout, /hello from console.trace\s+(\w+|@)/, 'prints console.trace')
assert.match(stdout, /dom <div \/>/, 'prints dom')
assert.match(stdout, /default: 1/, 'prints first default count')
assert.match(stdout, /default: 2/, 'prints second default count')
assert.match(stdout, /default: 3/, 'prints third default count')
assert.match(stdout, /count: 1/, 'prints first custom count')
assert.match(stdout, /count: 2/, 'prints second custom count')
assert.match(stdout, /count: 3/, 'prints third custom count')
assert.match(stdout, /default: [\d.]+ ms/, 'prints default time')
assert.match(stdout, /time: [\d.]+ ms/, 'prints custom time')
})
await test('logs are redirected to stderr', async () => {
assert.match(stderr, /stderr | test\/logs.test.ts > logging to stderr/)
assert.match(stderr, /hello from console.error/, 'prints console.log')
assert.match(stderr, /hello from console.warn/, 'prints console.info')
assert.match(stderr, /Timer "invalid timeLog" does not exist/, 'prints errored timeLog')
assert.match(stderr, /Timer "invalid timeEnd" does not exist/, 'prints errored timeEnd')
})
await test('popup apis should log a warning', () => {
assert.ok(stderr.includes('Vitest encountered a \`alert\("test"\)\`'), 'prints warning for alert')
assert.ok(stderr.includes('Vitest encountered a \`confirm\("test"\)\`'), 'prints warning for confirm')
assert.ok(stderr.includes('Vitest encountered a \`prompt\("test"\)\`'), 'prints warning for prompt')
})