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.