https://github.com/gtsop/babel-jest-boost
🐠 🃏 🚀 - Faster tests, using Babel
https://github.com/gtsop/babel-jest-boost
babel babel-jest babel-plugin barrel-files javascript jest performance
Last synced: 3 months ago
JSON representation
🐠 🃏 🚀 - Faster tests, using Babel
- Host: GitHub
- URL: https://github.com/gtsop/babel-jest-boost
- Owner: gtsop
- License: agpl-3.0
- Created: 2024-04-27T15:47:14.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-12T18:15:30.000Z (6 months ago)
- Last Synced: 2025-02-01T16:35:58.235Z (4 months ago)
- Topics: babel, babel-jest, babel-plugin, barrel-files, javascript, jest, performance
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@gtsopanoglou/babel-jest-boost
- Size: 452 KB
- Stars: 13
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
🐠 🃏 🚀
Babel Jest Boost
Faster tests, using Babel
## Overview
`babel-jest-boost` is a Babel plugin that makes your tests run faster by solving the [problem of unecessary imports from barrel files](https://github.com/jestjs/jest/issues/11234). 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
```bash
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:
jest.config.js
```diff
"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:
jest.config.js
```javascript
"transform": {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "/config/jest/babelTransform.js",
},
```config/jest/babelTransform.js
```diff
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:
```bash
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\]**
Array of strings/regexes, import paths matching these regexes will prevent `babel-jest-boost` from rewritting them. For instance, assuming this tree:
```bash
.
├── 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:
```javascript
{ 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
```javascript
// @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
```javascript
// @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