React Enzyme测试文件错误

时间:2018-10-13 23:38:31

标签: reactjs enzyme

我有一个简单的测试

import React from 'react';
import { shallow, mount, render } from 'enzyme';
import { CancelButton } from '../ClientApp/components/CancelButton';

describe('A suite', function() {
  it('should render without throwing an error', function() {
    expect(shallow(<CancelButton />).contains(<button className="btn btn-default"></button>)).toBe(true);
  });

  it('should be selectable by class "foo"', function() {
    expect(shallow(<CancelButton />).contains(<button className="btn btn-default"></button>).toBe(true);
    expect(shallow(<CancelButton />).is('.btn')).toBe(true);
  });

  it('should mount in a full DOM', function() {
    expect(mount(<CancelButton />).find('.btn').length).toBe(1);
  });
});

运行测试时,我得到

 FAIL  ClientApp/reactTests/CancelButton.test.js
  ● Test suite failed to run

    \ClientApp\reactTests\CancelButton.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
                                                                                                    ^^^^^

    SyntaxError: Unexpected identifier

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

我将此文件称为CancelButton.test.js并将其放置在reactTests文件夹中。似乎传递给浅层函数的JSX未被识别为JSX。我在使用<<和普通的“ html:element”语法时遇到错误。我不确定在编写测试时如何解决这个问题?

0 个答案:

没有答案