Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created September 10, 2019 22:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kentcdodds/695188284af8553dce65d2e05620d05f to your computer and use it in GitHub Desktop.
Save kentcdodds/695188284af8553dce65d2e05620d05f to your computer and use it in GitHub Desktop.
NodeJS file runner for Jest (create-node-runner using create-jest-runner). Use this with the runner option in jest config.
const {createJestRunner} = require('create-jest-runner')
module.exports = createJestRunner(require.resolve('./node-runner'))
const {formatStackTrace} = require('jest-message-util')
const Worker = require('jest-worker').default
const {fail, pass} = require('create-jest-runner')
module.exports = async ({testPath, config}) => {
const worker = new Worker(require.resolve('./require-module'))
worker.getStdout().pipe(process.stdout)
worker.getStderr().pipe(process.stderr)
process.stdout.write('\n')
const start = +new Date()
try {
await worker.require(testPath)
return pass({
start,
end: +new Date(),
test: {path: testPath},
})
} catch (error) {
return fail({
start,
end: +new Date(),
test: {
path: testPath,
errorMessage: formatStackTrace(
error.stack
.split('\n')
// we don't want any of these files to be included in the stack trace
.filter(
line =>
!line.includes('create-node-runner') &&
!line.includes('node_modules'),
)
.join('\n'),
config,
{noStackTrace: false},
testPath,
),
title: 'Error Running File',
},
})
}
}
/* eslint import/no-extraneous-dependencies:0 */
module.exports.require = modPath => {
require('@babel/register')
require(modPath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment