From 5c3fc146af83442e5a787e43057c397024bbfb2d Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:04:28 +0200 Subject: [PATCH 1/3] jest: no need to clear mock for integration tests Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/buildx/bake.test.itg.ts | 6 +----- __tests__/buildx/history.test.itg.ts | 6 +----- __tests__/github.test.itg.ts | 6 +----- jest.config.itg.ts | 1 - 4 files changed, 3 insertions(+), 16 deletions(-) diff --git a/__tests__/buildx/bake.test.itg.ts b/__tests__/buildx/bake.test.itg.ts index c9b900d..f4499f2 100644 --- a/__tests__/buildx/bake.test.itg.ts +++ b/__tests__/buildx/bake.test.itg.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {beforeEach, describe, expect, jest, test} from '@jest/globals'; +import {describe, expect, test} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; @@ -25,10 +25,6 @@ const fixturesDir = path.join(__dirname, '..', 'fixtures'); const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip; -beforeEach(() => { - jest.clearAllMocks(); -}); - maybe('getDefinition', () => { // prettier-ignore test.each([ diff --git a/__tests__/buildx/history.test.itg.ts b/__tests__/buildx/history.test.itg.ts index 009056b..78188c3 100644 --- a/__tests__/buildx/history.test.itg.ts +++ b/__tests__/buildx/history.test.itg.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {beforeEach, describe, expect, jest, test} from '@jest/globals'; +import {describe, expect, test} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; @@ -31,10 +31,6 @@ const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-history-jest'); const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip; -beforeEach(() => { - jest.clearAllMocks(); -}); - maybe('exportBuild', () => { // prettier-ignore test.each([ diff --git a/__tests__/github.test.itg.ts b/__tests__/github.test.itg.ts index 852e0a6..2163a76 100644 --- a/__tests__/github.test.itg.ts +++ b/__tests__/github.test.itg.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {beforeEach, describe, expect, it, jest, test} from '@jest/globals'; +import {describe, expect, it, test} from '@jest/globals'; import fs from 'fs'; import * as path from 'path'; @@ -32,10 +32,6 @@ const tmpDir = path.join(process.env.TEMP || '/tmp', 'github-jest'); const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip; -beforeEach(() => { - jest.clearAllMocks(); -}); - maybe('uploadArtifact', () => { it('uploads an artifact', async () => { const res = await GitHub.uploadArtifact({ diff --git a/jest.config.itg.ts b/jest.config.itg.ts index 9628c49..0e1365e 100644 --- a/jest.config.itg.ts +++ b/jest.config.itg.ts @@ -33,7 +33,6 @@ process.env = Object.assign({}, process.env, { }; module.exports = { - clearMocks: true, testEnvironment: 'node', moduleFileExtensions: ['js', 'ts'], setupFiles: ['dotenv/config'], From f27d52d6b76350ab7425ba0bec2e8cc4614d1970 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:04:28 +0200 Subject: [PATCH 2/3] jest: keep original github env vars for integration tests Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- jest.config.itg.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/jest.config.itg.ts b/jest.config.itg.ts index 0e1365e..56f56e0 100644 --- a/jest.config.itg.ts +++ b/jest.config.itg.ts @@ -22,10 +22,6 @@ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')) process.env = Object.assign({}, process.env, { TEMP: tmpDir, - GITHUB_REPOSITORY: 'docker/actions-toolkit', - GITHUB_RUN_ATTEMPT: 2, - GITHUB_RUN_ID: 2188748038, - GITHUB_RUN_NUMBER: 15, RUNNER_TEMP: path.join(tmpDir, 'runner-temp'), RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache') }) as { From 953dc3bb0031b614c3079cf04ef5ca8b1d8b6ea8 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:59:00 +0200 Subject: [PATCH 3/3] jest: mocks already cleared through configuration Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/buildkit/buildkit.test.ts | 6 +----- __tests__/buildkit/config.test.ts | 6 +----- __tests__/buildx/bake.test.ts | 6 +----- __tests__/buildx/build.test.ts | 4 ---- __tests__/buildx/builder.test.ts | 6 +----- __tests__/buildx/buildx.test.ts | 4 ---- __tests__/buildx/install.test.ts | 6 +----- __tests__/context.test.ts | 6 +----- __tests__/docker/docker.test.ts | 4 ---- __tests__/docker/install.test.ts | 4 ---- __tests__/dockerhub.test.ts | 6 +----- __tests__/exec.test.ts | 6 +----- __tests__/git.test.ts | 1 - __tests__/github.test.ts | 4 ---- __tests__/oci/oci.test.ts | 6 +----- __tests__/util.test.ts | 6 +----- 16 files changed, 10 insertions(+), 71 deletions(-) diff --git a/__tests__/buildkit/buildkit.test.ts b/__tests__/buildkit/buildkit.test.ts index de6f5b8..ea1b1f9 100644 --- a/__tests__/buildkit/buildkit.test.ts +++ b/__tests__/buildkit/buildkit.test.ts @@ -14,17 +14,13 @@ * limitations under the License. */ -import {beforeEach, describe, expect, it, jest, test} from '@jest/globals'; +import {describe, expect, it, jest, test} from '@jest/globals'; import {BuildKit} from '../../src/buildkit/buildkit'; import {Builder} from '../../src/buildx/builder'; import {BuilderInfo} from '../../src/types/buildx/builder'; -beforeEach(() => { - jest.clearAllMocks(); -}); - jest.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise => { return { name: 'builder2', diff --git a/__tests__/buildkit/config.test.ts b/__tests__/buildkit/config.test.ts index 13a7cf5..bf345ff 100644 --- a/__tests__/buildkit/config.test.ts +++ b/__tests__/buildkit/config.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {describe, expect, jest, test, beforeEach, afterEach} from '@jest/globals'; +import {describe, expect, jest, test, afterEach} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; import * as rimraf from 'rimraf'; @@ -38,10 +38,6 @@ jest.spyOn(Context, 'tmpName').mockImplementation((): string => { return tmpName; }); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(() => { rimraf.sync(tmpDir); }); diff --git a/__tests__/buildx/bake.test.ts b/__tests__/buildx/bake.test.ts index fd8b97a..c4464d2 100644 --- a/__tests__/buildx/bake.test.ts +++ b/__tests__/buildx/bake.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {afterEach, beforeEach, describe, expect, it, jest, test} from '@jest/globals'; +import {afterEach, describe, expect, it, jest, test} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; import * as rimraf from 'rimraf'; @@ -49,10 +49,6 @@ jest.spyOn(Context, 'tmpName').mockImplementation((): string => { return tmpName; }); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(() => { rimraf.sync(tmpDir); }); diff --git a/__tests__/buildx/build.test.ts b/__tests__/buildx/build.test.ts index c348a42..318a5fa 100644 --- a/__tests__/buildx/build.test.ts +++ b/__tests__/buildx/build.test.ts @@ -39,10 +39,6 @@ jest.spyOn(Context, 'tmpName').mockImplementation((): string => { return tmpName; }); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(() => { rimraf.sync(tmpDir); }); diff --git a/__tests__/buildx/builder.test.ts b/__tests__/buildx/builder.test.ts index fda5101..dd060f5 100644 --- a/__tests__/buildx/builder.test.ts +++ b/__tests__/buildx/builder.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {beforeEach, describe, expect, it, jest, test} from '@jest/globals'; +import {describe, expect, it, jest, test} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; @@ -25,10 +25,6 @@ import {BuilderInfo} from '../../src/types/buildx/builder'; const fixturesDir = path.join(__dirname, '..', 'fixtures'); -beforeEach(() => { - jest.clearAllMocks(); -}); - jest.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise => { return { name: 'builder2', diff --git a/__tests__/buildx/buildx.test.ts b/__tests__/buildx/buildx.test.ts index 25cf505..113a742 100644 --- a/__tests__/buildx/buildx.test.ts +++ b/__tests__/buildx/buildx.test.ts @@ -42,10 +42,6 @@ jest.spyOn(Context, 'tmpName').mockImplementation((): string => { return tmpName; }); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(() => { rimraf.sync(tmpDir); }); diff --git a/__tests__/buildx/install.test.ts b/__tests__/buildx/install.test.ts index c8269d4..92ae0b3 100644 --- a/__tests__/buildx/install.test.ts +++ b/__tests__/buildx/install.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {describe, expect, it, jest, test, beforeEach, afterEach} from '@jest/globals'; +import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; import * as rimraf from 'rimraf'; @@ -25,10 +25,6 @@ import {Install} from '../../src/buildx/install'; // prettier-ignore const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-jest'); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(function () { rimraf.sync(tmpDir); }); diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 7833e00..325655c 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -17,7 +17,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as rimraf from 'rimraf'; -import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals'; +import {describe, expect, jest, it, afterEach} from '@jest/globals'; import {Context} from '../src/context'; @@ -36,10 +36,6 @@ jest.spyOn(Context, 'tmpName').mockImplementation((): string => { return tmpName; }); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(() => { rimraf.sync(tmpDir); }); diff --git a/__tests__/docker/docker.test.ts b/__tests__/docker/docker.test.ts index 000ce50..cb75ccc 100644 --- a/__tests__/docker/docker.test.ts +++ b/__tests__/docker/docker.test.ts @@ -31,10 +31,6 @@ const fixturesDir = path.join(__dirname, '..', 'fixtures'); // prettier-ignore const tmpDir = path.join(process.env.TEMP || '/tmp', 'docker-jest'); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(function () { rimraf.sync(tmpDir); }); diff --git a/__tests__/docker/install.test.ts b/__tests__/docker/install.test.ts index 6b04848..ab92131 100644 --- a/__tests__/docker/install.test.ts +++ b/__tests__/docker/install.test.ts @@ -25,10 +25,6 @@ import {Install} from '../../src/docker/install'; // prettier-ignore const tmpDir = path.join(process.env.TEMP || '/tmp', 'docker-install-jest'); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(function () { rimraf.sync(tmpDir); }); diff --git a/__tests__/dockerhub.test.ts b/__tests__/dockerhub.test.ts index 8631925..971cfb5 100644 --- a/__tests__/dockerhub.test.ts +++ b/__tests__/dockerhub.test.ts @@ -14,17 +14,13 @@ * limitations under the License. */ -import {describe, expect, jest, it, beforeEach} from '@jest/globals'; +import {describe, expect, jest, it} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; import {DockerHub} from '../src/dockerhub'; import {RepositoryResponse, RepositoryTagsResponse} from '../src/types/dockerhub'; -beforeEach(() => { - jest.clearAllMocks(); -}); - import repoInfoFixture from './fixtures/dockerhub-repoinfo.json'; import repoTagsFixture from './fixtures/dockerhub-repotags.json'; import repoAllTagsFixture from './fixtures/dockerhub-repoalltags.json'; diff --git a/__tests__/exec.test.ts b/__tests__/exec.test.ts index 0880af0..4d15645 100644 --- a/__tests__/exec.test.ts +++ b/__tests__/exec.test.ts @@ -14,14 +14,10 @@ * limitations under the License. */ -import {beforeEach, describe, expect, it, jest} from '@jest/globals'; +import {describe, expect, it, jest} from '@jest/globals'; import {Exec} from '../src/exec'; -beforeEach(() => { - jest.clearAllMocks(); -}); - describe('exec', () => { it('returns docker version', async () => { const execSpy = jest.spyOn(Exec, 'exec'); diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 05d437d..8beb864 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -21,7 +21,6 @@ import {Exec} from '../src/exec'; import {ExecOutput} from '@actions/exec'; beforeEach(() => { - jest.clearAllMocks(); jest.restoreAllMocks(); }); diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 3ac4008..54c211a 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -22,10 +22,6 @@ import * as core from '@actions/core'; import {GitHub} from '../src/github'; import {GitHubRepo} from '../src/types/github'; -beforeEach(() => { - jest.clearAllMocks(); -}); - import repoFixture from './fixtures/github-repo.json'; jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise => { return >(repoFixture as unknown); diff --git a/__tests__/oci/oci.test.ts b/__tests__/oci/oci.test.ts index 7c60ac4..54dadf5 100644 --- a/__tests__/oci/oci.test.ts +++ b/__tests__/oci/oci.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {afterEach, beforeEach, describe, expect, jest, test} from '@jest/globals'; +import {afterEach, describe, expect, test} from '@jest/globals'; import * as fs from 'fs'; import path from 'path'; import * as rimraf from 'rimraf'; @@ -26,10 +26,6 @@ const fixturesDir = path.join(__dirname, '..', 'fixtures'); // prettier-ignore const tmpDir = path.join(process.env.TEMP || '/tmp', 'docker-jest'); -beforeEach(() => { - jest.clearAllMocks(); -}); - afterEach(function () { rimraf.sync(tmpDir); }); diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index e0cf658..2e67ec2 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -14,16 +14,12 @@ * limitations under the License. */ -import {beforeEach, describe, expect, it, jest, test} from '@jest/globals'; +import {describe, expect, it, test} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; import {Util} from '../src/util'; -beforeEach(() => { - jest.clearAllMocks(); -}); - describe('getInputList', () => { it('single line correctly', async () => { await setInput('foo', 'bar');