Формат datetime (класса) из импортированного модуля не работает - PullRequest
0 голосов
/ 19 февраля 2020

Я работаю над модульным тестом для проекта nodejs.

Здесь я хотел отформатировать дату и время, используя импортированный модуль (timeUtility. js Time Class), однако я не смог проверка времени ввода ...

Есть ли способ заставить эту работу?

Сообщение об ошибке:

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

testcode.spe c . js

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