-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathmocha-setup.js
229 lines (193 loc) · 6.7 KB
/
mocha-setup.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview
* - sets global.expect
* - configures mocha to use jest-snapshot
* - symlinks `fixtures/config-plugins/lighthouse-plugin-simple` to a place where the default
* config module resolution will find it
* - saves failed test results if env LH_FAILED_TESTS_FILE is set
*/
/* eslint-disable import/order */
// TODO: all of test-env should be moved to a root test folder
import fs from 'fs';
import path from 'path';
import {expect} from 'expect';
import * as td from 'testdouble';
import jestSnapshot from 'jest-snapshot';
import {LH_ROOT} from '../../../shared/root.js';
import './expect-setup.js';
import {timers} from './fake-timers.js';
const {SnapshotState, toMatchSnapshot, toMatchInlineSnapshot} = jestSnapshot;
// Use consistent TZ across testing environments.
// Timezone is used to construct date strings.
process.env.TZ = 'UTC';
// Expected to be set by lh-env.js
process.env.NODE_TEST = 'test';
global.expect = expect;
// Force marky to not use Node's performance, which is messed up by fake timers.
const performance = global.performance;
// @ts-expect-error
global.performance = undefined;
// @ts-expect-error: no types
await import('marky');
global.performance = performance;
/** @type {Map<string, SnapshotState['prototype']>} */
const snapshotStatesByTestFile = new Map();
let snapshotTestFailed = false;
/**
* @param {string} testFile
*/
function getSnapshotState(testFile) {
// For every test file, persist the same snapshot state object so there is
// not a read/write per snapshot access/change, but one per file.
let snapshotState = snapshotStatesByTestFile.get(testFile);
if (snapshotState) return snapshotState;
const snapshotDir = path.join(path.dirname(testFile), '__snapshots__');
const snapshotFile = path.join(snapshotDir, path.basename(testFile) + '.snap');
snapshotState = new SnapshotState(snapshotFile, {
rootDir: snapshotDir,
updateSnapshot: process.env.SNAPSHOT_UPDATE ? 'all' : 'none',
prettierPath: '',
snapshotFormat: {},
});
snapshotStatesByTestFile.set(testFile, snapshotState);
return snapshotState;
}
/**
* @param {Mocha.Test} test
* @return {string}
*/
function makeTestTitle(test) {
/** @type {Mocha.Test | Mocha.Suite} */
let next = test;
const title = [];
while (next.parent) {
title.push(next.title);
next = next.parent;
}
return title.reverse().join(' ');
}
expect.extend({
/**
* @param {any} actual
*/
toMatchSnapshot(actual) {
const test = mochaCurrentTest;
if (!test.file) throw new Error('unexpected value');
const title = makeTestTitle(test);
const snapshotState = getSnapshotState(test.file);
const context = {snapshotState, currentTestName: title};
// @ts-expect-error - this is enough for snapshots to work.
const matcher = toMatchSnapshot.bind(context);
const result = matcher(actual);
// @ts-expect-error - not sure why these types are so wrong
if (!result.pass) snapshotTestFailed = true;
return result;
},
/**
* @param {any} actual
* @param {any} expected
*/
toMatchInlineSnapshot(actual, expected) {
const test = mochaCurrentTest;
if (!test.file) throw new Error('unexpected value');
const title = makeTestTitle(test);
const snapshotState = getSnapshotState(test.file);
const context = {snapshotState, currentTestName: title};
// @ts-expect-error - this is enough for snapshots to work.
const matcher = toMatchInlineSnapshot.bind(context);
const result = matcher(actual, expected);
// @ts-expect-error - not sure why these types are so wrong
if (!result.pass) snapshotTestFailed = true;
return result;
},
});
const testPlugins = [
'lighthouse-plugin-no-category',
'lighthouse-plugin-no-groups',
'lighthouse-plugin-simple',
];
/** @type {Mocha.Test} */
let mochaCurrentTest;
/** @type {any[]} */
const failedTests = [];
const rootHooks = {
/** @this {Mocha.Context} */
beforeEach() {
if (!this.currentTest) throw new Error('unexpected value');
// Needed so `expect` extension method can access information about the current test.
mochaCurrentTest = this.currentTest;
// If a test is retried the snapshot indices will start where the previous attempt left off.
// This can lead to several problems including the test passing where it should be failing.
//
// Jest itself clears the snapshot state on retries although they seem to execute retries after
// all tests finish and not immediately after the initial test failure.
// https://github.com/jestjs/jest/pull/8629
if (this.currentTest.retries() && this.currentTest.file) {
const snapshotState = getSnapshotState(this.currentTest.file);
snapshotState.clear();
}
},
/** @this {Mocha.Context} */
afterEach() {
if (!this.currentTest) throw new Error('unexpected value');
const {file, title, state} = this.currentTest;
if (!file) throw new Error('unexpected value');
if (state === 'failed') {
failedTests.push({
file: path.relative(LH_ROOT, file),
title,
error: this.currentTest.err?.stack ?? this.currentTest.err?.toString(),
});
}
},
// This runs _once_, after all of the tests in the root suite.
async afterAll() {
timers.dispose();
td.reset();
for (const snapshotState of snapshotStatesByTestFile.values()) {
// Jest adds `file://` to inline snapshot paths, and uses its own fs module to read things,
// falling back to fs.readFileSync if not defined. node `fs` does not support
// protocols in the path specifier, so we remove it here.
// @ts-expect-error - private property.
for (const snapshot of snapshotState._inlineSnapshots) {
snapshot.frame.file = snapshot.frame.file.replace('file://', '');
}
snapshotState.save();
}
if (!process.env.SNAPSHOT_UPDATE && snapshotTestFailed) {
process.on('exit', () => {
console.log('To update snapshots, run again with `yarn mocha -u`');
});
}
if (process.env.LH_FAILED_TESTS_FILE && failedTests.length) {
fs.writeFileSync(process.env.LH_FAILED_TESTS_FILE, JSON.stringify(failedTests, null, 2));
}
},
};
function mochaGlobalSetup() {
for (const plugin of testPlugins) {
try {
fs.symlinkSync(
`${LH_ROOT}/core/test/fixtures/config-plugins/${plugin}`,
`${LH_ROOT}/node_modules/${plugin}`
);
} catch {
// Might exist already because process was killed before tests finished.
}
}
}
function mochaGlobalTeardown() {
for (const plugin of testPlugins) {
fs.unlinkSync(`${LH_ROOT}/node_modules/${plugin}`);
}
}
export {
rootHooks,
mochaGlobalSetup,
mochaGlobalTeardown,
};