forked from glimmerjs/glimmer-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-harness.ts
95 lines (79 loc) · 2.5 KB
/
setup-harness.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
86
87
88
89
90
91
92
93
94
95
/* eslint-disable no-console */
import { debug } from '@glimmer/validator';
import { autoRegister } from 'js-reporters';
export async function setupQunit() {
const qunit = await import('qunit');
await import('qunit/qunit/qunit.css');
const runner = autoRegister();
// @ts-expect-error qunit types don't expose "reporters"
const tap = qunit.reporters.tap;
tap.init(runner, { log: console.info });
QUnit.config.urlConfig.push({
id: 'smoke_tests',
label: 'Enable Smoke Tests',
tooltip: 'Enable Smoke Tests',
});
QUnit.config.urlConfig.push({
id: 'ci',
label: 'Enable CI Mode',
tooltip: 'CI mode makes tests run faster by sacrificing UI responsiveness',
});
await Promise.resolve();
const qunitDiv = document.createElement('div');
qunitDiv.id = 'qunit';
const qunitFixtureDiv = document.createElement('div');
qunitFixtureDiv.id = 'qunit-fixture';
document.body.append(qunitDiv, qunitFixtureDiv);
console.log(`[HARNESS] ci=${hasFlag('ci')}`);
QUnit.testStart(() => {
debug.resetTrackingTransaction?.();
});
if (!hasFlag('ci')) {
// since all of our tests are synchronous, the QUnit
// UI never has a chance to rerender / update. This
// leads to a very long "white screen" when running
// the tests
//
// this adds a very small amount of async, just to allow
// the QUnit UI to rerender once per module completed
const pause = () =>
new Promise<void>((res) => {
setTimeout(res, 1);
});
let start = performance.now();
qunit.testDone(async () => {
let gap = performance.now() - start;
if (gap > 200) {
await pause();
start = performance.now();
}
});
qunit.moduleDone(pause);
}
// @ts-expect-error missing in types, does exist: https://api.qunitjs.com/callbacks/QUnit.on/#the-testend-event
QUnit.on('testEnd', (testEnd) => {
if (testEnd.status === 'failed') {
testEnd.errors.forEach((assertion: any) => {
console.error(assertion.stack);
// message: speedometer
// actual: 75
// expected: 88
// stack: at dmc.test.js:12
});
}
});
qunit.done(({ failed }) => {
if (failed > 0) {
console.log('[HARNESS] fail');
} else {
console.log('[HARNESS] done');
}
});
return {
smokeTest: hasFlag('smoke_test'),
};
}
function hasFlag(flag: string): boolean {
let location = typeof window !== 'undefined' && window.location;
return location && new RegExp(`[?&]${flag}`).test(location.search);
}