酶浅潜水失败,连接成分

时间:2018-03-16 14:45:45

标签: reactjs enzyme jest create-react-app

我尝试测试我的连接组件。运行此测试:

import React from 'react';
import { shallow } from 'enzyme';
import Dashboard from './Dashboard';

describe('Dashboard', () => {
  test('should render correctly according to the snapshot', () => {
    const wrapper = shallow(<Dashboard />).dive();
    expect(wrapper).toMatchSnapshot();
  });
});

根据this评论,这应该有效但失败了:

node_modules/subscriptions-transport-ws/node_modules/iterall/index.mjs:84
    export var $$iterator = SYMBOL_ITERATOR || '@@iterator'
    ^^^^^^

SyntaxError: Unexpected token export

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
  at Object.<anonymous> (node_modules/subscriptions-transport-ws/dist/utils/empty-iterable.js:3:17)
  at Object.<anonymous> (node_modules/subscriptions-transport-ws/dist/server.js:8:24)

信息中心与react-redux的{​​{1}} HOC:

相关联
connnect

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

在测试使用redux的组件时,您需要先模拟商店并在测试之前将其提供给组件。您可以使用&#34; redux-mock-store&#34; npm包这样做。

例如:

import React from 'react';
import { shallow } from 'enzyme';
import Dashboard from './Dashboard';
import configureStore from 'redux-mock-store'

describe('Dashboard', () => {
  initialState = { apollo: { apolloLink: 'test value goes here' } }
  let store, wrapper;

  beforeEach(() => {
    store = mockStore(initialState)
    wrapper = shallow(<Dashboard store={store} />).dive()
  })

  it("Should render correctly according to snapshot", () => {
    let tree = wrapper.toJSON();
    expect(tree).toMatchSnapshot();
  });
});