使用jest酶返回单元测试返回SyntaxError:意外的令牌

时间:2017-10-24 21:35:07

标签: node.js unit-testing enzyme jest

我正在尝试对React进行单元测试。我正在使用开玩笑和酶。

这是我的.babelrc

{
    "presets": ["es2015", "react", "airbnb"]
}

我的测试文件:

import React from 'react';
import { shallow, mount, render } from 'enzyme';

import Home from './Home/Home.js';

describe('Home', function() {
    it('should render without throwing an error', function() {
        expect(shallow(<Home />).contains(<div className="columns is-mobile"></div>)).toBe(true);
    });
});

当我运行npm test

我收到以下错误:

FAIL  client\components\Home.test.js
  ● Test suite failed to run

    C:\Users\KeOt777\Kevin Ruiz - Front End Software Engineer\video_portal_api-master\client\components\Home\Home.css:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.columns {
                                                                                             ^

    SyntaxError: Unexpected token .

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
      at Object.<anonymous> (client/components/Home/Home.js:4:1)
      at Object.<anonymous> (client/components/Home.test.js:4:13)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.373s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

任何人都可以帮我看看为什么我的测试无法运行?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用ES6代理。使用

通过npm安装
<html>
<title> MY FORM </title>
<body>
<form method="POST" action="{{ url_for('form') }}">
  {{form.csrf_token}}
  {{ form.username.label }}
  {{ form.username }}
  <ul>
  {% for error in form.username.errors %}
    <li style="color: red;">{{ error }} </li>

  {% endfor %}
  </ul>
  {{ form.password }}
  {{ form.password.label }}
  <input type="submit" value="Submit">

</form>
</body>
</html>

然后在你的jest配置文件中,将css的模块映射器更改为类似这样的

npm install --save-dev identity-obj-proxy

here中查看更多内容。