未捕获错误:无法找到模块'期待'在node_modules / mocha / mocha.js:1

时间:2017-12-07 22:31:33

标签: reactjs mocha karma-mocha

我使用create-react-app创建了一个React应用程序,我想为我的组件运行一些测试。当我运行npm test时,我收到以下错误消息:

> karma start


START:
07 12 2017 16:18:37.676:INFO [karma]: Karma v1.7.1 server started at 
http://0.0.0.0:9876/
07 12 2017 16:18:37.679:INFO [launcher]: Launching browser Chrome with 
unlimited concurrency
07 12 2017 16:18:37.690:INFO [launcher]: Starting browser Chrome
07 12 2017 16:18:38.862:INFO [Chrome 62.0.3202 (Windows 7 0.0.0)]: Connected 
on socket 9zYM4BbuqSydz
_toAAAA with id 71036615
Chrome 62.0.3202 (Windows 7 0.0.0) ERROR
Uncaught SyntaxError: Unexpected token import
at testFiles/CountDownClock.test.js:1

Finished in 0.154 secs / 0 secs @ 16:18:39 GMT-0600 (Central Standard Time)

SUMMARY:
√ 0 tests completed
Chrome 62.0.3202 (Windows 7 0.0.0) ERROR
Uncaught Error: Cannot find module 'expect'
at node_modules/mocha/mocha.js:1

x Error while running the tests! Exit code: 1

npm ERR! Test failed.  See above for more details.

这是我的package.json文件:

{
  "name": "timer",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^4.2.0",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "karma start",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "expect": "^21.2.1",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "^2.2.0",
    "karma-mocha": "^1.3.0",
    "karma-mocha-reporter": "^2.2.5",
    "karma-sourcemap-loader": "^0.3.7",
    "mocha": "^4.0.1",
    "react-addons-test-utils": "^15.6.2"
  }
}

这是我的测试文件:CountDownClock.test.js

import React from 'react';
import ReactDOM from 'react-dom';
import expect from 'expect';
import TestUtils from 'react-addons-test-utils';
import CountDownClock from 'CountDownClock';

describe('CountDownClock', () => {
    it('should exist', () => {
        expect(CountDownClock).toExist();
    });
});

我试过npm安装,它没有帮助。我发现在github中提到了几个其他链接,但他们建议运行npm install,它仍然一直给我同样的错误。一些帮助将是值得赞赏的,或者如果你可以让我沿着正确的道路向我发现这一点。

1 个答案:

答案 0 :(得分:0)

试试这个npm install expect --save。我的问题在安装时解决了。