Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion __tests__/config/jest.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ const baseConfig: Config = {
setupFilesAfterEnv: ["<rootDir>/__tests__/jest.setup.ts"],
preset: "ts-jest",
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/$1",
"^@/components/(.*)$": "<rootDir>/components/$1",
"^@/styles/(.*)$": "<rootDir>/styles/$1",
"^@/lib/(.*)$": "<rootDir>/lib/$1",
"^@/__tests__/(.*)$": "<rootDir>/__tests__/$1",
"^@shared/(.*)$": "<rootDir>/shared/src/$1",
"^shared/(.*)$": "<rootDir>/shared/src/$1",
"^@workers/(.*)$": "<rootDir>/apps/workers/src/$1",
"^@/(adapters|entities|ports|providers|services|ui|usecases|utils)(/.*)?$":
"<rootDir>/shared/src/$1$2",
"^@/(.*)$": "<rootDir>/$1",
},
coveragePathIgnorePatterns: ["/node_modules/", "/.next/", "/coverage/"],
rootDir: "../..",
Expand Down
6 changes: 5 additions & 1 deletion __tests__/config/jest.config.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ const createJestConfig = nextJest({
const config: Config = {
...baseConfig,
testEnvironment: "node",
testMatch: ["**/__tests__/lib/**/*.ts?(x)", "**/__tests__/api/**/*.ts?(x)"],
testMatch: [
"**/__tests__/lib/**/*.ts?(x)",
"**/__tests__/api/**/*.ts?(x)",
"**/__tests__/apps/**/*.ts?(x)",
],
testPathIgnorePatterns: [
"/node_modules/",
"/.next/",
Expand Down
61 changes: 40 additions & 21 deletions __tests__/lib/utils/setupEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ jest.mock("node:child_process", () => {
}
})

import type {
ChildProcess,
ExecException,
ExecOptions,
} from "node:child_process"
import { exec as execCallback } from "node:child_process"

import { setupEnv } from "@/lib/utils/cli"
Expand All @@ -13,6 +18,13 @@ const execMock = execCallback as unknown as jest.MockedFunction<
typeof execCallback
>

// Typed callback used by our mock
type ExecCallback = (
error: ExecException | null,
stdout: string,
stderr: string
) => void

describe("setupEnv utility", () => {
const baseDir = process.cwd()

Expand All @@ -21,20 +33,21 @@ describe("setupEnv utility", () => {
})

it("returns confirmation when 'pnpm i' succeeds", async () => {
// Arrange mocked successful exec
execMock.mockImplementation(
(
cmd: string,
options: unknown,
callback: (error: Error | null, stdout: string, stderr: string) => void
) => {
// Support optional options argument
if (typeof options === "function") {
callback = options
}
optionsOrCallback?: ExecOptions | ExecCallback | null,
maybeCallback?: ExecCallback | null
): ChildProcess => {
const callback: ExecCallback =
typeof optionsOrCallback === "function"
? optionsOrCallback
: (maybeCallback as ExecCallback)

callback(null, "installation complete", "")
// Return dummy ChildProcess object
return {}

// We don't care about the ChildProcess in this test; satisfy TS with a cast.
return {} as ChildProcess
}
)

Expand All @@ -50,21 +63,27 @@ describe("setupEnv utility", () => {
})

it("throws a helpful error when the command fails", async () => {
// Arrange mocked failing exec
execMock.mockImplementation(
(
cmd: string,
options: unknown,
callback: (error: Error | null, stdout: string, stderr: string) => void
) => {
if (typeof options === "function") {
callback = options
}
const error = new Error("mock failure")
error.stdout = "some stdout"
error.stderr = "some stderr"
optionsOrCallback?: ExecOptions | ExecCallback | null,
maybeCallback?: ExecCallback | null
): ChildProcess => {
const callback: ExecCallback =
typeof optionsOrCallback === "function"
? optionsOrCallback
: (maybeCallback as ExecCallback)

const error: ExecException & {
stdout: string
stderr: string
} = Object.assign(new Error("mock failure"), {
stdout: "some stdout",
stderr: "some stderr",
})

callback(error, "some stdout", "some stderr")
return {}
return {} as ChildProcess
}
)

Expand Down