Created
September 10, 2019 22:26
-
-
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.
This file contains hidden or 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
const {createJestRunner} = require('create-jest-runner') | |
module.exports = createJestRunner(require.resolve('./node-runner')) |
This file contains hidden or 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
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 */ |
This file contains hidden or 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
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