Skip to content

test: Run node integration tests in isolation #5721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"fix:prettier": "prettier --write \"{suites,utils}/**/*.ts\"",
"type-check": "tsc",
"pretest": "run-s --silent prisma:init",
"test": "jest --forceExit",
"test": "ts-node ./utils/run-tests.ts",
"test:watch": "yarn test --watch"
},
"dependencies": {
Expand All @@ -34,8 +34,7 @@
"mongodb-memory-server-global": "^7.6.3",
"mysql": "^2.18.1",
"nock": "^13.1.0",
"pg": "^8.7.3",
"portfinder": "^1.0.28"
"pg": "^8.7.3"
},
"config": {
"mongodbMemoryServer": {
Expand Down
10 changes: 4 additions & 6 deletions packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { logger, parseSemver } from '@sentry/utils';
import axios, { AxiosRequestConfig } from 'axios';
import { Express } from 'express';
import * as http from 'http';
import { AddressInfo } from 'net';
import nock from 'nock';
import * as path from 'path';
import { getPortPromise } from 'portfinder';

export type TestServerConfig = {
url: string;
Expand Down Expand Up @@ -151,11 +151,9 @@ export class TestEnv {
}
});

void getPortPromise().then(port => {
const url = `http://localhost:${port}/test`;
const server = app.listen(port, () => {
resolve([server, url]);
});
const server = app.listen(0, () => {
const url = `http://localhost:${(server.address() as AddressInfo).port}/test`;
resolve([server, url]);
});
});

Expand Down
63 changes: 63 additions & 0 deletions packages/node-integration-tests/utils/run-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable no-console */
import childProcess from 'child_process';
import os from 'os';

// This variable will act as a job queue that is consumed by a number of worker threads. Each item represents a test to run.
const testPaths = childProcess.execSync('jest --listTests', { encoding: 'utf8' }).trim().split('\n');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having an explanation here is both helpful in its own right and possibly a way to forestall other folks asking the same question Abhi asked about foreEach.

Suggested change
const testPaths = childProcess.execSync('jest --listTests', { encoding: 'utf8' }).trim().split('\n');
// This will serve as a pool of remaining tests from which the threads spawned below can draw
const testPaths = childProcess.execSync('jest --listTests', { encoding: 'utf8' }).trim().split('\n');

Also:

jest --listTests

TIL! 🙂


const numTests = testPaths.length;
const fails: string[] = [];

// We're creating a worker for each CPU core.
const workers = os.cpus().map(async (_, i) => {
while (testPaths.length > 0) {
const testPath = testPaths.pop();
console.log(`(Worker ${i}) Running test "${testPath}"`);
await new Promise(resolve => {
const jestProcess = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit']);

// We're collecting the output and logging it all at once instead of inheriting stdout and stderr, so that
// test outputs of the individual workers aren't interwoven, in case they print at the same time.
let output = '';

jestProcess.stdout.on('data', (data: Buffer) => {
output = output + data.toString();
});

jestProcess.stderr.on('data', (data: Buffer) => {
output = output + data.toString();
});
Comment on lines +21 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you doing it this way (rather than just setting stdio to inherit in your spawn options) so that the output from each test is printed in a single block, rather than being interleaved with output from the other running tests? If so, a) good thinking!, and b) I think a comment saying so would be helpful.


jestProcess.on('error', error => {
console.log(output);
console.log(`(Worker ${i}) Error in test "${testPath}"`, error);
fails.push(`FAILED: "${testPath}"`);
resolve();
});

jestProcess.on('exit', exitcode => {
console.log(`(Worker ${i}) Finished test "${testPath}"`);
console.log(output);
if (exitcode !== 0) {
fails.push(`FAILED: "${testPath}"`);
}
resolve();
});
});
}
});

void Promise.all(workers).then(() => {
console.log('-------------------');
console.log(`Successfully ran ${numTests} tests.`);
if (fails.length > 0) {
console.log('Not all tests succeeded:\n');
fails.forEach(fail => {
console.log(`● ${fail}`);
});
process.exit(1);
} else {
console.log('All tests succeeded.');
process.exit(0);
}
});
3 changes: 2 additions & 1 deletion packages/remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"devDependencies": {
"@remix-run/node": "^1.4.3",
"@remix-run/react": "^1.4.3"
"@remix-run/react": "^1.4.3",
"portfinder": "^1.0.28"
},
"peerDependencies": {
"@remix-run/node": "1.x",
Expand Down