JEST测试,从酶导入配置时出错

时间:2019-04-28 18:08:58

标签: javascript testing jestjs enzyme

我正在尝试在以下仓库中运行我的测试:https://github.com/Futuratum/moon.holdings

但是我遇到了以下错误

  

/Users/leongaban/projects/Futuratum/moon.holdings/jest.config.js:1   (功能(导出,需求,模块,__ filename,__ dirname){import {configure} from'enzyme'

我的测试曾经可以正常工作,但我没有做任何更改,所以好奇是什么导致了这个问题?

我的 jest.config.js 文件看起来正确:

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

configure({ adapter: new Adapter() })

package.json

{
  "name": "moon.holdings",
  "version": "2.0.0",
  "description": "Moon Holdings: track your cryptocurrency portfolio.",
  "main": "index.js",
  "scripts": {
    "dev": "next -p 7777",
    "build": "next build",
    "start": "next -p 7777",
    "test": "NODE_ENV=test jest --watch --no-cache",
    "test-win": "SET NODE_ENV=test&& jest --watch"
  },
  "author": "Futuratum",
  "license": "UNLICENSED",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "@zeit/next-typescript": "^1.1.1",
    "axios": "^0.18.0",
    "decko": "^1.2.0",
    "next": "^8.0.4-canary.10",
    "next-routes": "^1.4.2",
    "node-sass": "^4.11.0",
    "ramda": "^0.26.1",
    "ramda-adjunct": "^2.17.0",
    "react": "^16.7.0",
    "react-adopt": "^0.6.0",
    "react-dom": "^16.7.0",
    "react-ga": "^2.5.7",
    "react-redux": "^6.0.0",
    "react-transition-group": "^2.5.0",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "styled-components": "4.0.3",
    "tslint": "^5.12.1",
    "tslint-react": "^3.6.0",
    "typescript": "^3.2.4",
    "waait": "^1.0.2"
  },
  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.3.0",
    "@babel/preset-typescript": "^7.1.0",
    "@types/enzyme": "^3.1.15",
    "@types/jest": "^23.3.13",
    "@types/next": "^7.0.6",
    "@types/ramda": "^0.25.49",
    "@types/react": "^16.7.20",
    "@types/react-dom": "^16.0.11",
    "@types/react-redux": "^7.0.1",
    "@types/styled-components": "4.0.3",
    "@types/zeit__next-typescript": "^0.1.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^24.1.0",
    "babel-plugin-sass-vars": "^0.2.1",
    "babel-plugin-styled-components": "^1.10.0",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "enzyme-to-json": "^3.3.4",
    "jest": "^24.1.0"
  },
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/jest.setup.js",
    "testPathIgnorePatterns": [
      "<rootDir>/.next/",
      "<rootDir>/node_modules/"
    ],
    "transform": {
      ".*": "babel-jest",
      "^.+\\.js?$": "babel-jest",
      "^.+\\.ts?$": "babel-jest",
      "^.+\\.tsx?$": "babel-jest"
    },
    "moduleFileExtensions": [
      "js",
      "json",
      "ts",
      "tsx"
    ],
    "modulePaths": [
      "<rootDir>/components/",
      "<rootDir>/pages/",
      "<rootDir>/shared/"
    ]
  }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

好像您不小心将this commit中的jest.setup.js重命名为jest.config.js

将其更改回jest.setup.js,它应该可以工作。


详细信息

jest.config.js是一个特殊文件,用于设置Jest configuration options

enzyme配置通常是在setupTestFrameworkScriptFile文件中完成的,用于Jest的较旧版本,而对于Jest的较新版本则是setupFilesAfterEnv

您的package.json已将setupTestFrameworkScriptFile设置为jest.setup.js

基于该信息,我怀疑jest.setup.js可能被意外重命名,并能够在回购历史记录中找到发生该提交的地方。