mirror of
https://github.com/Azure/k8s-deploy.git
synced 2026-06-23 13:09:27 +08:00
01cfe404ef
* Migrate build toolchain from ncc/Jest to esbuild/Vitest Replace the legacy ncc/Jest/Babel build stack with a modern ESM toolchain: Build: - Replace @vercel/ncc with esbuild (--platform=node --target=node20 --format=esm) - Add createRequire banner for CJS interop in ESM bundle - Add "type": "module" to package.json - Add tsc --noEmit typecheck script (esbuild strips types without checking) - Add typecheck to husky pre-commit hook Dependencies: - Bump @actions/core@3, exec@3, io@3, tool-cache@4 (ESM-only) - Replace jest/ts-jest/@babel/* with vitest@4 Tests: - Convert 29 test files: jest.fn()→vi.fn(), jest.mock()→vi.mock(), jest.spyOn()→vi.spyOn() - Fix vitest 4 compat: mockImplementation requires args, mock call tracking, await .rejects CI: - Update build step from ncc build → npm run build - Update composite action to use npm run build * Switch tsconfig to NodeNext module resolution Change module/moduleResolution from ES2022/bundler to NodeNext/NodeNext and target from ES2022 to ES2020. - Add .js extensions to all relative imports across 59 source/test files (required by NodeNext module resolution) - Add vitest/globals to tsconfig types array for global test API declarations
102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
import {vi} from 'vitest'
|
|
vi.mock('@actions/exec')
|
|
|
|
import {DockerExec} from './docker.js'
|
|
import * as actions from '@actions/exec'
|
|
|
|
const dockerPath = 'dockerPath'
|
|
const image = 'image'
|
|
const args = ['arg1', 'arg2', 'arg3']
|
|
|
|
describe('Docker class', () => {
|
|
const docker = new DockerExec(dockerPath)
|
|
|
|
describe('with a success exec return', () => {
|
|
const execReturn = {exitCode: 0, stdout: 'Output', stderr: ''}
|
|
|
|
beforeEach(() => {
|
|
vi.spyOn(actions, 'getExecOutput').mockImplementation(async () => {
|
|
return execReturn
|
|
})
|
|
})
|
|
|
|
test('pulls an image', async () => {
|
|
await docker.pull(image, args)
|
|
expect(actions.getExecOutput).toHaveBeenCalledWith(
|
|
dockerPath,
|
|
['pull', image, ...args],
|
|
{silent: false}
|
|
)
|
|
})
|
|
|
|
test('pulls an image silently', async () => {
|
|
await docker.pull(image, args, true)
|
|
expect(actions.getExecOutput).toHaveBeenCalledWith(
|
|
dockerPath,
|
|
['pull', image, ...args],
|
|
{silent: true}
|
|
)
|
|
})
|
|
|
|
test('inspects a docker image', async () => {
|
|
const result = await docker.inspect(image, args)
|
|
expect(result).toBe(execReturn.stdout)
|
|
expect(actions.getExecOutput).toHaveBeenCalledWith(
|
|
dockerPath,
|
|
['inspect', image, ...args],
|
|
{silent: false}
|
|
)
|
|
})
|
|
|
|
test('inspects a docker image silently', async () => {
|
|
const result = await docker.inspect(image, args, true)
|
|
expect(result).toBe(execReturn.stdout)
|
|
expect(actions.getExecOutput).toHaveBeenCalledWith(
|
|
dockerPath,
|
|
['inspect', image, ...args],
|
|
{silent: true}
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('with an unsuccessful exec return code', () => {
|
|
const execReturn = {exitCode: 3, stdout: '', stderr: ''}
|
|
|
|
beforeEach(() => {
|
|
vi.spyOn(actions, 'getExecOutput').mockImplementation(async () => {
|
|
return execReturn
|
|
})
|
|
})
|
|
|
|
test('pulls an image', async () => {
|
|
await expect(docker.pull(image, args)).rejects.toThrow()
|
|
})
|
|
|
|
test('inspects a docker image', async () => {
|
|
const result = await expect(
|
|
docker.inspect(image, args)
|
|
).rejects.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('with an unsuccessful exec return code', () => {
|
|
const execReturn = {exitCode: 0, stdout: '', stderr: 'Output'}
|
|
|
|
beforeEach(() => {
|
|
vi.spyOn(actions, 'getExecOutput').mockImplementation(async () => {
|
|
return execReturn
|
|
})
|
|
})
|
|
|
|
test('pulls an image', async () => {
|
|
await expect(docker.pull(image, args)).rejects.toThrow()
|
|
})
|
|
|
|
test('inspects a docker image', async () => {
|
|
const result = await expect(
|
|
docker.inspect(image, args)
|
|
).rejects.toThrow()
|
|
})
|
|
})
|
|
})
|