Skip to content

Commit acda980

Browse files
committed
Convert tests to ESM
1 parent fe27a14 commit acda980

File tree

4 files changed

+117
-25
lines changed

4 files changed

+117
-25
lines changed

__tests__/main.test.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { ArtifactClient, UploadArtifactResponse } from '@actions/artifact'
2-
import * as core from '@actions/core'
3-
import * as github from '@actions/github'
2+
import core from '@actions/core'
3+
import github from '@actions/github'
44
import type { Context } from '@actions/github/lib/context'
5+
import { jest } from '@jest/globals'
56
import type { components } from '@octokit/openapi-types'
67
import type { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods'
78
import {
@@ -12,30 +13,36 @@ import {
1213
rm,
1314
writeFile
1415
} from 'node:fs/promises'
15-
import { join } from 'node:path'
16-
import { simpleGit, type DiffResult, type SimpleGit } from 'simple-git'
16+
import { dirname, join } from 'node:path'
17+
import { fileURLToPath } from 'node:url'
18+
import {
19+
simpleGit,
20+
type DiffResult,
21+
type FetchResult,
22+
type SimpleGit
23+
} from 'simple-git'
1724
import type { ActionInputs } from '../src/inputs'
1825
import { run } from '../src/main'
1926

20-
jest.mock('@actions/core')
21-
jest.mock('@actions/github')
22-
2327
describe('code-pushup action', () => {
2428
const workDir = join('tmp', 'git-repo')
2529

2630
let git: SimpleGit
2731
let artifact: ArtifactClient
2832

29-
let cwdSpy: jest.SpiedFunction<typeof process.cwd>
30-
3133
beforeEach(async () => {
3234
jest.clearAllMocks()
3335

34-
cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue(workDir)
36+
jest.spyOn(process, 'cwd').mockReturnValue(workDir)
3537

36-
// uncomment to see debug logs
37-
// jest.spyOn(core, 'debug').mockImplementation(console.debug)
38-
// jest.spyOn(core, 'isDebug').mockReturnValue(true)
38+
jest.spyOn(core, 'setOutput').mockReturnValue()
39+
jest.spyOn(core, 'setFailed').mockReturnValue()
40+
jest.spyOn(core, 'error').mockReturnValue()
41+
jest.spyOn(core, 'warning').mockReturnValue()
42+
jest.spyOn(core, 'notice').mockReturnValue()
43+
jest.spyOn(core, 'info').mockReturnValue()
44+
jest.spyOn(core, 'debug').mockReturnValue()
45+
jest.spyOn(core, 'isDebug').mockReturnValue(false)
3946

4047
jest.spyOn(core, 'getInput').mockImplementation((name: string): string => {
4148
switch (name as keyof ActionInputs) {
@@ -111,22 +118,26 @@ describe('code-pushup action', () => {
111118
jest.spyOn(github, 'getOctokit').mockReturnValue(mockOctokit)
112119

113120
artifact = {
114-
uploadArtifact: jest.fn().mockResolvedValue({
115-
id: 123
116-
} as UploadArtifactResponse) as ArtifactClient['uploadArtifact']
117-
} as ArtifactClient
121+
uploadArtifact: jest
122+
.fn<ArtifactClient['uploadArtifact']>()
123+
.mockResolvedValue({ id: 123 } as UploadArtifactResponse)
124+
} as Partial<ArtifactClient> as ArtifactClient
118125

119126
await rm(workDir, { recursive: true, force: true })
120127
await mkdir(workDir, { recursive: true })
121128
await copyFile(
122-
join(__dirname, 'fixtures', 'code-pushup.config.ts'),
129+
join(
130+
fileURLToPath(dirname(import.meta.url)),
131+
'fixtures',
132+
'code-pushup.config.ts'
133+
),
123134
join(workDir, 'code-pushup.config.ts')
124135
)
125136
await writeFile(join(workDir, 'index.js'), 'console.log("Hello, world!")')
126137

127138
git = simpleGit(workDir)
128139

129-
jest.spyOn(git, 'fetch').mockImplementation()
140+
jest.spyOn(git, 'fetch').mockResolvedValue({} as FetchResult)
130141
jest.spyOn(git, 'diffSummary').mockResolvedValue({
131142
files: [{ file: 'index.ts', binary: false }]
132143
} as DiffResult)
@@ -140,11 +151,12 @@ describe('code-pushup action', () => {
140151
await git.add('index.js')
141152
await git.add('code-pushup.config.ts')
142153
await git.commit('Initial commit')
154+
155+
github.context.ref = 'refs/heads/main'
156+
github.context.sha = await git.revparse('main')
143157
})
144158

145159
afterAll(async () => {
146-
cwdSpy.mockRestore()
147-
148160
await rm(workDir, { recursive: true, force: true })
149161
})
150162

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

package-lock.json

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
},
2626
"scripts": {
2727
"bundle": "npm run format:write && npm run package",
28-
"ci-test": "npx jest",
28+
"ci-test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
2929
"coverage": "npx make-coverage-badge --output-path ./badges/coverage.svg",
3030
"format:write": "npx prettier --write .",
3131
"format:check": "npx prettier --check .",
3232
"lint": "npx eslint . -c ./.github/linters/.eslintrc.yml",
3333
"package": "npx ncc build src/index.ts -o dist --source-map --license licenses.txt",
3434
"package:watch": "npm run package -- --watch",
35-
"test": "npx jest",
35+
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
3636
"all": "npm run format:write && npm run lint && npm run test && npm run coverage && npm run package"
3737
},
3838
"license": "MIT",
@@ -53,8 +53,19 @@
5353
"/dist/"
5454
],
5555
"transform": {
56-
"^.+\\.ts$": "ts-jest"
56+
"^.+\\.ts$": [
57+
"ts-jest",
58+
{
59+
"useESM": true
60+
}
61+
]
5762
},
63+
"moduleNameMapper": {
64+
"^(\\.{1,2}/.*)\\.js$": "$1"
65+
},
66+
"extensionsToTreatAsEsm": [
67+
".ts"
68+
],
5869
"coverageReporters": [
5970
"json-summary",
6071
"text",
@@ -83,6 +94,7 @@
8394
"@code-pushup/coverage-plugin": "^0.52.0",
8495
"@code-pushup/eslint-plugin": "^0.52.0",
8596
"@code-pushup/js-packages-plugin": "^0.52.0",
97+
"@code-pushup/portal-client": "^0.9.0",
8698
"@types/jest": "^29.5.13",
8799
"@types/node": "^22.7.5",
88100
"@typescript-eslint/eslint-plugin": "^7.18.0",

0 commit comments

Comments
 (0)