@gtsopanoglou/babel-jest-boost
v0.1.22
Published
Babel plugin to boost jest performance by skipping intermediate imports
Downloads
767
Readme
Overview
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.
Usage
Step 1: Install the package
npm install -D @gtsopanoglou/babel-jest-boost
Step 2: Use the babel-jest-boost
plugin in your jest config
Method 1
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:
"transform": {
- "\\.[jt]sx?$": "babel-jest"
+ "\\.[jt]sx?$": "@gtsopanoglou/babel-jest-boost/transformer"
}
Method 2
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:
"transform": {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/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
})
Step 3: Run your tests, prevent breakages
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.
4. Re-iterate until your tests are green again.
5. Done
Once your tests are green, you are done. You can now keep running your tests as usual without having to clear your cache.
Plugin options
importIgnorePatterns
[array<string>]
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.
ignoreNodeModules
[boolean]
Set this flag to true if you want to completely ignore all node_modules imports from being re-written. Default is false.
Plugin directives
no-boost
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';
no-boost-next
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';
ROADMAP
- 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