Jest: [Bug] babel-jest throws if `only` in babel.config.js excludes current file

Created on 31 Jan 2019  ·  11Comments  ·  Source: facebook/jest

I am upgrading jest 23.6.0 to jest 24.0.0.

They have deprecated setupTestFrameworkScriptFile , see: https://jestjs.io/docs/en/configuration#setupfilesafterenv-array

We used to have in our package.json:

"setupTestFrameworkScriptFile": "<rootDir>/internals/testing/test-bundler.js",

We have tried to update to :

"setupFilesAfterEnv": ["<rootDir>/internals/testing/test-bundler.js"],

We now have the following error in our test npm run test:

FAIL  src/Table/tests/Tbody.test.js
● Test suite failed to run

TypeError: Cannot read property 'config' of null

  at Object.getCacheKey (node_modules/babel-jest/build/index.js:127:22)

This is how our test-bundler.js:

const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
Enzyme.configure({ adapter: new Adapter() });

It fails when doing:
const babelOptions = loadBabelConfig("/home/dka/workspace/github.com/bootstrap-styled/v4", "/home/dka/workspace/github.com/bootstrap-styled/v4/internals/testing/test-bundler.js");
babelOptions is null

This is happening in babel-jest package after upgrading from 23.6.0 to 24.0.0

It seems that setupFilesAfterEnv behave differently, does anybody know how to fix it, I see many breaking change on their changlog since our two version but none says how to fix this.

reproduction

git clone [email protected]:bootstrap-styled/v4.git
cd v4
git checkout dev-jest24
npm i 
npm test

There's also a detailled issue on stackoverflow

Travis build failure: https://travis-ci.org/bootstrap-styled/v4/jobs/486846842#L614

Bug

All 11 comments

Thanks for the reproduction! This is a completely separate issue from #7438. This fails due to the babel config having only in it:

  only: [
    'src',
    'styleguide',
  ],

That makes loadPartialConfig return null (which is not documented behavior, but maybe makes sense?). You can update transformIgnorePatterns to ignore the same files while waiting for a fix.

diff --git i/package.json w/package.json
index da61831a..b03f0ea1 100644
--- i/package.json
+++ w/package.json
@@ -167,6 +167,10 @@
       "node_modules",
       "src"
     ],
+    "transformIgnorePatterns": [
+        "/node_modules/",
+        "/internals/"
+    ],
     "testRegex": "tests/.*\\.test\\.js$",
     "testResultsProcessor": "jest-sonar-reporter"
   },

@loganfsmyth does it make sense for loadPartialConfig to return null rather than just empty configuration?

I think this is a babel bug, no? If no babel config is found, this is the result:

PartialConfig {
  options:
   { filename: '/Users/simen/repos/ugh/hello.js',
     babelrc: false,
     configFile: false,
     passPerPreset: false,
     envName: 'development',
     cwd: '/Users/simen/repos/ugh',
     root: '/Users/simen/repos/ugh',
     plugins: [],
     presets: [] },
  babelignore: undefined,
  babelrc: undefined,
  config: undefined }
$ cat index.js
const { loadPartialConfig } = require("@babel/core");

const o = loadPartialConfig({ filename: require.resolve("./hello.js") });

console.log(o);
$ cat hello.js
$ node index.js
PartialConfig {
  options:
   { filename: '/Users/simen/repos/ugh/hello.js',
     babelrc: false,
     configFile: false,
     passPerPreset: false,
     envName: 'development',
     cwd: '/Users/simen/repos/ugh',
     root: '/Users/simen/repos/ugh',
     plugins: [],
     presets: [] },
  babelignore: undefined,
  babelrc: undefined,
  config: undefined }

@SimenB thanks, I have applied the temporary fix by adding into package.json under jest:

+  transformIgnorePatterns: [
+  '/node_modules/'
+  '/internals/'
+  ]

Thanks!

Still a bug, in either Jest or Babel, so I'll reopen this to keep track of it 🙂

babel.transform() returns null if a file is ignored, and loadPartialConfig does the same because we stop processing the config ASAP to avoid doing extra work for ignored files.

This also fails when the current test file is excluded with ignore in the .babelrc file.

Yeah, we need to handle Babel saying "nope". I'll put together a PR

Not sure how to handle code coverage. We currently do that by adding a babel plugin. Should we still do that even though a file is ignored by babel config?

I think I'll just throw an error saying that you need to also ignore the file in transformIgnorePatterns if babel wants to ignore the file

@SimenB Yes, a warning like that would have helped me. Thank you for the fast response :)

PR: #7797

Was this page helpful?
0 / 5 - 0 ratings