npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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