首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Jest模仿dayjs

用Jest模仿dayjs
EN

Stack Overflow用户
提问于 2020-07-02 15:03:43
回答 1查看 3.8K关注 0票数 6

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

我似乎找不到如何设置我的jest环境来将其识别为非缺省导出。

这是我对失败的模仿的简单测试?

代码语言:javascript
复制
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 />);
  });
});

错误:

代码语言:javascript
复制
 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格式呈现):

代码语言:javascript
复制
 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]);
EN

回答 1

Stack Overflow用户

发布于 2021-01-01 01:54:23

MockDate在这方面做得很好,不需要激烈的模拟代码。

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

代码语言:javascript
复制
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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62690739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档