Skip to content

Commit 8f19db5

Browse files
author
Luca Forstner
authored
test: Run node integration tests in isolation (#5721)
1 parent 78e47cb commit 8f19db5

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
lines changed

packages/node-integration-tests/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"fix:prettier": "prettier --write \"{suites,utils}/**/*.ts\"",
1818
"type-check": "tsc",
1919
"pretest": "run-s --silent prisma:init",
20-
"test": "jest --forceExit",
20+
"test": "ts-node ./utils/run-tests.ts",
2121
"test:watch": "yarn test --watch"
2222
},
2323
"dependencies": {
@@ -34,8 +34,7 @@
3434
"mongodb-memory-server-global": "^7.6.3",
3535
"mysql": "^2.18.1",
3636
"nock": "^13.1.0",
37-
"pg": "^8.7.3",
38-
"portfinder": "^1.0.28"
37+
"pg": "^8.7.3"
3938
},
4039
"config": {
4140
"mongodbMemoryServer": {

packages/node-integration-tests/utils/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { logger, parseSemver } from '@sentry/utils';
55
import axios, { AxiosRequestConfig } from 'axios';
66
import { Express } from 'express';
77
import * as http from 'http';
8+
import { AddressInfo } from 'net';
89
import nock from 'nock';
910
import * as path from 'path';
10-
import { getPortPromise } from 'portfinder';
1111

1212
export type TestServerConfig = {
1313
url: string;
@@ -151,11 +151,9 @@ export class TestEnv {
151151
}
152152
});
153153

154-
void getPortPromise().then(port => {
155-
const url = `http://localhost:${port}/test`;
156-
const server = app.listen(port, () => {
157-
resolve([server, url]);
158-
});
154+
const server = app.listen(0, () => {
155+
const url = `http://localhost:${(server.address() as AddressInfo).port}/test`;
156+
resolve([server, url]);
159157
});
160158
});
161159

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-disable no-console */
2+
import childProcess from 'child_process';
3+
import os from 'os';
4+
5+
// This variable will act as a job queue that is consumed by a number of worker threads. Each item represents a test to run.
6+
const testPaths = childProcess.execSync('jest --listTests', { encoding: 'utf8' }).trim().split('\n');
7+
8+
const numTests = testPaths.length;
9+
const fails: string[] = [];
10+
11+
// We're creating a worker for each CPU core.
12+
const workers = os.cpus().map(async (_, i) => {
13+
while (testPaths.length > 0) {
14+
const testPath = testPaths.pop();
15+
console.log(`(Worker ${i}) Running test "${testPath}"`);
16+
await new Promise(resolve => {
17+
const jestProcess = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit']);
18+
19+
// We're collecting the output and logging it all at once instead of inheriting stdout and stderr, so that
20+
// test outputs of the individual workers aren't interwoven, in case they print at the same time.
21+
let output = '';
22+
23+
jestProcess.stdout.on('data', (data: Buffer) => {
24+
output = output + data.toString();
25+
});
26+
27+
jestProcess.stderr.on('data', (data: Buffer) => {
28+
output = output + data.toString();
29+
});
30+
31+
jestProcess.on('error', error => {
32+
console.log(output);
33+
console.log(`(Worker ${i}) Error in test "${testPath}"`, error);
34+
fails.push(`FAILED: "${testPath}"`);
35+
resolve();
36+
});
37+
38+
jestProcess.on('exit', exitcode => {
39+
console.log(`(Worker ${i}) Finished test "${testPath}"`);
40+
console.log(output);
41+
if (exitcode !== 0) {
42+
fails.push(`FAILED: "${testPath}"`);
43+
}
44+
resolve();
45+
});
46+
});
47+
}
48+
});
49+
50+
void Promise.all(workers).then(() => {
51+
console.log('-------------------');
52+
console.log(`Successfully ran ${numTests} tests.`);
53+
if (fails.length > 0) {
54+
console.log('Not all tests succeeded:\n');
55+
fails.forEach(fail => {
56+
console.log(`● ${fail}`);
57+
});
58+
process.exit(1);
59+
} else {
60+
console.log('All tests succeeded.');
61+
process.exit(0);
62+
}
63+
});

packages/remix/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
"devDependencies": {
3636
"@remix-run/node": "^1.4.3",
37-
"@remix-run/react": "^1.4.3"
37+
"@remix-run/react": "^1.4.3",
38+
"portfinder": "^1.0.28"
3839
},
3940
"peerDependencies": {
4041
"@remix-run/node": "1.x",

0 commit comments

Comments
 (0)