Skip to content

Commit

Permalink
refactor: revert back to the old import interface, update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsop-d committed Oct 30, 2024
1 parent 81f35d9 commit 3f87c41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 61 deletions.
66 changes: 6 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
<br/>
<p align="center">Babel Jest Boost</p>
</h1>
<p align="center">Brings tree-shaking* to Jest, speeding up your test runs, using babel-jest</p>
<p align="center">Faster tests, using Babel</p>

## Overview

`babel-jest-boost` makes your tests run faster by solving the [problem of unecessary imports from barrel files](https://github.com/jestjs/jest/issues/11234)

It is a Babel plugin that re-writes your import statements to skip intermediate re-exports, thus bypassing barrel files.
`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

Expand All @@ -19,78 +17,26 @@ It is a Babel plugin that re-writes your import statements to skip intermediate
npm install -D @gtsopanoglou/babel-jest-boost
```

### Step 2: Use the `babel-jest-boost` transformer in your jest config
### 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` in your jest config:
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:

<details>
<summary>jest.config.js</summary>

```diff
"transform": {
- "\\.[jt]sx?$": "babel-jest"
+ "\\.[jt]sx?$": "@gtsopanoglou/babel-jest-boost"
+ "\\.[jt]sx?$": "@gtsopanoglou/babel-jest-boost/transformer"
}
```
</details>



#### Method 2

If you are using a script file as your transformer, you can swap the `babel-jest` transformer factory for `gtsopanoglou/babel-jest-boost` in your script. Example from ejected CRA:

<details>
<summary>jest.config.js</summary>

```javascript
"transform": {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js",
},
```
</details>

<details>
<summary>config/jest/babelTransform.js</summary>

```diff
-const babelJest = require('babel-jest')
+const babelJest = require('@gtsopanoglou/babel-jest-boost')

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'
}
]
],
babelrc: false,
configFile: false
+}, {
+ // babel-jest-boost plugin options
})
```
</details>

#### Method 3

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:

<details>
Expand Down Expand Up @@ -134,7 +80,7 @@ module.exports = babelJest.createTransformer({
],
+ plugins: [
+ [
+ require.resolve('@gtsopanoglou/babel-jest-boost/plugin'),
+ require.resolve('@gtsopanoglou/babel-jest-boost'),
+ {
+ jestConfig,
+ // babel-jest-boost plugin options
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gtsopanoglou/babel-jest-boost",
"version": "0.1.19",
"version": "0.1.20",
"description": "Babel plugin to boost jest performance by skipping intermediate imports",
"scripts": {
"format:check": "prettier . --check",
Expand Down

0 comments on commit 3f87c41

Please sign in to comment.