-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
spec-helper.mjs
70 lines (62 loc) · 2.1 KB
/
spec-helper.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* node:coverage disable */
import assert from "node:assert/strict"
import { executionAsyncId } from "node:async_hooks"
import { createRequire } from "node:module"
import { test, before, after, beforeEach, afterEach, describe, it } from "node:test"
import { MockAgent, setGlobalDispatcher } from "undici"
import core from "@actions/core"
import "esm-reload"
globalThis.test = test
globalThis.before = before
globalThis.after = after
globalThis.beforeEach = beforeEach
globalThis.afterEach = afterEach
globalThis.describe = describe
globalThis.it = it
globalThis.assert = assert
globalThis.mockInput = function(input, value) {
process.env[`INPUT_${input.replaceAll(" ", "_").toUpperCase()}`] = value
}
globalThis.githubMockPool = function() {
return mockAgent.get("https://api.github.com")
}
globalThis.loadMain = async function() {
const testFile = process.argv[1]
if (!testFile.endsWith(".test.mjs")) {
throw new Error("Could not detect test file.")
}
const mainFile = testFile.substring(0, testFile.length - 8) + "mjs"
await import(`${mainFile}?instance=${executionAsyncId()}`)
}
// Don't inherit CI environment variables
for (const key of Object.keys(process.env)) {
if (key.startsWith("GITHUB_") || key.startsWith("INPUT_")) {
delete process.env[key]
}
}
// Consistent fake variables
process.env.GITHUB_ACTIONS = "true"
globalThis.GITHUB_REPOSITORY = "fake-owner/fake-repo"
process.env.GITHUB_REPOSITORY = GITHUB_REPOSITORY
process.env.GITHUB_REPOSITORY_OWNER = GITHUB_REPOSITORY.split("/", 1)[0]
const originalEnv = process.env
const require = createRequire(import.meta.url)
beforeEach((t) => {
process.env = structuredClone(originalEnv)
delete require.cache[require.resolve("@actions/github")]
const agent = new MockAgent()
agent.disableNetConnect()
setGlobalDispatcher(agent)
globalThis.mockAgent = agent
// Make core.setFailed raise an error rather than print to stdout
t.mock.method(core, "setFailed", (error) => {
if (typeof error === "string") {
throw new Error(error)
} else {
throw error
}
})
})
afterEach(() => {
mockAgent.assertNoPendingInterceptors()
})