用Jest模拟Dayjs

时间:2020-07-02 07:03:43

标签: javascript reactjs unit-testing jestjs

我是测试驱动开发的新手,我正在尝试实现简单的测试,但开玩笑总是发现我的dayjs不是一个函数。另外,我没有使用typecrcipt,所以我的问题类似于this

我似乎无法找到如何设置笑话环境以将其识别为非默认导出。

这是我的模拟测试失败的简单测试吗?:

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 />);
  });
});

错误:

 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)

失败的组件(实际上以chrome呈现):

 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]);

1 个答案:

答案 0 :(得分:1)

MockDate 非常适合这个,不需要大量的模拟代码。

https://www.npmjs.com/package/mockdate

import MockDate from 'mockdate'
import dayjs from 'dayjs'

MockDate.set('2020-01-01')
console.log(dayjs.format())

// >>> 2019-12-31T18:00:00-06:00

记住在测试后清除模拟: MockDate.reset();

对于您的情况,您可能希望使用 DayJS UTC 来避免意外的日期数学结果

https://day.js.org/docs/en/plugin/utc

相关问题