From 0699df936488c8d218581574f0bb27f455b14bd3 Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Sun, 19 Mar 2023 01:49:21 -0700 Subject: [PATCH] test(action): Import types statically Prefer static type imports (using the import keyword) to dynamic type imports (using the import() function). Unlike regular static imports, static type imports do not interfere with mocking as they compile out, so there is no need to delay them until after our mocks are set up. Introduce types/aliases.ts to export convenient names for the shapes of our production modules that we import in our tests. We use these types to ensure that our mocks accurately reflect the shapes of their production counterparts. --- src/docker.test.ts | 10 +++++++--- src/integration.test.ts | 6 +++--- src/main.test.ts | 7 +++++-- src/post.test.ts | 7 +++++-- src/util.test.ts | 6 ++++-- types/aliases.ts | 5 +++++ 6 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 types/aliases.ts diff --git a/src/docker.test.ts b/src/docker.test.ts index ee0df5ce..24dbd783 100644 --- a/src/docker.test.ts +++ b/src/docker.test.ts @@ -5,12 +5,16 @@ import { boolean, string, uniqueArray } from "fast-check"; import type { InputOptions } from "@actions/core"; import type { FunctionLike } from "jest-mock"; +import type { Util } from "../types/aliases.js"; +import type { execBashCommand } from "./util.js"; + jest.mock("@actions/cache"); jest.mock("@actions/core"); -jest.unstable_mockModule("./util.js", (): typeof import("./util.js") => ({ - execBashCommand: jest.fn(), -})); +jest.unstable_mockModule( + "./util.js", + (): Util => ({ execBashCommand: jest.fn() }) +); const cache = jest.mocked(await import("@actions/cache")); const core = jest.mocked(await import("@actions/core")); diff --git a/src/integration.test.ts b/src/integration.test.ts index a42ab773..3ace7166 100644 --- a/src/integration.test.ts +++ b/src/integration.test.ts @@ -4,13 +4,13 @@ import { string } from "fast-check"; import { consoleOutput } from "./arbitraries/util.js"; -import type { ExecOptions } from "node:child_process"; +import type { exec, ExecOptions } from "node:child_process"; import type { InputOptions } from "@actions/core"; import type { ConsoleOutput } from "./util.js"; jest.unstable_mockModule("node:child_process", () => ({ - exec: jest.fn(), + exec: jest.fn(), })); jest.mock("@actions/cache"); jest.mock("@actions/core"); @@ -64,7 +64,7 @@ describe("Integration Test", (): void => { }); const mockExec = (listStderr: string, otherOutput: ConsoleOutput): void => { - child_process.exec.mockImplementation((( + child_process.exec.mockImplementation((( command: string, _options: any, callback: any diff --git a/src/main.test.ts b/src/main.test.ts index 1cd2fecd..ec073516 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -1,9 +1,12 @@ import { jest } from "@jest/globals"; +import type { loadDockerImages } from "./docker.js"; +import type { Docker } from "../types/aliases.js"; + jest.unstable_mockModule( "./docker.js", - (): Partial => ({ - loadDockerImages: jest.fn(), + (): Partial => ({ + loadDockerImages: jest.fn(), }) ); diff --git a/src/post.test.ts b/src/post.test.ts index 71603e5c..6fd80595 100644 --- a/src/post.test.ts +++ b/src/post.test.ts @@ -1,9 +1,12 @@ import { jest } from "@jest/globals"; +import type { saveDockerImages } from "./docker.js"; +import type { Docker } from "../types/aliases.js"; + jest.unstable_mockModule( "./docker.js", - (): Partial => ({ - saveDockerImages: jest.fn(), + (): Partial => ({ + saveDockerImages: jest.fn(), }) ); diff --git a/src/util.test.ts b/src/util.test.ts index 6d89550f..85edcf9a 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -4,10 +4,12 @@ import { string } from "fast-check"; import { consoleOutput, platform } from "./arbitraries/util.js"; +import type { exec } from "node:child_process"; + import type { ConsoleOutput } from "./util.js"; jest.unstable_mockModule("node:child_process", () => ({ - exec: jest.fn(), + exec: jest.fn(), })); jest.mock("@actions/core"); @@ -24,7 +26,7 @@ describe("Util", (): void => { platform: NodeJS.Platform, output: ConsoleOutput ): Promise => { - child_process.exec.mockImplementationOnce((( + child_process.exec.mockImplementationOnce((( _command: any, _options: any, callback: any diff --git a/types/aliases.ts b/types/aliases.ts new file mode 100644 index 00000000..b11f34f4 --- /dev/null +++ b/types/aliases.ts @@ -0,0 +1,5 @@ +import type * as docker from "../src/docker.js"; +import type * as util from "../src/util.js"; + +export type Docker = typeof docker; +export type Util = typeof util;