Skip to content

Commit 8c7c604

Browse files
committed
use Testem.afterTests instead of QUnit.done
Sometimes, like in CI environments, there can exist a race condition where Testem closes before the local middleware reporter can finish writing the reports to the filesystem. According to [Testem's README][1], it appears the proper way to do this is to use `Testem.afterTests` to send reports. This will keep testem open and prevent the process from exiting before the reports are written. This seems to be a [known issue with Testem itself][2]. [1]: https://github.com/testem/testem/blob/0bd05cec7f83bfe9f2e10ddfab54556d724ee179/README.md#running-browser-code-after-tests-complete [2]: testem/testem#1577
1 parent 6cc13b1 commit 8c7c604

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

addon-test-support/setup-middleware-reporter.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ export function pushTestResult() {
139139
}
140140
}
141141

142+
type TestemCallback = (config: any, data: any, callback: () => void) => void;
143+
144+
declare global {
145+
interface Window {
146+
Testem?: {
147+
afterTests: (callback: TestemCallback) => void;
148+
};
149+
}
150+
}
151+
142152
/**
143153
* Sets up the middleware reporter, which reports results when the test suite is done.
144154
*/
@@ -149,15 +159,28 @@ export function setupMiddlewareReporter() {
149159

150160
QUnit.testDone(pushTestResult);
151161

152-
QUnit.done(async function () {
153-
let response = await fetch('/report-violations', {
154-
method: 'POST',
155-
headers: {
156-
'Content-Type': 'application/json',
157-
},
158-
body: JSON.stringify(TEST_SUITE_RESULTS),
162+
if (window.Testem) {
163+
window.Testem.afterTests(async function (_config, _data, callback) {
164+
try {
165+
await sendViolationsToServer();
166+
} finally {
167+
callback();
168+
}
169+
});
170+
} else {
171+
QUnit.done(async function () {
172+
await sendViolationsToServer();
159173
});
174+
}
175+
}
160176

161-
return response.json();
177+
async function sendViolationsToServer() {
178+
let response = await fetch('/report-violations', {
179+
method: 'POST',
180+
headers: {
181+
'Content-Type': 'application/json',
182+
},
183+
body: JSON.stringify(TEST_SUITE_RESULTS),
162184
});
185+
return response.json();
163186
}

0 commit comments

Comments
 (0)