Skip to content

Commit

Permalink
Tests: Avoid instrumenting modules used by testing infrastructure [fix]
Browse files Browse the repository at this point in the history
Fixes #474.
  • Loading branch information
overlookmotel committed Sep 3, 2023
1 parent f43f8e6 commit c596596
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module.exports = {
'<rootDir>/test/support/register.js'
],
testSequencer: '<rootDir>/test/support/sequencer.js',
// Disable babel-jest transformation
transform: {},
// Jest by default uses a number of workers equal to number of CPU cores minus 1.
// Github Actions runners provide 2 cores and running with 2 workers is faster than 1.
...(process.env.CI && {maxWorkers: '100%'})
Expand Down
25 changes: 21 additions & 4 deletions test/support/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,39 @@
* Tests install require hook to intrument test files' code
* ------------------*/

/* eslint-disable import/order, import/newline-after-import */
/* eslint-disable import/order, import/no-dynamic-require, global-require */

'use strict';

// Imports
const {
useInternalModuleCache, useGlobalModuleCache, usingInternalModuleCache, Module
} = require('../../lib/shared/moduleCache.js');

// Patch filesystem for virtual fixtures files
require('./fixturesFs.js');

// Pre-load `fast-json-stable-stringify`.
// `jest-light-runner` imports `jest-snapshot` which requires `@jest/transform`.
// After `jest-light-runner`'s worker loads the test file, it runs a method on `jest-snapshot`
// which causes `@jest/transform` to lazily require `fast-json-stable-stringify`.
// Trigger this now, so `fast-json-stable-stringify` is not loaded later and unnecessarily instrumented.
// `createScriptTransformer()` when called with no arguments throws an error before it does anything.
const {createRequire} = Module,
jestLightRunnerPath = require.resolve('jest-light-runner'),
jestSnapshotPath = createRequire(jestLightRunnerPath).resolve('jest-snapshot'),
jestTransformPath = createRequire(jestSnapshotPath).resolve('@jest/transform');
if (Module._cache[jestTransformPath]) {
const jestTransform = require(jestTransformPath);
jestTransform.createScriptTransformer().catch(() => {});
}

// Install register require hook.
// Disable instrumentation cache. Tests run in multiple threads, so it'd be read from and written to
// concurrently unless disabled.
require('../../register.js')({cache: false});

// Use internal module cache to avoid transpiling modules
const {
useInternalModuleCache, useGlobalModuleCache, usingInternalModuleCache
} = require('../../lib/shared/moduleCache.js');
useInternalModuleCache();

// Modules
Expand Down

0 comments on commit c596596

Please sign in to comment.