Skip to content

Commit

Permalink
actually fix @babel/register
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jun 10, 2021
1 parent 871b4c1 commit 94dee36
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export function createTestWorker(): GatsbyTestWorkerPool {
process.env.FORCE_TEST_DATABASE_ID = process.env.JEST_WORKER_ID
process.env.GATSBY_WORKER_POOL_WORKER = `true`

// deleting process.env.JEST_WORKER_ID is a workaround for `@babel/register` used in `./wrapper-for-tests`
// creating new Worker without explicitly defining `exposedMethods` result in requiring the module
// we need a way to recognize when require module is executed in context of process running test
// versus process(es) spawned for worker. jest-worker actually doesn't have a way to pass custom env vars
// to worker process without overriding JEST_WORKER_ID (yikes), but it does reuse current process.env
// so we temporarily unset JEST_WORKER_ID, so that module loaded in the same process as this code is executed
// doesn't use `@babel/register` and we rely on `jest-worker` auto-assigning JEST_WORKER_ID for child processes
const tmpJestWorkerId = process.env.JEST_WORKER_ID
delete process.env.JEST_WORKER_ID

const worker = new Worker(require.resolve(`./wrapper-for-tests`), {
numWorkers: 1,
forkOptions: {
Expand All @@ -20,6 +30,7 @@ export function createTestWorker(): GatsbyTestWorkerPool {
maxRetries: 1,
}) as GatsbyTestWorkerPool
delete process.env.GATSBY_WORKER_POOL_WORKER
process.env.JEST_WORKER_ID = tmpJestWorkerId

return worker
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// spawned process won't use jest config to support TS, so we need to add support ourselves
require(`@babel/register`)({
extensions: [`.js`, `.ts`],
configFile: require.resolve(`../../../../../babel.config.js`),
ignore: [/node_modules/],
})
// this IS executed in process that spawn worker to analyze exports and create functions for them on worker pool object
// so we want to use `@babel/register` ONLY inside actual worker as otherwise it does mess with jest transformation and tools like `rewire`
// see `./create-test-worker.ts` file for explanation why this env var is used.
if (process.env.JEST_WORKER_ID) {
// spawned process won't use jest config to support TS, so we need to add support ourselves
require(`@babel/register`)({
extensions: [`.js`, `.ts`],
configFile: require.resolve(`../../../../../babel.config.js`),
ignore: [/node_modules/],
})
}

module.exports = require(`./child-for-tests`)

0 comments on commit 94dee36

Please sign in to comment.