Convert to ESM.

Context: https://github.com/actions/typescript-action/pull/969
This commit is contained in:
Fabio Niephaus
2025-09-16 13:20:56 +02:00
committed by Fabio Niephaus
parent 59f089d81e
commit b4c67abb33
35 changed files with 118600 additions and 129021 deletions

View File

@@ -24,21 +24,23 @@
* Forked from https://github.com/actions/setup-java/blob/5b36705a13905facb447b6812d613a06a07e371d/__tests__/cleanup-java.test.ts
*/
import { run as cleanup } from '../src/cleanup'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import { jest } from '@jest/globals'
import * as cache from '../__fixtures__/cache.js'
import * as core from '../__fixtures__/core.js'
// Mocks should be declared before the module being tested is imported.
jest.unstable_mockModule('@actions/core', () => core)
jest.unstable_mockModule('@actions/cache', () => cache)
// The module being tested should be imported dynamically. This ensures that the
// mocks are used in place of any actual dependencies.
const { run } = await import('../src/cleanup.js')
describe('cleanup', () => {
let spyWarning: jest.SpyInstance<void, Parameters<typeof core.warning>>
let spyInfo: jest.SpyInstance<void, Parameters<typeof core.info>>
let spyCacheSave: jest.SpyInstance<ReturnType<typeof cache.saveCache>, Parameters<typeof cache.saveCache>>
beforeEach(() => {
spyWarning = jest.spyOn(core, 'warning')
spyWarning.mockImplementation(() => null)
spyInfo = jest.spyOn(core, 'info')
spyInfo.mockImplementation(() => null)
spyCacheSave = jest.spyOn(cache, 'saveCache')
core.info.mockImplementation(() => null)
core.warning.mockImplementation(() => null)
core.debug.mockImplementation(() => null)
createStateForSuccessfulRestore()
})
afterEach(() => {
@@ -46,38 +48,40 @@ describe('cleanup', () => {
})
it('does not fail nor warn even when the save process throws a ReserveCacheError', async () => {
spyCacheSave.mockImplementation((_paths: string[], _key: string) =>
cache.saveCache.mockImplementation((_paths: string[], _key: string) =>
Promise.reject(
new cache.ReserveCacheError('Unable to reserve cache with key, another job may be creating this cache.')
)
)
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
core.getInput.mockImplementation((name: string) => {
return name === 'cache' ? 'gradle' : ''
})
await cleanup()
expect(spyCacheSave).toHaveBeenCalled()
expect(spyWarning).not.toHaveBeenCalled()
await run()
expect(cache.saveCache).toHaveBeenCalled()
expect(core.warning).not.toHaveBeenCalled()
})
it('does not fail even though the save process throws error', async () => {
spyCacheSave.mockImplementation((_paths: string[], _key: string) => Promise.reject(new Error('Unexpected error')))
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
cache.saveCache.mockImplementation((_paths: string[], _key: string) =>
Promise.reject(new Error('Unexpected error'))
)
core.getInput.mockImplementation((name: string) => {
return name === 'cache' ? 'gradle' : ''
})
await cleanup()
expect(spyCacheSave).toHaveBeenCalled()
await run()
expect(cache.saveCache).toHaveBeenCalled()
})
})
function resetState() {
jest.spyOn(core, 'getState').mockReset()
core.getState.mockReset()
}
/**
* Create states to emulate a successful restore process.
*/
function createStateForSuccessfulRestore() {
jest.spyOn(core, 'getState').mockImplementation((name) => {
core.getState.mockImplementation((name) => {
switch (name) {
case 'cache-primary-key':
return 'setup-java-cache-primary-key'