使用Jest-Enzyme运行单元测试用例时出错

时间:2018-12-28 05:32:28

标签: reactjs react-native jestjs enzyme

我正在尝试为登录页面编写单元测试用例。由于第一行本身出现错误,因此我无法添加其他测试用例。下面是我的login.spec.tsx代码

import LoginPage from '../pages/login';
import { shallow } from 'enzyme';

describe('Login button', () => {
it('should login successfully', () => {
const button = shallow(<LoginPage />); // Error on this line
});
})

下面是我得到的错误。但是,如果我注释掉“浅”代码,那么测试用例就会通过。

E:\React Js Project\doorstop\src\__tests__\login.spec.tsx:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import LoginPage from '../pages/login';
                                                                                                ^^^^^^^^^

SyntaxError: Unexpected identifier

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

下面是我的package.json。以下是我在项目中所做的所有安装和依赖关系。

{
"name": "cliq-ravendb",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/polyfill": "^7.2.5",
"@types/node": "10.12.9",
"@types/react": "16.7.6",
"@types/react-dom": "16.0.9",
"@types/react-router-dom": "^4.3.1",
"antd": "^3.11.2",
"apollo-cache-inmemory": "^1.3.11",
"apollo-client": "^2.4.7",
"apollo-link": "^1.2.4",
"apollo-link-error": "^1.1.2",
"apollo-link-http": "^1.5.7",
"babel-jest": "^23.6.0",
"graphql": "^14.0.2",
"graphql-tag": "^2.10.0",
"node-sass": "^4.11.0",
"react": "^16.6.3",
"react-apollo": "^2.3.2",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"typescript": "3.1.6"
},
"scripts": {
"dev": "concurrently \"npm run start-frontend\" \"npm run start-server\"",
"build": "craco build && cd backend && npm run build",
"start": "node ./build/backend/server.js",
"test": "./node_modules/.bin/jest",
"start-frontend": "craco start",
"start-server": "cd backend && npm run dev",
"postinstall": "cd backend && npm install",
"eject": "react-scripts eject"
},
"jest": {
"moduleFileExtensions": [
  "ts",
  "tsx",
  "js"
],
"transform": {
  "^.+\\.(ts|tsx)$": "<rootDir>/test-preprocessor.ts"
},
"transformIgnorePatterns": [
  "/node_modules"
],
"testMatch": [
  "**/__tests__/*.(ts|tsx|js)"
]
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"@craco/craco": "^3.2.2",
"@types/enzyme": "^3.1.15",
"@types/enzyme-adapter-react-16": "^1.0.3",
"@types/jest": "^23.3.10",
"concurrently": "^4.1.0",
"craco-antd": "^1.9.0",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1",
"eslint": "^5.6.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-prettier": "^3.0.0",
"eslint-plugin-react": "^7.11.1",
"jest": "^23.6.0",
"jest-enzyme": "^7.0.1",
"jest-fetch-mock": "^2.1.0",
"prettier": "^1.15.2",
"react-dom": "^16.7.0",
"ts-jest": "^23.10.5"
},
"proxy": "http://localhost:5000"
}

请为我找到此问题的解决方案。谢谢。

0 个答案:

没有答案
相关问题