Shallow rendering a component which depends on Twe

2019-07-12 18:19发布

I'm trying to make a simple unit test with a React component but I keep getting:

C:\work\portfolio\node_modules\gsap\TweenMax.js:13
    import TweenLite, { TweenPlugin, Ease, Power0, Power1, Power2, Power3, Power4, Linear } from "./TweenLite.js";
           ^^^^^^^^^

Which is an error with an import of one of children's of 'App' components 3rd party libraries.

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

fit("renders without crashing", () => {
  const wrapper = shallow(<App />);
});

app.js

import React from "react";
import "./App.css";
import ChronologyGraph from "./chronology/ChronologyGraph";
import { nodeTypes, milestones } from "../staticData";

const App = () => (
  <ChronologyGraph
    width="700"
    height="800"
    nodeSize={10}
    milestones={milestones.reverse()}
    columns={nodeTypes}
  />
);

export default App;

package.json:

{
  "name": "portfolio",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "font-awesome": "^4.7.0",
    "gsap": "^2.0.1",
    "moment": "^2.22.2",
    "prop-types": "^15.6.2",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-fontawesome": "^1.6.1",
    "react-scripts": "^1.1.5",
    "react-transition-group": "^2.4.0",
    "typeface-lato": "0.0.54",
    "typeface-roboto": "0.0.54",
    "uuid": "^3.3.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "lint": "eslint src",
    "test": "react-scripts test --env=jsdom",
    "testCov": "react-scripts test --env=jsdom --coverage",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "enzyme": "^3.4.4",
    "enzyme-adapter-react-16": "^1.2.0",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^17.0.0",
    "eslint-plugin-import": "^2.12.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.9.1",
    "prettier-eslint": "^8.8.2"
  }
}

I couldn't find any similar examples online, am I supposed to somehow mock the import of a child? I thought 'shallow' render wouldn't import children and thus children's imports

2条回答
forever°为你锁心
2楼-- · 2019-07-12 18:43

Following @ljharb 3rd option:

If you read Jest documentation you can simply mock GSAP creating a file in __mocks__ directory.

Let's say you are importing TweenMax and you want to use to method:

import { TweenMax } from "gsap/TweenMax";

Add two files into the mocks directory. TweenLite can be empty.

.
└── __mocks__
    └── gsap
        └── TweenLite.js 
        └── TweenMax.js 
module.exports = {
  TweenMax: class{
    static to(selector, time, options) {
      return jest.fn();
    }
  }
}

You've successfully mock your TweenMax.to method.

Timelines are different

Because timeline works on instances of a class the mock should be done this way:

import { TimelineMax } from "gsap/TimelineMax";

Again, add two files into the mocks directory. TweenLite can be empty.

.
└── __mocks__
    └── gsap
        └── TweenLite.js 
        └── TimelineMax.js 
module.exports = {
  TimelineMax: class {
    constructor(){
      this.to = jest.fn().mockReturnThis();
    }
  }
};

Use mockReturnThis() to be able to chain methods.

查看更多
小情绪 Triste *
3楼-- · 2019-07-12 18:48

(enzyme maintainer here)

Third party modules should be transpiled prepublish - since it's not safe to run babel on node_modules, and since node doesn't support import. You have basically these options:

  1. file an issue on gsap so they properly transpile prepublish
  2. configure babel to exclude node_modules (the default) but to include this module
  3. configure jest to mock out gsap with something else
查看更多
登录 后发表回答