-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathsetup.js
More file actions
77 lines (68 loc) · 2.69 KB
/
setup.js
File metadata and controls
77 lines (68 loc) · 2.69 KB
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
// This import ensures that the global `Mocha` object is present for mutation.
import { JestAsymmetricMatchers, JestChaiExpect, JestExtend } from '@vitest/expect';
import * as chai from 'chai';
import { registerCustomMatchers } from './matchers/index.js';
import { initSignals } from './signals.js';
initSignals();
// allows using expect.extend instead of chai.use to extend plugins
chai.use(JestExtend);
// adds all jest matchers to expect
chai.use(JestChaiExpect);
// adds asymmetric matchers like stringContaining, objectContaining
chai.use(JestAsymmetricMatchers);
// add our custom matchers
chai.use(registerCustomMatchers);
// expose so we don't need to import `expect` in every test file
globalThis.expect = chai.expect;
/**
* `@web/test-runner-mocha`'s autorun.js file inlines its own copy of mocha, and there's no direct
* way to modify the globals before the tests are executed. As a workaround, we predefine setters
* that modify the provided values when they get set by the autorun script.
* @param {string} name Global variable name
* @param {Function} replacer Function that takes the original value and returns a modified one
*/
function hijackGlobal(name, replacer) {
Object.defineProperty(globalThis, name, {
configurable: true,
enumerable: true,
set(original) {
Object.defineProperty(globalThis, name, {
configurable: true,
enumerable: true,
writable: true,
value: replacer(original) ?? original,
});
},
});
}
hijackGlobal('describe', (describe) => {
describe.runIf = (condition) => (condition ? globalThis.describe : globalThis.xdescribe);
describe.skipIf = (condition) => (condition ? globalThis.xdescribe : globalThis.describe);
});
hijackGlobal('it', (it) => {
it.runIf = (condition) => (condition ? globalThis.it : globalThis.xit);
it.skipIf = (condition) => (condition ? globalThis.xit : globalThis.it);
});
hijackGlobal('before', (before) => {
// Expose as an alias for migration
globalThis.beforeAll = before;
});
hijackGlobal('after', (after) => {
// Expose as an alias for migration
globalThis.afterAll = after;
});
hijackGlobal('afterEach', (afterEach) => {
afterEach(() => {
// FIXME: Boost test speed by moving this to only files that need it
// Ensure the DOM is in a clean state
document.body.replaceChildren();
document.head.replaceChildren();
window.__lwcResetGlobalStylesheets();
});
});
hijackGlobal('beforeEach', (beforeEach) => {
beforeEach(() => {
// FIXME: Boost test speed by moving this to only files that need it
window.__lwcResetAlreadyLoggedMessages();
});
});