fix(tests): replace mock.module with spyOn to prevent test pollution
The opencode-config-agents-reader.test.ts was using mock.module() which permanently replaced the module in bun's module cache, causing state pollution in downstream tests (plugin-detection, write-omo-config, config-loader). Replaced with spyOn() pattern that properly restores in afterEach.
This commit is contained in:
@@ -1,23 +1,22 @@
|
|||||||
import { describe, expect, it, beforeEach, afterEach } from "bun:test"
|
import { describe, expect, it, beforeEach, afterEach, spyOn } from "bun:test"
|
||||||
import { mock } from "bun:test"
|
|
||||||
import * as fs from "node:fs"
|
import * as fs from "node:fs"
|
||||||
import * as os from "node:os"
|
import * as os from "node:os"
|
||||||
import * as path from "node:path"
|
import * as path from "node:path"
|
||||||
|
|
||||||
// Mock getOpenCodeConfigDir to prevent global config leakage
|
import * as configDir from "../../shared/opencode-config-dir"
|
||||||
let mockGlobalConfigDir: string
|
import { readOpencodeConfigAgents } from "./opencode-config-agents-reader"
|
||||||
mock.module("../../shared/opencode-config-dir", () => ({
|
|
||||||
getOpenCodeConfigDir: () => mockGlobalConfigDir,
|
|
||||||
}))
|
|
||||||
|
|
||||||
const { readOpencodeConfigAgents } = require("./opencode-config-agents-reader")
|
|
||||||
|
|
||||||
describe("readOpencodeConfigAgents", () => {
|
describe("readOpencodeConfigAgents", () => {
|
||||||
|
let mockGlobalConfigDir = ""
|
||||||
|
let configDirSpy: ReturnType<typeof spyOn>
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockGlobalConfigDir = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-mock-global-"))
|
mockGlobalConfigDir = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-mock-global-"))
|
||||||
|
configDirSpy = spyOn(configDir, "getOpenCodeConfigDir").mockReturnValue(mockGlobalConfigDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
configDirSpy.mockRestore()
|
||||||
fs.rmSync(mockGlobalConfigDir, { recursive: true, force: true })
|
fs.rmSync(mockGlobalConfigDir, { recursive: true, force: true })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user