简体   繁体   English

创建反应应用程序测试和笑话模拟

[英]Create-React-App Testing and Jest Mocking

For a project at work we decided to use a custom-built JS library that injects a <script> tag in the DOM that links to a third-party JS library. 对于正在进行的项目,我们决定使用定制的JS库,该库在DOM中注入一个<script>标记,该标记链接到第三方JS库。 I've been writing this project in React with CRA and using Airbnb Enzyme with shallow rendering to test the components. 我一直在用CRA在React中编写这个项目,并使用带有浅层渲染的Airbnb酶来测试组件。 Prior to introducing this custom-built JS library, we had no issues testing the project and getting strong coverage. 在介绍这个定制的JS库之前,我们没有测试项目和获得广泛报道的问题。 When we introduced it, all test files from the target component (where the custom-logic is introduced) up to the root started failing. 当我们引入它时,从目标组件(引入了定制逻辑)到根的所有测试文件都开始失败。 I debugged the tests and noticed that it's because those components import each other (like below) and the last one imports one of those functions from the custom library that uses the window variable and it can't find it. 我调试了测试,发现这是因为这些组件相互导入(如下所示),而最后一个组件从使用window变量的自定义库中导入了其中一个函数,但找不到它。 I suspect this is because I am using Airbnb Enzyme shallow rendering that does not render a full DOM tree. 我怀疑这是因为我使用的Airbnb酶浅层渲染无法渲染完整的DOM树。 However, in a fully fledged out application, the custom library works because there is a window . 但是,在完全成熟的应用程序中,由于存在window ,因此自定义库起作用。

嵌套的进口 You can see from the chain that Content imports ObjectCatalogContainer which imports ObjectCatalog which imports CatalogView where the newly-introduced custom library is called. 从链中可以看到, Content导入了ObjectCatalogContainer ,后者又导入了ObjectCatalog ,后者又导入了CatalogView ,在其中CatalogView了新引入的自定义库。

When I introduce this line in Content.test.tsx , after the imports, the test passes again: jest.mock('../../containers/ObjectCatalog/ObjectCatalogContainer', () => 'mock') 当我在Content.test.tsx引入这一行时,在导入之后,测试再次通过: jest.mock('../../containers/ObjectCatalog/ObjectCatalogContainer', () => 'mock')

Content.tsx

import * as React from 'react'
import { Redirect, Route, Switch } from 'react-router-dom'
import { Layout } from 'antd'
import { ObjectCatalogContainer } from '../../containers/ObjectCatalog/ObjectCatalogContainer'

export const Content = () => (
  <Layout.Content>
    <Switch>
      <Route exact={true} path="/" component={ObjectCatalogContainer} />
      <Redirect to="/" />
    </Switch>
  </Layout.Content>
)

This is my test: 这是我的测试:

it('render', () => {
  // method under test
  const wrapper = shallow(<Content />)

  // assertions
  expect(wrapper.matchesElement((
    <Layout.Content>
      <Switch>
        <Route exact={true} path="/" component={ObjectCatalogContainer} />
        <Redirect to="/" />
      </Switch>
    </Layout.Content>
  ))).toEqual(true)
})

full test failure output: 完整的测试失败输出:

  ● Test suite failed to run

    ReferenceError: window is not defined

      7 | }
      8 | export const AppTracking = new Tracking(trackingOptions)
    > 9 | 

      at node_modules/we-tracking-js/dist/src/tracking/Tracking.js:94:29
      at Tracking.Object.<anonymous>.Tracking.initializeSegment (node_modules/we-tracking-js/dist/src/tracking/Tracking.js:125:10)
      at new Tracking (node_modules/we-tracking-js/dist/src/tracking/Tracking.js:12:18)
      at Object.<anonymous> (src/services/AppTracking.ts:9:23)
      at Object.<anonymous> (src/components/ObjectCatalog/Catalog/CatalogView.tsx:10:23)

First, I wonder how to get the shallow rendering to work without fully resolving all nested components. 首先,我想知道如何在不完全解决所有嵌套组件的情况下使浅层渲染正常工作。 Second, if the nested components need to be mandatorily resolved, then is it possible to have Enzyme mock them out? 其次,如果需要强制解决嵌套的组件,那么是否可以让酶模拟掉它们? Third, is it possible to tell Jest to mock such components in automatic fashion? 第三,是否可以告诉Jest以自动方式模拟此类组件? (Remember I am using Create-React-App, so configurations are limited) (请记住,我正在使用Create-React-App,因此配置受到限制)

Why don't you use mount instead of shallow for the full DOM rendering? 你为什么不使用安装 ,而不是为全面DOM渲染?

try this: 尝试这个:

              "automock":true

For more: check here 欲了解更多, 请点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM