2016-11-29 70 views
0

使用enzyme-example-jest Github project我能克隆回购和运行测试:如何测试一个阵营组件用玩笑和酶

git clone https://github.com/lelandrichardson/enzyme-example-jest.git 
cd enzyme-example-jest 
yarn install 
yarn test 

不过,我注释掉的Foo-test.js线11:

expect(shallow(<Foo />).contains(<div className="foo" />)).toBe(true); 

,以便该文件现在看起来是这样:

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

jest.dontMock('../Foo'); 

const Foo = require('../Foo'); 

describe("A suite", function() { 
    it("contains spec with an expectation", function() { 
    expect(true).toBe(true); 
    expect(shallow(<Foo />).contains(<div className="foo" />)).toBe(true); 
    }); 

    it("contains spec with an expectation", function() { 
    //expect(shallow(<Foo />).is('.foo')).toBe(true); 
    }); 

    it("contains spec with an expectation", function() { 
    //expect(mount(<Foo />).find('.foo').length).toBe(1); 
    }); 
}); 

当我运行测试我现在摹et此错误:

yarn test v0.17.8 
$ jest 
Using Jest CLI v0.8.2, jasmine1 
Running 1 test suite...Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). 
FAIL src/__tests__/Foo-test.js (0.931s) 
● A suite › it contains spec with an expectation 
    - TypeError: Component is not a function 
     at StatelessComponent.render (node_modules/react/lib/ReactCompositeComponent.js:44:10) 
1 test failed, 2 tests passed (3 total in 1 test suite, run time 1.281s) 
error Command failed with exit code 1. 

如何成功运行此测试?

回答

1

'Foo'使用ES6类语法定义,但需要requireFoo-test.js。使用import将解决它。

取代这个

const Foo = require('../ Foo');

与此

从'../Foo'导入Foo;

+0

工程就像一个魅力,谢谢! – SeanPlusPlus