reactjs - 如何对momentjs进行单元测试
解决方案
我只需在我的测试文件中导入函数,然后编写将日期作为参数传递给函数的测试,然后期待从这些函数返回的值。
像这样的东西:
import {
formatDate,
startEndDate
} from "./index.js";
describe("formatDate", () => {
it("should format date properly", () => {
expect(formatDate(new Date("01/12/1992"))).toBe("Jan 12, 1992");
});
// More such tests here with different values of dates.
});
describe("startEndDate", () => {
it("should return start and end date in proper format", () => {
expect(startEndDate(new Date("01/12/1992"), new Date("12/12/1992"))).toBe(
"01/12/1992 - 12/12/1992"
);
});
// More such tests here with different values of start and end dates.
});
这是您的参考的工作 CodeSandbox 示例。
PS:忽略Exceeded timeout of 5000ms for a test.
你在结果中看到的测试。我们编写的 onces(2 测试) 已经通过。
推荐阅读
- ggplot2 - ggplot在一页中绘制多图
- javascript - JavaScript 全局执行上下文
- android - 从代码更改可绘制对象的颜色 (xamarin.android)
- c# - 将图像大小调整为内存流会提供错误的 FinalHeight 和 FinalWidth
- r - 将不同长度的列组合成一个数据框
- c# - Task.run 不适用于数据库连接
- java - CountDownLatch 可以用排他锁来实现吗?
- javascript - 保存 js 对象状态的更好方法?
- node.js - 通过 webpack 运行脚本后无法运行脚本。(错误:无法定位全局对象)
- opc-ua - 增加的通道令牌 ID 使会话无法使用:如何在 Eclipse Milo 客户端中处理它?