forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.test.ts
74 lines (66 loc) · 3.16 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
import fs from 'node:fs'
import zlib from 'node:zlib'
import { resolve } from 'pathe'
import { describe, expect, it } from 'vitest'
import { parse } from 'flatted'
import stripAnsi from 'strip-ansi'
import { runVitest } from '../../test-utils'
describe('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 runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root, env: { NO_COLOR: '1' } }, [testFile])
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.environmentLoad = 0
file.prepareDuration = 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.errors).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.test', 'html/fail']
await runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root, env: { NO_COLOR: '1' } }, [testFile])
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.environmentLoad = 0
file.prepareDuration = 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.errors).toBeDefined()
task.result.errors[0].stack = task.result.errors[0].stack.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 ${stripAnsi(expected)}`)
expect(indexHtml).toMatch('window.METADATA_PATH="html.meta.json.gz"')
}, 120000)
})