简体   繁体   English

酵素的浅薄正在渲染儿童的孩子?

[英]Enzyme's shallow is rendering children's children?

I'm trying to set up some simple testing in Jest and Enzyme for my React App. 我正在尝试为我的React App在Jest和Enzyme中设置一些简单的测试。 I'm using shallow to render the main container for my app , however it appears that the children and all of the children below are being rendered. 我正在使用shallow渲染我的应用程序的主容器,但是似乎正在渲染子级和下面的所有子级。

  ● Test suite failed to run

    TypeError: Cannot read property 'NaN' of undefined

       7 | export function getRandomColor() {
       8 |      console.log(colors);
    >  9 |      return colorsKeys[Math.floor(Math.random() * colorsLength)];
         |                       ^
      10 | }
      11 |
      12 | export function determineColor(genotype) {

      at getRandomColor (src/utils/determineColor.js:9:19)
      at Object.<anonymous> (src/exampleState.js:10:16)
      at Object.<anonymous> (src/reducers/flowersReducer.js:1:1)
      at Object.<anonymous> (src/reducers/indexReducer.js:2:1)
      at Object.<anonymous> (src/index.js:14:1)
      at Object.<anonymous> (src/utils/determineColor.js:5:1)
      at Object.<anonymous> (src/components/NewFlowerFromPunnettButton.js:4:1)
      at Object.<anonymous> (src/components/Dashboard.js:2:1)
      at Object.<anonymous> (src/App.jsx:6:1)
      at Object.<anonymous> (test/App.test.js:4:1)

Per the Enzyme docs "Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child components." 根据酶文档, “浅呈现有助于将组件作为一个单元进行测试,并确保您的测试不会间接断言子组件的行为。”

This SO answer seems to clarify that shallow "will render all its childs and childs of the childs and so on." 这样的SO答案似乎澄清了“ shallow ”将“使其所有子项以及所有子项等等”。


    //App.test.js
    import React from "react";
    // shallow prevents rendering of sub components????
    import { shallow, mount, render } from "enzyme";
    import App from "../src/App";

    describe("<App />", () => {
        const app = shallow(<App />);
        it("Should render ", () => {
            expect(app).toMatchSnapShot();
        });
        it("Should have <Punnett/> <Dashboard/> and <FlowerTable />", () => {
            expect(app.find("<Punnett />")).toBeTruthy();
            expect(app.find("<Dashboard/>")).toBeTruthy();
            expect(app.find("<FlowerTable />")).toBeTruthy();
        });

    });


    //App.jsx
    import React, { Component, Fragment } from "react";

    import "./css/App.css";
    import Punnett from "./components/Punnett";
    import FlowerTable from "./components/FlowerTable/FlowerTable";
    import Dashboard from "./components/Dashboard";

    class App extends Component {
        render() {
            return (
                <Fragment>
                    <div className="App">
                        <Punnett />
                        <Dashboard />
                    </div>
                    <div className="App">
                        <FlowerTable display={true} />
                    </div>
                </Fragment>
            );
        }
    }

    export default App;


    // determineColor.js
    import { colors } from "../types/colors";
    const colorsKeys = Object.keys(colors);
    const colorsLength = colorsKeys.length;

    import { store } from "../index";

    export function getRandomColor() {
        console.log(colors);
        return colorsKeys[Math.floor(Math.random() * colorsLength)];
    }

I'm expecting that either shallow does not render the children, or if the intended behavior is to render all children, that the children be able to import their modules correctly. 我期望浅层不能渲染子级,或者如果预期的行为是渲染所有子级,则这些子级能够正确导入其模块。 Swapping shallow for render results in the same error. shallow交换以进行render导致相同的错误。

Reproducible by cloning and running npm run test on https://github.com/nodes777/flower-game-phaser3 通过在https://github.com/nodes777/flower-game-phaser3上克隆并运行npm run test重现

shallow does render children only, you are correct. shallow只会渲染孩子,你是对的。 root cause: seems "./components/Dashboard"; 根本原因:似乎是"./components/Dashboard"; has code inside that runs on import - some top level code that is execution not declaration. 内部包含可在import运行的代码-一些执行而不是声明的顶级代码。

You may either change Dashboard not doing that, or provide data it needs to work, or mock it explicitly in every single test that may import it directly or indirectly: 您可以更改不执行此操作的Dashboard ,或者提供它需要工作的数据,或者在可能直接或间接导入它的每个测试中显式模拟它:

App.test.js: App.test.js:

jest.mock('./components/Dashboard');

if you choose later approach you may consider automocking by creating components/Dashboard/__mocks__/index.jsx (or how is name of file with Dashboard) but beware of bug in jest that disallow you from using automock for more than one index.js regardless they are in different folders 如果您选择以后的方法,则可以考虑通过创建components/Dashboard/__mocks__/index.jsx (或仪表板中的文件名如何)来进行自动模拟,但是要小心,以免出现错误,无论您使用automock还是使用多个index.js它们在不同的文件夹中

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

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