🐠 🃏 🚀
Babel Jest Boost
Faster tests, using Babel
babel-jest-boost
is a Babel plugin that makes your tests run faster by solving the problem of unecessary imports from barrel files. It does that by re-writing your import statements (only for tests) to skip intermediate re-exports, thus bypassing barrel files.
npm install -D @gtsopanoglou/babel-jest-boost
The simplest way to use this plugin is by replacing the babel-jest
transformer with the @gtsopanoglou/babel-jest-boost/transformer
in your jest config:
jest.config.js
"transform": {
- "\\.[jt]sx?$": "babel-jest"
+ "\\.[jt]sx?$": "@gtsopanoglou/babel-jest-boost/transformer"
}
You may use babel-jest-boost
as a regular babel plugin. It needs access to your jest config (moduleNameMapper
and modulePaths
in particular). To help you do that we export a jestConfig
object. Again an example from an ejected CRA:
jest.config.js
"transform": {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js",
},
config/jest/babelTransform.js
const babelJest = require('babel-jest')
+const { jestConfig } = require('@gtsopanoglou/babel-jest-boost/config')
const hasJsxRuntime = (() => {
if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
return false
}
try {
require.resolve('react/jsx-runtime')
return true
} catch (e) {
return false
}
})()
module.exports = babelJest.createTransformer({
presets: [
[
require.resolve('babel-preset-react-app'),
{
runtime: hasJsxRuntime ? 'automatic' : 'classic'
}
]
],
+ plugins: [
+ [
+ require.resolve('@gtsopanoglou/babel-jest-boost'),
+ {
+ jestConfig,
+ // babel-jest-boost plugin options
+ }
+ ]
+ ],
babelrc: false,
configFile: false
})
Since babel-jest-boost
modifies the transpiled code, you will need to clear jest's cache before each run (just for this integration phase) to ensure you see non-cached results:
jest --clearCache && jest # or whatever you testing command is
It is likely that some tests will now break. The breakage may be caused by some implicit dependency in your code that you're not aware of, or some bug within babel-jest-boost
.
Either way, you are not going to fix them right now. In order to avoid this problem you have two tools: importIgnorePatterns
plugin option and the no-boost
/no-boost-next
directives.
Once your tests are green, you are done. You can now keep running your tests as usual without having to clear your cache.
Array of strings/regexes, import paths matching these regexes will prevent babel-jest-boost
from rewritting them. For instance, assuming this tree:
.
├── lib
│ ├── lib.js # export function libFunc () {}
│ └── index.js # export * from './lib.js'
└── code.js # import { libFunc } from './lib';
In this scenario, importIgnorePatterns
will be matched against the only import statement in this tree, import { libFunc } from './lib'
, so if you wish to exclude imports to ./lib
from being re-written, you can use:
{ importIgnorePatterns: ['./lib'] }
This is intended to help you defer refactoring some barrels or modules that are causing trouble or breaking your tests when you integrate this plugin.
Set this flag to true if you want to completely ignore all node_modules imports from being re-written. Default is false.
You can ommit transforming all imports/mocks within a file by adding this comment at the top
// @babel-jest-boost no-boost
import { libFunc } from './lib';
You can ommit specific imports/mocks within a file by adding this comment right above the code to be ommited
// @babel-jest-boost no-boost-next
import { libFunc } from './lib';
- 0.1.22 Expose debugging options to the user (like printing which imports are being rewritten, or the transpiled output of a particular file).
- 0.1.23 Expose a jest reporter to print a high-level overview of what the plugin did within the run (and potientialy report barel file statistics)
- 0.1.24 Performance testing: Fork some open-source codebases, integrate
babel-jest-boost
and test to measure the performance increase. Do this in the CI/CD pipeline - 0.1.25 Figure out automatic changelog, version increase, github release, npm publish actions