chore(datetime): use FakeTime for testing parse (#2109)

This commit is contained in:
Kyle June 2022-04-14 04:52:58 -05:00 committed by GitHub
parent 403f226a20
commit bcec5f9d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 71 deletions

View File

@ -28,18 +28,9 @@ jobs:
with: with:
deno-version: ${{ matrix.deno }} deno-version: ${{ matrix.deno }}
- name: Install fakedate
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt install faketime
- name: Run tests canary - name: Run tests canary
run: deno task test run: deno task test
- name: Run test with the date 2021-12-31
if: matrix.os == 'ubuntu-latest'
run: deno task test:faketime
- name: Type check browser compatible modules - name: Type check browser compatible modules
shell: bash shell: bash
run: deno task test:browser run: deno task test:browser

View File

@ -1,5 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts"; import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import { FakeTime } from "../testing/time.ts";
import * as datetime from "./mod.ts"; import * as datetime from "./mod.ts";
Deno.test({ Deno.test({
@ -64,6 +65,75 @@ Deno.test({
}, },
}); });
Deno.test("[std/datetime] parse: The date is 2021-12-31", () => {
const time = new FakeTime("2021-12-31");
try {
assertEquals(
datetime.parse("01-01", "MM-dd"),
new Date(2021, 0, 1),
);
assertEquals(
datetime.parse("02-01", "MM-dd"),
new Date(2021, 1, 1),
);
assertEquals(
datetime.parse("03-01", "MM-dd"),
new Date(2021, 2, 1),
);
assertEquals(
datetime.parse("04-01", "MM-dd"),
new Date(2021, 3, 1),
);
assertEquals(
datetime.parse("05-01", "MM-dd"),
new Date(2021, 4, 1),
);
assertEquals(
datetime.parse("06-01", "MM-dd"),
new Date(2021, 5, 1),
);
assertEquals(
datetime.parse("07-01", "MM-dd"),
new Date(2021, 6, 1),
);
assertEquals(
datetime.parse("08-01", "MM-dd"),
new Date(2021, 7, 1),
);
assertEquals(
datetime.parse("09-01", "MM-dd"),
new Date(2021, 8, 1),
);
assertEquals(
datetime.parse("10-01", "MM-dd"),
new Date(2021, 9, 1),
);
assertEquals(
datetime.parse("11-01", "MM-dd"),
new Date(2021, 10, 1),
);
assertEquals(
datetime.parse("12-01", "MM-dd"),
new Date(2021, 11, 1),
);
assertEquals(
datetime.parse("01", "dd"),
new Date(2021, 11, 1),
);
assertEquals(
datetime.parse("15", "dd"),
new Date(2021, 11, 15),
);
assertEquals(
datetime.parse("31", "dd"),
new Date(2021, 11, 31),
);
} finally {
time.restore();
}
});
Deno.test({ Deno.test({
name: "[std/datetime] invalidParseDateTimeFormatThrows", name: "[std/datetime] invalidParseDateTimeFormatThrows",
fn: () => { fn: () => {

View File

@ -1,61 +0,0 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This file contains test cases which needs to be run at the specific date (2021-12-31)
import { assertEquals } from "../testing/asserts.ts";
import { parse } from "./mod.ts";
Deno.test("The date is 2021-12-31", () => {
const d = new Date();
d.setHours(0, 0, 0, 0);
assertEquals(d, new Date(2021, 11, 31));
});
Deno.test("[std/datetime] parse: The date is 2021-12-31", () => {
assertEquals(
parse("01-01", "MM-dd"),
new Date(2021, 0, 1),
);
assertEquals(
parse("02-01", "MM-dd"),
new Date(2021, 1, 1),
);
assertEquals(
parse("03-01", "MM-dd"),
new Date(2021, 2, 1),
);
assertEquals(
parse("04-01", "MM-dd"),
new Date(2021, 3, 1),
);
assertEquals(
parse("05-01", "MM-dd"),
new Date(2021, 4, 1),
);
assertEquals(
parse("06-01", "MM-dd"),
new Date(2021, 5, 1),
);
assertEquals(
parse("07-01", "MM-dd"),
new Date(2021, 6, 1),
);
assertEquals(
parse("08-01", "MM-dd"),
new Date(2021, 7, 1),
);
assertEquals(
parse("09-01", "MM-dd"),
new Date(2021, 8, 1),
);
assertEquals(
parse("10-01", "MM-dd"),
new Date(2021, 9, 1),
);
assertEquals(
parse("11-01", "MM-dd"),
new Date(2021, 10, 1),
);
assertEquals(
parse("12-01", "MM-dd"),
new Date(2021, 11, 1),
);
});

View File

@ -35,7 +35,6 @@
}, },
"tasks": { "tasks": {
"test": "deno test --doc --unstable --allow-all --coverage=./cov --ignore=node/", "test": "deno test --doc --unstable --allow-all --coverage=./cov --ignore=node/",
"test:faketime": "faketime '2021-12-31' deno test --unstable --allow-all datetime/test_2021_12_31.ts",
"test:browser": "git grep --name-only \"// This module is browser compatible.\" | grep -v \".github/workflows\" | xargs deno cache --config browser-compat.tsconfig.json", "test:browser": "git grep --name-only \"// This module is browser compatible.\" | grep -v \".github/workflows\" | xargs deno cache --config browser-compat.tsconfig.json",
"node:unit": "DENO_FUTURE_CHECK=1 deno test --unstable --allow-all node/ --ignore=node/_tools/test.ts,node/_tools/versions/", "node:unit": "DENO_FUTURE_CHECK=1 deno test --unstable --allow-all node/ --ignore=node/_tools/test.ts,node/_tools/versions/",
"node:test": "deno test --unstable --allow-all node/_tools/test.ts", "node:test": "deno test --unstable --allow-all node/_tools/test.ts",