Typescript 日期时间(类)和#x27;导入模块中的s格式不起作用
我正在为nodejs项目进行单元测试 然而,在这里,我想使用导入的模块(timeUtility.js的Time类)格式化datetime 我无法测试时间输入 有没有办法让这一切顺利进行 错误消息:Typescript 日期时间(类)和#x27;导入模块中的s格式不起作用,typescript,unit-testing,datetime,jestjs,datetime-format,Typescript,Unit Testing,Datetime,Jestjs,Datetime Format,我正在为nodejs项目进行单元测试 然而,在这里,我想使用导入的模块(timeUtility.js的Time类)格式化datetime 我无法测试时间输入 有没有办法让这一切顺利进行 错误消息: expect(received).toEqual(expected) // deep equality Expected: 2020-02-18T20:49:03.954Z Received: 2020-02-18T00:00:00.000Z 38 | let d =
expect(received).toEqual(expected) // deep equality
Expected: 2020-02-18T20:49:03.954Z
Received: 2020-02-18T00:00:00.000Z
38 | let d = new Time(2020, 2, 19, 15, 13, 0, 0);
39 | let current = new Date();
40 | expect(d).toEqual(current);
正如您所看到的,来自“d”的输入没有反映出来
这是密码
timeUtility.js
export class Time extends Date {
constructor({ hours = 0, minutes = 0, seconds = 0, milliSeconds = 0 }) {
var now = new Date()
super(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, seconds, milliSeconds)
}
get hours() {
return this.getHours()
}
get minutes() {
return this.getMinutes()
}
get seconds() {
return this.getSeconds()
}
get milliSeconds() {
return this.getMilliseconds()
}
/**
* @param {string} format keywords 'hh', 'mm', 'ss', 'SSSS' are available
* @param {boolean} padding pads with 0. default is true
*/
format(format, padding = true) {
return format.replace('hh', padding ? padWithZeros(this.hours, 2) : this.hours)
.replace('mm', padding ? padWithZeros(this.minutes, 2) : this.minutes)
.replace('ss', padding ? padWithZeros(this.seconds, 2) : this.seconds)
.replace('SSSS', padding ? padWithZeros(this.milliSeconds, 4) : this.milliSeconds)
}
}
import { padWithZeros } from "../../../../src/public/js/air/util/numberUtil";
import { convertTimeStringToNumber, Time } from "../../../../src/public/js/air/util/timeUtil";
describe("datetime format", () => {
it("should format datetime", () => {
let d = new Time(2020, 2, 19, 15, 13, 0, 0);
let current = new Date(2020, 1, 18);
expect(d).toEqual(current);
});
});
testcode.spec.js
export class Time extends Date {
constructor({ hours = 0, minutes = 0, seconds = 0, milliSeconds = 0 }) {
var now = new Date()
super(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, seconds, milliSeconds)
}
get hours() {
return this.getHours()
}
get minutes() {
return this.getMinutes()
}
get seconds() {
return this.getSeconds()
}
get milliSeconds() {
return this.getMilliseconds()
}
/**
* @param {string} format keywords 'hh', 'mm', 'ss', 'SSSS' are available
* @param {boolean} padding pads with 0. default is true
*/
format(format, padding = true) {
return format.replace('hh', padding ? padWithZeros(this.hours, 2) : this.hours)
.replace('mm', padding ? padWithZeros(this.minutes, 2) : this.minutes)
.replace('ss', padding ? padWithZeros(this.seconds, 2) : this.seconds)
.replace('SSSS', padding ? padWithZeros(this.milliSeconds, 4) : this.milliSeconds)
}
}
import { padWithZeros } from "../../../../src/public/js/air/util/numberUtil";
import { convertTimeStringToNumber, Time } from "../../../../src/public/js/air/util/timeUtil";
describe("datetime format", () => {
it("should format datetime", () => {
let d = new Time(2020, 2, 19, 15, 13, 0, 0);
let current = new Date(2020, 1, 18);
expect(d).toEqual(current);
});
});
导入模块在哪里?导入的模块是timeUtility.js中的时间类。最初我从测试文件中省略了import语句,现在我又重新添加了它。