Skip to content
Merged
Changes from 2 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
34 changes: 24 additions & 10 deletions src/utils/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import * as realConfig from './config.js'
import * as realEnv from './env.js'
import * as realEnvUtils from './envUtils.js'

// Snapshot each real module surface into a plain object BEFORE any
// mock.module call. `import * as` gives a live namespace: mock.module
// repoints it, so restoring with the namespace itself re-installs the mock
// rather than the real module -- permanently, since mock.module lasts for the
// life of the process. That is how this suite's stderr-less `execa` stub used
// to escape into every later suite, where `result.stderr.trim()` then threw.
const realAuthSnapshot = { ...realAuth }
const realConfigSnapshot = { ...realConfig }
const realEnvSnapshot = { ...realEnv }
const realEnvUtilsSnapshot = { ...realEnvUtils }
const realExecaSnapshot = { ...realExeca }

const originalEnv = { ...process.env }
const originalMacro = (globalThis as Record<string, unknown>).MACRO

Expand All @@ -18,16 +30,18 @@ async function importFreshUserModule() {

async function importActualUserTestDeps() {
const nonce = `${Date.now()}-${Math.random()}`
const [authModule, configModule, execaModule] = await Promise.all([
const [authModule, configModule] = await Promise.all([
import(`./auth.js?ts=${nonce}`),
import(`./config.js?ts=${nonce}`),
import('execa'),
])

// execa comes from the pre-mock snapshot: a plain `import('execa')` here
// resolves to whatever mock is currently installed, so spreading it would
// build each new stub on top of the previous one.
return {
authModule,
configModule,
execaModule,
execaModule: realExecaSnapshot,
}
}

Expand Down Expand Up @@ -64,13 +78,13 @@ async function installCommonMocks(options?: {
}))

mock.module('./env.js', () => ({
...realEnv,
...realEnvSnapshot,
env: { platform: 'windows' },
getHostPlatformForAnalytics: () => 'windows',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}))

mock.module('./envUtils.js', () => ({
...realEnvUtils,
...realEnvUtilsSnapshot,
isEnvTruthy: (value: string | undefined) =>
!!value && value !== '0' && value.toLowerCase() !== 'false',
}))
Expand All @@ -97,11 +111,11 @@ beforeEach(async () => {
afterEach(() => {
try {
mock.restore()
mock.module('./auth.js', () => realAuth)
mock.module('./config.js', () => realConfig)
mock.module('./env.js', () => realEnv)
mock.module('./envUtils.js', () => realEnvUtils)
mock.module('execa', () => realExeca)
mock.module('./auth.js', () => realAuthSnapshot)
mock.module('./config.js', () => realConfigSnapshot)
mock.module('./env.js', () => realEnvSnapshot)
mock.module('./envUtils.js', () => realEnvUtilsSnapshot)
mock.module('execa', () => realExecaSnapshot)
process.env = { ...originalEnv }
if (originalMacro === undefined) {
delete (globalThis as Record<string, unknown>).MACRO
Expand Down
Loading