/
html.test.ts
85 lines (81 loc) · 3.54 KB
/
html.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
import fs from 'fs'
import zlib from 'zlib'
import { resolve } from 'pathe'
import { execa } from 'execa'
import { describe, expect, it } from 'vitest'
import { parse } from 'flatted'
const skip = (process.platform === 'win32' || process.platform === 'darwin') && process.env.CI
describe.skipIf(skip)('html reporter', async () => {
const vitestRoot = resolve(__dirname, '../../..')
const root = resolve(__dirname, '../fixtures')
it('resolves to "passing" status for test file "all-passing-or-skipped"', async () => {
const [expected, testFile, basePath] = ['passing', 'all-passing-or-skipped', 'html/all-passing-or-skipped']
await execa('npx', ['vitest', 'run', testFile, '--reporter=html', `--outputFile=${basePath}/index.html`], {
cwd: root,
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
stdio: 'inherit',
}).catch(e => e)
const metaJsonGzipeed = fs.readFileSync(resolve(root, `${basePath}/html.meta.json.gz`))
const metaJson = zlib.gunzipSync(metaJsonGzipeed).toString('utf-8')
const indexHtml = fs.readFileSync(resolve(root, `${basePath}/index.html`), { encoding: 'utf-8' })
const resultJson = parse(metaJson.replace(new RegExp(vitestRoot, 'g'), '<rootDir>'))
resultJson.config = {} // doesn't matter for a test
const file = resultJson.files[0]
file.id = 0
file.collectDuration = 0
file.setupDuration = 0
file.result.duration = 0
file.result.startTime = 0
const task = file.tasks[0]
task.id = 0
task.result.duration = 0
task.result.startTime = 0
expect(task.result.error).not.toBeDefined()
expect(task.result.logs).not.toBeDefined()
expect(resultJson).toMatchSnapshot(`tests are ${expected}`)
expect(indexHtml).toMatch('window.METADATA_PATH="html.meta.json.gz"')
}, 120000)
it('resolves to "failing" status for test file "json-fail"', async () => {
const [expected, testFile, basePath] = ['failing', 'json-fail', 'html/fail']
await execa('npx', ['vitest', 'run', testFile, '--reporter=html', `--outputFile=${basePath}/index.html`], {
cwd: root,
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
stdio: 'inherit',
}).catch(e => e)
const metaJsonGzipped = fs.readFileSync(resolve(root, `${basePath}/html.meta.json.gz`))
const metaJson = zlib.gunzipSync(metaJsonGzipped).toString('utf-8')
const indexHtml = fs.readFileSync(resolve(root, `${basePath}/index.html`), { encoding: 'utf-8' })
const resultJson = parse(metaJson.replace(new RegExp(vitestRoot, 'g'), '<rootDir>'))
resultJson.config = {} // doesn't matter for a test
const file = resultJson.files[0]
file.id = 0
file.collectDuration = 0
file.setupDuration = 0
file.result.duration = 0
file.result.startTime = 0
const task = file.tasks[0]
task.id = 0
task.result.duration = 0
task.result.startTime = 0
expect(task.result.error).toBeDefined()
expect(task.result.errors).toBeDefined()
task.result.error.stack = task.result.error.stack.split('\n')[0]
task.result.errors[0].stack = task.result.errors[0].stack.split('\n')[0]
task.result.error.stackStr = task.result.error.stackStr.split('\n')[0]
task.result.errors[0].stackStr = task.result.errors[0].stackStr.split('\n')[0]
expect(task.logs).toBeDefined()
expect(task.logs).toHaveLength(1)
task.logs[0].taskId = 0
task.logs[0].time = 0
expect(resultJson).toMatchSnapshot(`tests are ${expected}`)
expect(indexHtml).toMatch('window.METADATA_PATH="html.meta.json.gz"')
}, 120000)
})