-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 4 commits
b0b65c7
e4b7631
b86bdb7
dd5fe3f
8028d9e
d697937
cc38145
4aee3fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,61 @@ | ||||||||
/* eslint-disable no-console */ | ||||||||
import childProcess from 'child_process'; | ||||||||
import os from 'os'; | ||||||||
|
||||||||
const testPaths = childProcess.execSync('jest --listTests', { encoding: 'utf8' }).trim().split('\n'); | ||||||||
|
||||||||
let testsSucceeded = true; | ||||||||
const testAmount = testPaths.length; | ||||||||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
const fails: string[] = []; | ||||||||
|
||||||||
const threads = os.cpus().map(async (_, i) => { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think this makes it slightly clearer that two things are really happening, thread creation and thread use.
Suggested change
|
||||||||
let testPath = testPaths.pop(); | ||||||||
while (testPath !== undefined) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had in my head to just split the tests by cpus beforehand and then sync iterate, but the producer - consumer pattern you have here is way more effective, I just didn't think it through. |
||||||||
console.log(`(Worker ${i}) Running test "${testPath}"`); | ||||||||
await new Promise(resolve => { | ||||||||
const p = childProcess.spawn('jest', ['--runTestsByPath', testPath as string, '--forceExit']); | ||||||||
|
||||||||
let output = ''; | ||||||||
|
||||||||
p.stdout.on('data', (data: Buffer) => { | ||||||||
output = output + data.toString(); | ||||||||
}); | ||||||||
|
||||||||
p.stderr.on('data', (data: Buffer) => { | ||||||||
output = output + data.toString(); | ||||||||
}); | ||||||||
|
||||||||
p.on('error', error => { | ||||||||
console.log(`(Worker ${i}) Error in test "${testPath}"`, error); | ||||||||
console.log(output); | ||||||||
resolve(); | ||||||||
}); | ||||||||
|
||||||||
p.on('exit', exitcode => { | ||||||||
console.log(`(Worker ${i}) Finished test "${testPath}"`); | ||||||||
console.log(output); | ||||||||
if (exitcode !== 0) { | ||||||||
fails.push(`FAILED: "${testPath}"`); | ||||||||
testsSucceeded = false; | ||||||||
} | ||||||||
resolve(); | ||||||||
}); | ||||||||
}); | ||||||||
testPath = testPaths.pop(); | ||||||||
} | ||||||||
}); | ||||||||
|
||||||||
void Promise.all(threads).then(() => { | ||||||||
console.log('-------------------'); | ||||||||
console.log(`Successfully ran ${testAmount} tests.`); | ||||||||
if (!testsSucceeded) { | ||||||||
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); | ||||||||
} | ||||||||
}); |
There was a problem hiding this comment.
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
.Also:
TIL! 🙂