Skip to content

Commit

Permalink
test(action): Import types statically
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kurt-von-Laven committed Mar 23, 2023
1 parent b85c43d commit 0699df9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof import("./util.js").execBashCommand>(),
}));
jest.unstable_mockModule(
"./util.js",
(): Util => ({ execBashCommand: jest.fn<typeof execBashCommand>() })
);

const cache = jest.mocked(await import("@actions/cache"));
const core = jest.mocked(await import("@actions/core"));
Expand Down
6 changes: 3 additions & 3 deletions src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof import("node:child_process").exec>(),
exec: jest.fn<typeof exec>(),
}));
jest.mock("@actions/cache");
jest.mock("@actions/core");
Expand Down Expand Up @@ -64,7 +64,7 @@ describe("Integration Test", (): void => {
});

const mockExec = (listStderr: string, otherOutput: ConsoleOutput): void => {
child_process.exec.mockImplementation(<typeof child_process.exec>((
child_process.exec.mockImplementation(<typeof exec>((
command: string,
_options: any,
callback: any
Expand Down
7 changes: 5 additions & 2 deletions src/main.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import("./docker.js")> => ({
loadDockerImages: jest.fn<typeof import("./docker.js").loadDockerImages>(),
(): Partial<Docker> => ({
loadDockerImages: jest.fn<typeof loadDockerImages>(),
})
);

Expand Down
7 changes: 5 additions & 2 deletions src/post.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import("./docker.js")> => ({
saveDockerImages: jest.fn<typeof import("./docker.js").saveDockerImages>(),
(): Partial<Docker> => ({
saveDockerImages: jest.fn<typeof saveDockerImages>(),
})
);

Expand Down
6 changes: 4 additions & 2 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof import("node:child_process").exec>(),
exec: jest.fn<typeof exec>(),
}));

jest.mock("@actions/core");
Expand All @@ -24,7 +26,7 @@ describe("Util", (): void => {
platform: NodeJS.Platform,
output: ConsoleOutput
): Promise<string> => {
child_process.exec.mockImplementationOnce(<typeof child_process.exec>((
child_process.exec.mockImplementationOnce(<typeof exec>((
_command: any,
_options: any,
callback: any
Expand Down
5 changes: 5 additions & 0 deletions types/aliases.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 0699df9

Please sign in to comment.