浅渲染依赖于 TweenLite 的组件
Shallow rendering a component which depends on TweenLite
我正在尝试使用 React 组件进行简单的单元测试,但我不断收到:
C:\work\portfolio\node_modules\gsap\TweenMax.js:13
import TweenLite, { TweenPlugin, Ease, Power0, Power1, Power2, Power3, Power4, Linear } from "./TweenLite.js";
^^^^^^^^^
导入 children 的 'App' 组件第 3 方库之一时出错。
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"
}
}
我在网上找不到任何类似的例子,我是否应该以某种方式模拟 child 的导入?我认为 'shallow' 渲染不会导入 children,因此 children 的导入
(此处为酶维护者)
第三方模块应该在发布前转译 - 因为 运行 babel 在 node_modules 上不安全,而且节点不支持 import
。你基本上有这些选择:
- 在
gsap
上提交问题,以便他们正确转译预发布
- 配置 babel 以排除 node_modules(默认)但包含 这个模块
- 配置 jest 以用其他东西模拟
gsap
关注@ljharb 第三个选项:
如果您阅读 Jest documentation,您可以简单地模拟 GSAP 在 __mocks__
目录中创建一个文件。
假设您正在导入 TweenMax 并且您想要使用 to
方法:
import { TweenMax } from "gsap/TweenMax";
将两个文件添加到 mocks 目录中。 TweenLite 可以为空。
.
└── __mocks__
└── gsap
└── TweenLite.js
└── TweenMax.js
module.exports = {
TweenMax: class{
static to(selector, time, options) {
return jest.fn();
}
}
}
您已成功模拟您的 TweenMax.to
方法。
时间线不同
因为时间轴适用于 class 的实例,所以模拟应该以这种方式完成:
import { TimelineMax } from "gsap/TimelineMax";
再次,将两个文件添加到 mocks 目录中。 TweenLite 可以为空。
.
└── __mocks__
└── gsap
└── TweenLite.js
└── TimelineMax.js
module.exports = {
TimelineMax: class {
constructor(){
this.to = jest.fn().mockReturnThis();
}
}
};
使用 mockReturnThis()
可以链接方法。
我正在尝试使用 React 组件进行简单的单元测试,但我不断收到:
C:\work\portfolio\node_modules\gsap\TweenMax.js:13
import TweenLite, { TweenPlugin, Ease, Power0, Power1, Power2, Power3, Power4, Linear } from "./TweenLite.js";
^^^^^^^^^
导入 children 的 'App' 组件第 3 方库之一时出错。
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"
}
}
我在网上找不到任何类似的例子,我是否应该以某种方式模拟 child 的导入?我认为 'shallow' 渲染不会导入 children,因此 children 的导入
(此处为酶维护者)
第三方模块应该在发布前转译 - 因为 运行 babel 在 node_modules 上不安全,而且节点不支持 import
。你基本上有这些选择:
- 在
gsap
上提交问题,以便他们正确转译预发布 - 配置 babel 以排除 node_modules(默认)但包含 这个模块
- 配置 jest 以用其他东西模拟
gsap
关注@ljharb 第三个选项:
如果您阅读 Jest documentation,您可以简单地模拟 GSAP 在 __mocks__
目录中创建一个文件。
假设您正在导入 TweenMax 并且您想要使用 to
方法:
import { TweenMax } from "gsap/TweenMax";
将两个文件添加到 mocks 目录中。 TweenLite 可以为空。
.
└── __mocks__
└── gsap
└── TweenLite.js
└── TweenMax.js
module.exports = {
TweenMax: class{
static to(selector, time, options) {
return jest.fn();
}
}
}
您已成功模拟您的 TweenMax.to
方法。
时间线不同
因为时间轴适用于 class 的实例,所以模拟应该以这种方式完成:
import { TimelineMax } from "gsap/TimelineMax";
再次,将两个文件添加到 mocks 目录中。 TweenLite 可以为空。
.
└── __mocks__
└── gsap
└── TweenLite.js
└── TimelineMax.js
module.exports = {
TimelineMax: class {
constructor(){
this.to = jest.fn().mockReturnThis();
}
}
};
使用 mockReturnThis()
可以链接方法。