[英]Apollo react import not transpiled when running jest tests
I'm currently experimenting an issue with my new React project using React and GraphQL Apollo. 我目前正在使用React和GraphQL Apollo在我的新React项目中尝试一个问题。 Here is my stack : 这是我的堆栈:
The issue: When I run my tests, it seems that Apollo react client is not transpiled and throw tests : 问题:当我运行测试时,似乎Apollo react client没有被编译并抛出测试:
pp/containers/App/__tests__/App.test.jsx
● Test suite failed to run
/Users/utilisateur/Project/TrAVis/TrAVis/node_modules/react-apollo/graphql.js:19
import { Component, createElement } from 'react';
^^^^^^
SyntaxError: Unexpected token import
1 | import React, { Component } from 'react';
> 2 | import graphql from 'react-apollo/graphql';
3 | import { bool, object, shape } from 'prop-types';
4 |
5 | import getUserQuery from './query';
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)
at Object.<anonymous> (app/containers/App/App.jsx:2:16)
at Object.<anonymous> (app/containers/App/__tests__/App.test.jsx:4:12)
My jest configuration is : 我开玩笑的配置是:
module.exports = {
verbose: true,
moduleNameMapper: {
'\\.(css)$': 'identity-obj-proxy',
},
transform: {
'^.+\\.(js|jsx)$': 'babel-jest'
}
};
and my .babelrc is simply : 而我的.babelrc很简单:
{
"presets": [
"react",
"es2015"
]
}
I found this issue https://github.com/facebook/jest/issues/3202 but it seems that the solution doesn't work with my project. 我发现了这个问题https://github.com/facebook/jest/issues/3202,但看来该解决方案不适用于我的项目。
Can you help me? 你能帮助我吗?
Thank you, 谢谢,
SLedunois 泥edu
You need to configure jest to handle es6 imports using babel-jest. 您需要配置jest以使用babel-jest处理es6导入。
Add babel-jest to your project using the following command in your terminal. 在终端中使用以下命令将babel-jest添加到您的项目中。
npm install --save-dev jest babel-jest
In your jest.config.js file include the following under transform. 在您的jest.config.js文件中,在transform下包含以下内容。
transform: {
"^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest",
},
Also make sure you are importing react-apollo correctly. 还要确保正确导入了react-apollo。
Currect version 2.1.9 at time of writing imports using the following syntax. 使用以下语法在编写导入时将版本2.1.9修改为当前版本。
import { graphql } from 'react-apollo';
By default Jest won't transform the contents of your node_modules
folder, where your installation of Apollo resides. 默认情况下,Jest不会转换您的Apollo安装所在的node_modules
文件夹的内容。 There is a config option for Jest, called transformIgnorePatterns that provides the ability to change this behavior in order to transform some of your dependencies present in node_modules. Jest有一个配置选项,称为transformIgnorePatterns ,它提供了更改此行为的功能,以便转换node_modules中存在的某些依赖项。 In your case, you need to whitelist Apollo so that Jest transforms it before running your tests: 在您的情况下,您需要将Apollo列入白名单,以便Jest在运行测试之前对其进行转换:
"transformIgnorePatterns": ["node_modules\/(?!(react-apollo))"]
What happens here is that Jest will ignore all the contents of node_modules except react-apollo, which will get transformed with Babel. 这里发生的是,Jest将忽略node_modules的所有内容,只有react-apollo除外,后者将通过Babel进行转换。 That way you should stop seeing the error you are getting. 这样一来,您应该停止看到自己得到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.