From 3f7be6d97d121ef2bdaf3e9135a3082a1c285c20 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Wed, 28 Jan 2026 11:52:41 +0100 Subject: [PATCH] add ESM-safe os test helpers and tsconfig for tests Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/.helpers/os.ts | 30 +++++++++++++++++++++++++++++ __tests__/buildx/install.test.ts | 9 +++++---- __tests__/compose/install.test.ts | 9 +++++---- __tests__/cosign/install.test.ts | 9 +++++---- __tests__/docker/docker.test.ts | 5 +++-- __tests__/docker/install.test.ts | 7 ++++--- __tests__/oci/oci.test.ts | 9 +++++---- __tests__/regclient/install.test.ts | 9 +++++---- __tests__/undock/install.test.ts | 9 +++++---- jest.config.cjs | 3 ++- jest.config.itg.cjs | 3 ++- tsconfig.test.json | 15 +++++++++++++++ 12 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 __tests__/.helpers/os.ts create mode 100644 tsconfig.test.json diff --git a/__tests__/.helpers/os.ts b/__tests__/.helpers/os.ts new file mode 100644 index 0000000..0f2f770 --- /dev/null +++ b/__tests__/.helpers/os.ts @@ -0,0 +1,30 @@ +/** + * Copyright 2025 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {jest} from '@jest/globals'; +import os from 'os'; + +export const mockPlatform = (platform: NodeJS.Platform) => { + return jest.spyOn(os, 'platform').mockImplementation(() => platform); +}; + +export const mockArch = (arch: string) => { + return jest.spyOn(os, 'arch').mockImplementation(() => arch); +}; + +export const mockHomedir = (dir: string) => { + return jest.spyOn(os, 'homedir').mockImplementation(() => dir); +}; diff --git a/__tests__/buildx/install.test.ts b/__tests__/buildx/install.test.ts index 9defe18..fcfa311 100644 --- a/__tests__/buildx/install.test.ts +++ b/__tests__/buildx/install.test.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; +import {describe, expect, it, test, afterEach} from '@jest/globals'; import fs from 'fs'; import os from 'os'; import path from 'path'; import * as rimraf from 'rimraf'; -import osm = require('os'); + +import {mockArch, mockPlatform} from '../.helpers/os'; import {Install} from '../../src/buildx/install'; @@ -85,8 +86,8 @@ describe('download', () => { ['linux', 's390x'], ])( 'acquires buildx for %s/%s', async (os, arch) => { - jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); - jest.spyOn(osm, 'arch').mockImplementation(() => arch); + mockPlatform(os as NodeJS.Platform); + mockArch(arch); const install = new Install(); const buildxBin = await install.download('latest'); expect(fs.existsSync(buildxBin)).toBe(true); diff --git a/__tests__/compose/install.test.ts b/__tests__/compose/install.test.ts index c87ea0c..b50da2c 100644 --- a/__tests__/compose/install.test.ts +++ b/__tests__/compose/install.test.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; +import {describe, expect, it, test, afterEach} from '@jest/globals'; import fs from 'fs'; import os from 'os'; import path from 'path'; import * as rimraf from 'rimraf'; -import osm = require('os'); + +import {mockArch, mockPlatform} from '../.helpers/os'; import {Install} from '../../src/compose/install'; @@ -85,8 +86,8 @@ describe('download', () => { ['linux', 's390x'], ])( 'acquires compose for %s/%s', async (os, arch) => { - jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); - jest.spyOn(osm, 'arch').mockImplementation(() => arch); + mockPlatform(os as NodeJS.Platform); + mockArch(arch); const install = new Install(); const composeBin = await install.download('latest'); expect(fs.existsSync(composeBin)).toBe(true); diff --git a/__tests__/cosign/install.test.ts b/__tests__/cosign/install.test.ts index f77fe10..4a97fc8 100644 --- a/__tests__/cosign/install.test.ts +++ b/__tests__/cosign/install.test.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; +import {describe, expect, it, test, afterEach} from '@jest/globals'; import fs from 'fs'; import os from 'os'; import path from 'path'; import * as rimraf from 'rimraf'; -import osm = require('os'); + +import {mockArch, mockPlatform} from '../.helpers/os'; import {Install} from '../../src/cosign/install'; @@ -80,8 +81,8 @@ describe('download', () => { ['linux', 'arm64'] ])( 'acquires undock for %s/%s', async (os, arch) => { - jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); - jest.spyOn(osm, 'arch').mockImplementation(() => arch); + mockPlatform(os as NodeJS.Platform); + mockArch(arch); const install = new Install(); const cosignBin = await install.download({ version: 'latest' diff --git a/__tests__/docker/docker.test.ts b/__tests__/docker/docker.test.ts index c0bd65a..483c52a 100644 --- a/__tests__/docker/docker.test.ts +++ b/__tests__/docker/docker.test.ts @@ -19,9 +19,10 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import * as io from '@actions/io'; -import osm = require('os'); import * as rimraf from 'rimraf'; +import {mockHomedir} from '../.helpers/os'; + import {Docker} from '../../src/docker/docker'; import {ConfigFile} from '../../src/types/docker/docker'; @@ -47,7 +48,7 @@ describe('configDir', () => { }); it('returns default', async () => { process.env.DOCKER_CONFIG = ''; - jest.spyOn(osm, 'homedir').mockImplementation(() => path.join('/tmp', 'home')); + mockHomedir(path.join('/tmp', 'home')); expect(Docker.configDir).toEqual(path.join('/tmp', 'home', '.docker')); }); it('returns from env', async () => { diff --git a/__tests__/docker/install.test.ts b/__tests__/docker/install.test.ts index 7b1d1ce..aebec66 100644 --- a/__tests__/docker/install.test.ts +++ b/__tests__/docker/install.test.ts @@ -19,7 +19,8 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import * as rimraf from 'rimraf'; -import osm = require('os'); + +import {mockArch, mockPlatform} from '../.helpers/os'; import {Install, InstallSourceArchive, InstallSourceImage} from '../../src/docker/install'; @@ -60,8 +61,8 @@ describe('download', () => { [image('27.3.1'), 'win32'], ])( 'acquires %p of docker (%s)', async (source, platformOS) => { - jest.spyOn(osm, 'platform').mockImplementation(() => platformOS as NodeJS.Platform); - jest.spyOn(osm, 'arch').mockImplementation(() => 'x64'); + mockPlatform(platformOS as NodeJS.Platform); + mockArch('x64'); const install = new Install({ source: source, runDir: tmpDir diff --git a/__tests__/oci/oci.test.ts b/__tests__/oci/oci.test.ts index 684f88f..0125ee7 100644 --- a/__tests__/oci/oci.test.ts +++ b/__tests__/oci/oci.test.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import {afterEach, describe, expect, jest, test} from '@jest/globals'; +import {afterEach, describe, expect, test} from '@jest/globals'; import fs from 'fs'; import os from 'os'; import path from 'path'; import * as rimraf from 'rimraf'; -import osm = require('os'); + +import {mockArch, mockPlatform} from '../.helpers/os'; import {OCI} from '../../src/oci/oci'; @@ -44,8 +45,8 @@ describe('defaultPlatform', () => { ['linux', 'ppc64', {architecture: 'ppc64le', os: 'linux'}], ['linux', 's390x', {architecture: 's390x', os: 'linux'}] ])('default platform for %s/%s', async (os: string, arch: string, expected: Platform) => { - jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); - jest.spyOn(osm, 'arch').mockImplementation(() => arch); + mockPlatform(os as NodeJS.Platform); + mockArch(arch); const res = OCI.defaultPlatform(); expect(res).toEqual(expected); }); diff --git a/__tests__/regclient/install.test.ts b/__tests__/regclient/install.test.ts index 132fd7a..90733a8 100644 --- a/__tests__/regclient/install.test.ts +++ b/__tests__/regclient/install.test.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; +import {describe, expect, it, test, afterEach} from '@jest/globals'; import fs from 'fs'; import os from 'os'; import path from 'path'; import * as rimraf from 'rimraf'; -import osm = require('os'); + +import {mockArch, mockPlatform} from '../.helpers/os'; import {Install} from '../../src/regclient/install'; @@ -75,8 +76,8 @@ describe('download', () => { ['linux', 's390x'], ])( 'acquires regclient for %s/%s', async (os, arch) => { - jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); - jest.spyOn(osm, 'arch').mockImplementation(() => arch); + mockPlatform(os as NodeJS.Platform); + mockArch(arch); const install = new Install(); const regclientBin = await install.download('latest'); expect(fs.existsSync(regclientBin)).toBe(true); diff --git a/__tests__/undock/install.test.ts b/__tests__/undock/install.test.ts index fe604cf..3d07580 100644 --- a/__tests__/undock/install.test.ts +++ b/__tests__/undock/install.test.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; +import {describe, expect, it, test, afterEach} from '@jest/globals'; import fs from 'fs'; import os from 'os'; import path from 'path'; import * as rimraf from 'rimraf'; -import osm = require('os'); + +import {mockArch, mockPlatform} from '../.helpers/os'; import {Install} from '../../src/undock/install'; @@ -80,8 +81,8 @@ describe('download', () => { ['linux', 's390x'], ])( 'acquires undock for %s/%s', async (os, arch) => { - jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); - jest.spyOn(osm, 'arch').mockImplementation(() => arch); + mockPlatform(os as NodeJS.Platform); + mockArch(arch); const install = new Install(); const undockBin = await install.download('latest'); expect(fs.existsSync(undockBin)).toBe(true); diff --git a/jest.config.cjs b/jest.config.cjs index 6104c12..374a9f0 100644 --- a/jest.config.cjs +++ b/jest.config.cjs @@ -41,7 +41,8 @@ module.exports = { '^.+\\.ts$': [ 'ts-jest', { - useESM: true + useESM: true, + tsconfig: '/tsconfig.test.json' } ] }, diff --git a/jest.config.itg.cjs b/jest.config.itg.cjs index 477478f..8754752 100644 --- a/jest.config.itg.cjs +++ b/jest.config.itg.cjs @@ -24,7 +24,8 @@ module.exports = { '^.+\\.ts$': [ 'ts-jest', { - useESM: true + useESM: true, + tsconfig: '/tsconfig.test.json' } ] }, diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 0000000..d78b1e5 --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "noEmit": true + }, + "include": [ + "src/**/*.ts", + "__tests__/**/*.ts" + ], + "exclude": [ + "lib", + "node_modules" + ] +}