2016-11-29 1 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 

Однако я раскомментировать строку 11 Foo-test.js:

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 this error:

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, но требуется с require в Foo-test.js. С помощью import это исправит.

Заменить этот

сопзЬ = Foo требуется ('../ Foo');

с этим

импорта Foo из '../Foo';

+0

Работает как очарование, спасибо! – SeanPlusPlus

Смежные вопросы