-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First working example of create-jest-runner
- Loading branch information
Showing
19 changed files
with
318 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
runner: require.resolve('../../runner'), | ||
testMatch: ['**/__src__/**/*.js'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// ⚔️🏃 | ||
console.log(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
runner: require.resolve('../../runner'), | ||
testMatch: ['**/__src__/**/*.js'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Works when it has failing tests 1`] = ` | ||
"FAIL integrationTests/__fixtures__/failing/__src__/file1.js | ||
Company policies require ⚔️ 🏃 in every file | ||
✕ Check for ⚔️ 🏃 | ||
Test Suites: 1 failed, 1 total | ||
Tests: 1 failed, 1 total | ||
Snapshots: 0 total | ||
Time: | ||
Ran all test suites. | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Works when it has only passing tests 1`] = ` | ||
"PASS integrationTests/__fixtures__/passing/__src__/file1.js | ||
✓ | ||
Test Suites: 1 passed, 1 total | ||
Tests: 1 passed, 1 total | ||
Snapshots: 0 total | ||
Time: | ||
Ran all test suites. | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const runJest = require('./runJest'); | ||
|
||
it('Works when it has failing tests', () => { | ||
return expect(runJest('failing')).resolves.toMatchSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const runJest = require('./runJest'); | ||
|
||
it('Works when it has only passing tests', () => { | ||
return expect(runJest('passing')).resolves.toMatchSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const execa = require('execa'); | ||
const path = require('path'); | ||
|
||
const rootDir = path.join(__dirname, '..'); | ||
|
||
const normalize = output => | ||
output | ||
.replace(/\(?\d*\.?\d+m?s\)?/g, '') | ||
.replace(/, estimated/g, '') | ||
.replace(new RegExp(rootDir, 'g'), '/mocked-path-to-jest-runner-mocha') | ||
.replace(new RegExp('.*at .*\\n', 'g'), 'mocked-stack-trace') | ||
.replace(/.*at .*\\n/g, 'mocked-stack-trace') | ||
.replace(/(mocked-stack-trace)+/, ' at mocked-stack-trace') | ||
.replace(/\s+\n/g, '\n'); | ||
|
||
const runJest = (project, options = []) => { | ||
// eslint-disable-next-line | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000; | ||
return execa( | ||
'jest', | ||
[ | ||
'--useStderr', | ||
'--no-watchman', | ||
'--no-cache', | ||
'--projects', | ||
path.join(__dirname, '__fixtures__', project), | ||
].concat(options), | ||
{ | ||
env: process.env, | ||
}, | ||
) | ||
.catch(t => t) | ||
.then(({ stdout, stderr }) => `${normalize(stderr)}\n${normalize(stdout)}`); | ||
}; | ||
|
||
module.exports = runJest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const { createJestRunner } = require('../../'); | ||
module.exports = createJestRunner(require.resolve('./run')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const fs = require('fs'); | ||
const { pass, fail } = require('../../'); | ||
|
||
module.exports = ({ testPath }) => { | ||
const start = +new Date(); | ||
const contents = fs.readFileSync(testPath, 'utf8'); | ||
const end = +new Date(); | ||
|
||
if (contents.includes('⚔️🏃')) { | ||
return pass({ start, end, test: { path: testPath } }); | ||
} | ||
const errorMessage = 'Company policies require ⚔️ 🏃 in every file'; | ||
return fail({ | ||
start, | ||
end, | ||
test: { path: testPath, errorMessage, title: 'Check for ⚔️ 🏃' }, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const Worker = require('jest-worker').default; | ||
|
||
class CancelRun extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'CancelRun'; | ||
} | ||
} | ||
|
||
const createRunner = runPath => { | ||
class BaseTestRunner { | ||
constructor(globalConfig) { | ||
this._globalConfig = globalConfig; | ||
} | ||
|
||
// eslint-disable-next-line | ||
runTests(tests, watcher, onStart, onResult, onFailure, options) { | ||
const worker = new Worker(runPath, { | ||
exposedMethods: ['default'], | ||
numWorkers: this._globalConfig.maxWorkers, | ||
}); | ||
|
||
const runTestInWorker = test => { | ||
if (watcher.isInterrupted()) { | ||
throw new CancelRun(); | ||
} | ||
|
||
return onStart(test).then(() => { | ||
const baseOptions = { | ||
config: test.context.config, | ||
globalConfig: this._globalConfig, | ||
testPath: test.path, | ||
rawModuleMap: watcher.isWatchMode() | ||
? test.context.moduleMap.getRawModuleMap() | ||
: null, | ||
options, | ||
}; | ||
|
||
return worker.default(baseOptions); | ||
}); | ||
}; | ||
|
||
const onError = (err, test) => { | ||
return onFailure(test, err).then(() => { | ||
if (err.type === 'ProcessTerminatedError') { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'A worker process has quit unexpectedly! ' + | ||
'Most likely this is an initialization error.', | ||
); | ||
process.exit(1); | ||
} | ||
}); | ||
}; | ||
|
||
const onInterrupt = new Promise((_, reject) => { | ||
watcher.on('change', state => { | ||
if (state.interrupted) { | ||
reject(new CancelRun()); | ||
} | ||
}); | ||
}); | ||
|
||
const runAllTests = Promise.all( | ||
tests.map(test => | ||
runTestInWorker(test) | ||
.then(testResult => onResult(test, testResult)) | ||
.catch(error => onError(error, test)), | ||
), | ||
); | ||
|
||
const cleanup = () => worker.end(); | ||
|
||
return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup); | ||
} | ||
} | ||
|
||
return BaseTestRunner; | ||
}; | ||
|
||
module.exports = createRunner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const toTestResult = require('./toTestResult'); | ||
|
||
const fail = ({ start, end, test, errorMessage }) => | ||
toTestResult({ | ||
errorMessage: errorMessage || test.errorMessage, | ||
stats: { | ||
failures: 1, | ||
pending: 0, | ||
passes: 0, | ||
start, | ||
end, | ||
}, | ||
tests: [Object.assign({ duration: end - start }, test)], | ||
jestTestPath: test.path, | ||
}); | ||
|
||
module.exports = fail; |
Oops, something went wrong.