Jest: async tests fail after upgrading from 21.x to 22.x

Created on 1 Mar 2018  路  6Comments  路  Source: facebook/jest

Do you want to request a feature or report a bug?

Bug.

Problem

After upgrading Jest from 21.2.1 to 22.4.2, async tests always fail with error ReferenceError: regeneratorRuntime is not defined. Steps to reproduce:

  1. git clone https://github.com/iwinux/jest-async-bug.git
  2. yarn install && yarn jest

If I revert Jest back to 21.2.1, it runs without error.

Environment

  • OS: both macOS / Linux
  • Node v8.9.1
  • Yarn v1.3.2
  • Jest v22.4.2

Jest Config

// jest.config.js
module.exports = {
  browser: true,
  moduleDirectories: ['<rootDir>/src', '<rootDir>/node_modules'],
  testMatch: ['<rootDir>/tests/**/*.spec.js'],
  testPathIgnorePatterns: ['/node_modules/'],
  transform: {
    '^.+\\.jsx?$': '<rootDir>/babel-transform.js',
  },
  verbose: true,
}
// babel-transform.js
const fs = require('fs')
const path = require('path')

const { createTransformer } = require('babel-jest')

function makeConfig() {
  const filePath = path.resolve(__dirname, './.babelrc')
  const rawContent = fs.readFileSync(filePath, { encoding: 'utf-8' })
  const config = JSON.parse(rawContent)
  config.presets[0][1].modules = 'commonjs'
  return config
}

module.exports = createTransformer(makeConfig())
// .babelrc
{
  "presets": [
    ["env", {
      "debug": false,
      "modules": false,
      "targets": {
        "browsers": [
          "last 2 versions",
          "Chrome >= 45",
          "Firefox >= 50",
          "IE >= 11",
          "Safari >= 10"
        ]
      },
      "useBuiltIns": true
    }]
  ],
  "comments": true,
  "plugins": [
    "syntax-dynamic-import",
    "transform-class-properties",
    "transform-object-rest-spread"
  ]
}

Most helpful comment

OK, adding setupFiles: ['<rootDir>/node_modules/regenerator-runtime/runtime'] to jest.config.js works. Thanks!

Should I close this issue now?

All 6 comments

Well that's weird, it shouldn't work at all on v21 too, because you're using custom transformer (do you need it? maybe try removing "transform" option and see if the default one suits you) and possibly not loading regenerator-runtime. You need to bootstrap it before running async tests, e.g. inside setupFiles:

"setupFiles" ["<rootDir>/node_modules/regenerator-runtime/runtime"]

@thymikee hmmm...so this is actually an incorrect use of custom transformer? It is necessary mainly because I want to set modules: 'commonjs for babel-preset-env when running Jest.

It's not incorrect, not everybody use generators. But if you do, the current Babel flow is to include regeneratorRuntime in the global scope (e.g. by running require('regenerator-runtime/runtime') or importing through setupFiles as I showed.

You can set "test" env in babel and override options there:

"env": {
  "test": {...},
   "development": {...}
   "production": {...}
}

However, according to https://github.com/facebook/jest/blob/48560bf52fa9d587f302d4d8512e7f7c8038f8c5/packages/jest-config/src/normalize.js#L578

setupFiles does't seem to be affected by custom transformer.

Because we only set this, when default babel-jest is used

OK, adding setupFiles: ['<rootDir>/node_modules/regenerator-runtime/runtime'] to jest.config.js works. Thanks!

Should I close this issue now?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antho2407 picture Antho2407  路  3Comments

jardakotesovec picture jardakotesovec  路  3Comments

hramos picture hramos  路  3Comments

withinboredom picture withinboredom  路  3Comments

StephanBijzitter picture StephanBijzitter  路  3Comments