Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] babel-jest throws if only in babel.config.js excludes current file #7765

Closed
kopax opened this issue Jan 31, 2019 · 12 comments · Fixed by #7797
Closed

[Bug] babel-jest throws if only in babel.config.js excludes current file #7765

kopax opened this issue Jan 31, 2019 · 12 comments · Fixed by #7797
Labels

Comments

@kopax
Copy link

kopax commented Jan 31, 2019

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 git@github.com: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

@SimenB
Copy link
Member

SimenB commented Jan 31, 2019

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?

@SimenB
Copy link
Member

SimenB commented Jan 31, 2019

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 }

@kopax
Copy link
Author

kopax commented Jan 31, 2019

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

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

Thanks!

@kopax kopax closed this as completed Jan 31, 2019
@SimenB
Copy link
Member

SimenB commented Jan 31, 2019

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

@SimenB SimenB reopened this Jan 31, 2019
@SimenB SimenB changed the title [Bug] setupFilesAfterEnv fail to replace setupTestFrameworkScriptFile in babel-jest 24.0.0 [Bug] babel-jest throws if only in babel.config.js excludes current file Jan 31, 2019
@loganfsmyth
Copy link

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.

@leMaik
Copy link

leMaik commented Feb 4, 2019

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

@SimenB
Copy link
Member

SimenB commented Feb 4, 2019

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

@SimenB
Copy link
Member

SimenB commented Feb 4, 2019

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?

@SimenB
Copy link
Member

SimenB commented Feb 4, 2019

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

@leMaik
Copy link

leMaik commented Feb 4, 2019

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

@SimenB
Copy link
Member

SimenB commented Feb 4, 2019

PR: #7797

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants