在我项目中有一个包含许多有用方法的util库,我想使用jest.spyOn来测试每个方法。这是我的util.js库的一部分
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import { compose } from "recompose";
export const withRouterAndConnect = (mapStateToProps, mapDispatchToProps) =>
compose(
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
)
);
export const applyShadow = dp => {
if (dp === 0) {
() => "none";
} else {
let shadow = "0px";
const ambientY = dp;
const ambientBlur = dp === 1 ? 3 : dp * 2;
const ambientAlpha = (dp + 10 + dp / 9.38) / 100;
shadow +=
ambientY +
"px " +
ambientBlur +
"px rgba(0, 0, 0, " +
ambientAlpha +
"), 0px";
const directY = dp < 10 ? Math.floor(dp / 2) + 1 : dp - 4;
const directBlur = dp === 1 ? 3 : dp * 2;
const directAlpha = (24 - Math.round(dp / 10)) / 100;
shadow +=
directY + "px " + directBlur + "px rgba(0,0,0, " + directAlpha + ")";
shadow => shadow;
}
};这是我的applyShadow方法的index.test.js文件
import React from "react";
import { configure, shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import toJson from "enzyme-to-json";
configure({ adapter: new Adapter() });
describe("mock function testing", () => {
test("test spyOn", () => {
const mockFn = jest.spyOn("./lib/util", "applyShadow");
expect(mockFn(2)).toEqual('resultOutput');
});
});我使用create-react-app,当我键入npm rum test时,控制台中会输出错误消息
TypeError: Cannot read property '_isMockFunction' of undefined发布于 2018-08-03 11:17:18
jest.spyOn需要一个对象作为第一个参数,而给定的是./lib/util字符串。监视一个你自称的方法是没有意义的。
它可能应该像这样进行测试:
import * as util from "./lib/util";
...
expect(util.applyShadow(2)).toEqual(...);https://stackoverflow.com/questions/51664527
复制相似问题