简体   繁体   中英

How can I resolve the following compilation error when running Jest for testing?

Although I am able to start the npm project using npm start without any issues with webpack or babel, once I run npm test , I find the following error related to testing App.js using App.test.js (where App.js imports ApolloClient ):

TypeError: Cannot assign to read only property '__esModule' of object '[object Object]'

| import ApolloClient from 'apollo-boost';
| ^

at node_modules/apollo-boost/lib/bundle.cjs.js:127:74
          at Array.forEach (<anonymous>)
      at Object.<anonymous> (node_modules/apollo-boost/lib/bundle.cjs.js:127:36)

Essentially, I'm confused as to why I get an error when running the test but not when starting the project.

I've tried adding in a number of babel plugins to both .babelrc and in my webpack config file:

  • @babel/plugin-transform-object-assign
  • @babel/plugin-transform-modules-commonjs
  • babel-plugin-transform-es2015-modules-commonjs

However, I haven't been able to resolve the issue. My thinking was that this is related to the fact that the file that fails to compile was originally CommonJS.

I was only able to find something relatively similar here, https://github.com/ReactTraining/react-router/pull/6758 , but I didn't find a solution.

Is there something that I'm missing specifically related to running tests? I should also mention I've tried frameworks other than Jest and ran into the same issue.

EDIT:

I removed everything from App.test.js except the imports to isolate the issue so it just contains the following:

import React from 'react';
import { shallow } from 'enzyme/build';
import App from './App';

UPDATE:

I was able to resolve the initial error by upgrading apollo-boost from version 0.3.1 to 0.4.2 . However, I now have a different error that is similarly frustrating. I am using Babel 7 and have added the plugin @babel/plugin-syntax-dynamic-import to both my .babelrc and to my webpack.config.js files. Despite this, I get the following error related to the use of a dynamic import in App.js when running the Jest to test App.test.js :

SyntaxError: Support for the experimental syntax 'dynamicImport' isn't currently enabled

Add @babel/plugin-syntax-dynamic-import (https://git.io/vb4Sv) to the 'plugins' section of your Babel config to enable parsing.

I'm not sure if there is a parsing error or something else, but I've tried numerous things that have not worked. The closest discussion I could find related to this problem is, https://github.com/facebook/jest/issues/5920 , however, the proposed solutions don't work for me.

UPDATE:

One thing that I'm trying is to avoid duplication of the babel options as right now they're both in .babelrc and in the babel-loader options within webpack.config.js . From what I found online ( Whats the difference when configuring webpack babel-loader vs configuring it within package.json? ), the way to make webpack use the settings in .babelrc is to not specify options . However, doing so results in the same error described above showing up only this time when running npm start . I will add that the project that was originally created using create-react-app , however, in order to support multiple pages, I needed to customize webpack 's configuration and so ejected from it. I'm not sure why this is so convoluted.

its probably a babel configuration issue, I'm pretty sure jest needs to be compiled to work with create-react-app... did you specify a setup file in package.json:

"jest": {
    "setupFiles": [
        "/setupTests.js"
    ]
}

and in setupTests.js:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

It turns out that one of the components in the project's src directory had its own local package.json file even though it wasn't being used and was not installed as a local dependency in the top level package.json (instead imports were done using relative urls). For some reason, the existence of this file changed the behavior of webpack and other tools when starting and testing the project such that none of the top level configurations were used for files within directories with separate package.json files. Once I removed these local package.json files from the components sub-directory, all the prior issues were resolved. One hallmark of this problem is that compilation errors were not showing up for JavaScript files that weren't nested under an alternate package.json file.

Hopefully this is useful for anyone that encounters similar errors as I don't think the cause can be directly determined from the compiler messages alone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM