Javascript 开玩笑地嘲笑dayjs
我是测试驱动开发的新手,我试图实现我的简单测试,但jest总是发现我的Javascript 开玩笑地嘲笑dayjs,javascript,reactjs,unit-testing,jestjs,Javascript,Reactjs,Unit Testing,Jestjs,我是测试驱动开发的新手,我试图实现我的简单测试,但jest总是发现我的dayjs不是一个函数。而且我没有使用typescrcipt,所以我的问题类似于 我似乎找不到如何设置jest环境以将其识别为非默认导出 下面是我的模拟失败的简单测试: import React from "react"; import { shallow } from "enzyme"; import MainDashboardApp from "../MainDashboa
dayjs
不是一个函数。而且我没有使用typescrcipt,所以我的问题类似于
我似乎找不到如何设置jest环境以将其识别为非默认导出
下面是我的模拟失败的简单测试:
import React from "react";
import { shallow } from "enzyme";
import MainDashboardApp from "../MainDashboard";
jest.mock("dayjs", () => {
return () => jest.requireActual("dayjs")("2020-01-01T00:00:00.000Z");
});
describe("Inititate main dashboard", () => {
it("renders without crashing", () => {
┊ shallow(<MainDashboardApp />);
});
});
MockDate在这方面效果很好,不需要大量的模拟代码 请记住在测试后使用以下工具清除模拟:
MockDate.reset()代码>
此外,对于您的情况,您可能希望使用DayJS UTC来避免意外的日期数学结果
当您使用console.log dayjs时,您会看到什么?为什么在代码中使用*作为dayjs?你不能从“daysjs”导入dayjs吗;因为我没有使用打字脚本。。因此,它不是作为默认值导出的,应该与MomentJS非常相似。在我看来,它似乎是作为默认值导出的:你说得对。另一方面,我没有使用typescript,所以它是默认的导出。。。愚蠢的错误,伙计!
FAIL src/app/main/apps/dashboards/main/__tests__/dashboard.test.js
● Test suite failed to run
TypeError: dayjs is not a function
9 | import { tableData } from "../helpers";
10 |
> 11 | const defaultStart = dayjs().startOf("month").format("YYYY-MM-DD");
| ^
12 | const defaultEnd = dayjs().endOf("month").format("YYYY-MM-DD");
13 |
14 | function IncidentList({
at Object.<anonymous> (src/app/main/apps/operations/incidents/IncidentList.js:11:22)
at Object.<anonymous> (src/app/main/apps/index.js:1:1)
at Object.<anonymous> (src/app/main/apps/dashboards/main/MainDashboard.js:22:1)
at Object.<anonymous> (src/app/main/apps/dashboards/main/__tests__/dashboard.test.js:3:1)
import React, { useEffect, useCallback, useState } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import * as incidents from "app/store/ducks/incident.duck";
import MaterialTable, { MTableToolbar } from "material-table";
import { useHistory } from "react-router-dom";
import * as dayjs from "dayjs";
import { DatePicker } from "@material-ui/pickers";
import { tableData } from "../helpers";
const defaultStart = dayjs().startOf("month").format("YYYY-MM-DD");
const defaultEnd = dayjs().endOf("month").format("YYYY-MM-DD");
function IncidentList({
incidentsList,
requestIncidents,
selectedLoc,
startDate,
endDate,
}) {
const history = useHistory();
const [selectedEndDate, handleEndDateChange] = useState(defaultEnd);
const [selectedStartDate, handleStartDateChange] = useState(defaultStart);
// Deps are ok because history constantly updates, and this handleclick does not need
// to be updated as well
const handleClick = useCallback((event, rowData) => {
history.push({
pathname: `/operation/incident-details/${rowData.id}`,
state: { detail: "testing" },
});
}, []);
useEffect(() => {
if (startDate && endDate) {
handleEndDateChange(endDate);
handleStartDateChange(startDate);
}
}, [startDate, endDate]);
import MockDate from 'mockdate'
import dayjs from 'dayjs'
MockDate.set('2020-01-01')
console.log(dayjs.format())
// >>> 2019-12-31T18:00:00-06:00