Skip to content

Commit

Permalink
test: upgrade Jest to v27 with new defaults
Browse files Browse the repository at this point in the history
also replace DefinitelyTyped with official type definitions.
https://jestjs.io/ja/blog/2021/05/25/jest-27
  • Loading branch information
KengoTODA committed Aug 22, 2021
1 parent 5ff23a2 commit 24f091a
Show file tree
Hide file tree
Showing 13 changed files with 2,253 additions and 5,702 deletions.
5 changes: 4 additions & 1 deletion __tests__/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { afterAll, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import io = require('@actions/io');
import fs = require('fs');
import path = require('path');
Expand All @@ -9,7 +12,7 @@ const m2Dir = path.join(__dirname, auth.M2_DIR);
const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE);

describe('auth tests', () => {
let spyOSHomedir: jest.SpyInstance;
let spyOSHomedir: SpyInstance<string, []>;

beforeEach(async () => {
await io.rmRF(m2Dir);
Expand Down
11 changes: 7 additions & 4 deletions __tests__/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { afterEach, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import { mkdtempSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
Expand All @@ -12,8 +15,8 @@ describe('dependency cache', () => {
const ORIGINAL_GITHUB_WORKSPACE = process.env['GITHUB_WORKSPACE'];
const ORIGINAL_CWD = process.cwd();
let workspace: string;
let spyInfo: jest.SpyInstance<void, Parameters<typeof core.info>>;
let spyWarning: jest.SpyInstance<void, Parameters<typeof core.warning>>;
let spyInfo: SpyInstance<void, Parameters<typeof core.info>>;
let spyWarning: SpyInstance<void, Parameters<typeof core.warning>>;

beforeEach(() => {
workspace = mkdtempSync(join(tmpdir(), 'setup-java-cache-'));
Expand Down Expand Up @@ -49,7 +52,7 @@ describe('dependency cache', () => {
});

describe('restore', () => {
let spyCacheRestore: jest.SpyInstance<
let spyCacheRestore: SpyInstance<
ReturnType<typeof cache.restoreCache>,
Parameters<typeof cache.restoreCache>
>;
Expand Down Expand Up @@ -108,7 +111,7 @@ describe('dependency cache', () => {
});
});
describe('save', () => {
let spyCacheSave: jest.SpyInstance<
let spyCacheSave: SpyInstance<
ReturnType<typeof cache.saveCache>,
Parameters<typeof cache.saveCache>
>;
Expand Down
9 changes: 6 additions & 3 deletions __tests__/cleanup-java.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { afterEach, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import { run as cleanup } from '../src/cleanup-java';
import * as core from '@actions/core';
import * as cache from '@actions/cache';
import * as util from '../src/util';

describe('cleanup', () => {
let spyWarning: jest.SpyInstance<void, Parameters<typeof core.warning>>;
let spyCacheSave: jest.SpyInstance<
let spyWarning: SpyInstance<void, Parameters<typeof core.warning>>;
let spyCacheSave: SpyInstance<
ReturnType<typeof cache.saveCache>,
Parameters<typeof cache.saveCache>
>;
let spyJobStatusSuccess: jest.SpyInstance;
let spyJobStatusSuccess: SpyInstance<boolean, []>;

beforeEach(() => {
spyWarning = jest.spyOn(core, 'warning');
Expand Down
54 changes: 33 additions & 21 deletions __tests__/distributors/adopt-installer.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { afterEach, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import { HttpClient } from '@actions/http-client';
import { IHeaders, ITypedResponse } from '@actions/http-client/interfaces';

import { AdoptDistribution, AdoptImplementation } from '../../src/distributions/adopt/installer';
import { JavaInstallerOptions } from '../../src/distributions/base-models';

let manifestData = require('../data/adopt.json') as [];

describe('getAvailableVersions', () => {
let spyHttpClient: jest.SpyInstance;
let spyHttpClient: SpyInstance<Promise<ITypedResponse<unknown>>, [string, IHeaders?]>;

beforeEach(() => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient.mockReturnValue({
statusCode: 200,
headers: {},
result: []
});
spyHttpClient.mockReturnValue(
Promise.resolve({
statusCode: 200,
headers: {},
result: []
})
);
});

afterEach(() => {
Expand Down Expand Up @@ -86,21 +92,27 @@ describe('getAvailableVersions', () => {
it('load available versions', async () => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: manifestData
})
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: manifestData
})
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: []
});
.mockReturnValueOnce(
Promise.resolve({
statusCode: 200,
headers: {},
result: manifestData
})
)
.mockReturnValueOnce(
Promise.resolve({
statusCode: 200,
headers: {},
result: manifestData
})
)
.mockReturnValueOnce(
Promise.resolve({
statusCode: 200,
headers: {},
result: []
})
);

const distribution = new AdoptDistribution(
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: false },
Expand Down
30 changes: 21 additions & 9 deletions __tests__/distributors/base-installer.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { afterEach, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import * as tc from '@actions/tool-cache';
import * as core from '@actions/core';
import * as util from '../../src/util';
Expand Down Expand Up @@ -42,8 +45,14 @@ describe('findInToolcache', () => {
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x64');

let mockJavaBase: EmptyJavaBase;
let spyGetToolcachePath: jest.SpyInstance;
let spyTcFindAllVersions: jest.SpyInstance;
let spyGetToolcachePath: SpyInstance<
ReturnType<typeof util.getToolcachePath>,
Parameters<typeof util.getToolcachePath>
>;
let spyTcFindAllVersions: SpyInstance<
ReturnType<typeof tc.findAllVersions>,
Parameters<typeof tc.findAllVersions>
>;

beforeEach(() => {
spyGetToolcachePath = jest.spyOn(util, 'getToolcachePath');
Expand Down Expand Up @@ -152,13 +161,16 @@ describe('setupJava', () => {

let mockJavaBase: EmptyJavaBase;

let spyGetToolcachePath: jest.SpyInstance;
let spyTcFindAllVersions: jest.SpyInstance;
let spyCoreDebug: jest.SpyInstance;
let spyCoreInfo: jest.SpyInstance;
let spyCoreExportVariable: jest.SpyInstance;
let spyCoreAddPath: jest.SpyInstance;
let spyCoreSetOutput: jest.SpyInstance;
let spyGetToolcachePath: SpyInstance<
ReturnType<typeof util.getToolcachePath>,
Parameters<typeof util.getToolcachePath>
>;
let spyTcFindAllVersions: SpyInstance<string[], Parameters<typeof tc.findAllVersions>>;
let spyCoreDebug: SpyInstance<void, [string]>;
let spyCoreInfo: SpyInstance<void, [string]>;
let spyCoreExportVariable: SpyInstance<void, [string, any]>;
let spyCoreAddPath: SpyInstance<void, [string]>;
let spyCoreSetOutput: SpyInstance<void, [string, any]>;

beforeEach(() => {
spyGetToolcachePath = jest.spyOn(util, 'getToolcachePath');
Expand Down
48 changes: 32 additions & 16 deletions __tests__/distributors/local-installer.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { afterEach, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import fs from 'fs';

import * as tc from '@actions/tool-cache';
Expand All @@ -15,18 +18,27 @@ describe('setupJava', () => {

let mockJavaBase: LocalDistribution;

let spyGetToolcachePath: jest.SpyInstance;
let spyTcCacheDir: jest.SpyInstance;
let spyTcFindAllVersions: jest.SpyInstance;
let spyCoreDebug: jest.SpyInstance;
let spyCoreInfo: jest.SpyInstance;
let spyCoreExportVariable: jest.SpyInstance;
let spyCoreAddPath: jest.SpyInstance;
let spyCoreSetOutput: jest.SpyInstance;
let spyFsStat: jest.SpyInstance;
let spyFsReadDir: jest.SpyInstance;
let spyUtilsExtractJdkFile: jest.SpyInstance;
let spyPathResolve: jest.SpyInstance;
let spyGetToolcachePath: SpyInstance<
ReturnType<typeof util.getToolcachePath>,
Parameters<typeof util.getToolcachePath>
>;
let spyTcCacheDir: SpyInstance<Promise<string>, Parameters<typeof tc.cacheDir>>;
let spyTcFindAllVersions: SpyInstance<
ReturnType<typeof tc.findAllVersions>,
Parameters<typeof tc.findAllVersions>
>;
let spyCoreDebug: SpyInstance<void, [string]>;
let spyCoreInfo: SpyInstance<void, [string]>;
let spyCoreExportVariable: SpyInstance<void, [string, any]>;
let spyCoreAddPath: SpyInstance<void, [string]>;
let spyCoreSetOutput: SpyInstance<void, [string, any]>;
let spyFsStat: SpyInstance<Partial<fs.Stats | fs.BigIntStats>, [fs.PathLike, fs.StatOptions?]>;
let spyFsReadDir: SpyInstance<unknown, Parameters<typeof fs.readdirSync>>;
let spyUtilsExtractJdkFile: SpyInstance<
ReturnType<typeof util.extractJdkFile>,
Parameters<typeof util.extractJdkFile>
>;
let spyPathResolve: SpyInstance<string, Parameters<typeof path.resolve>>;
let expectedJdkFile = 'JavaLocalJdkFile';

beforeEach(() => {
Expand All @@ -45,8 +57,12 @@ describe('setupJava', () => {

spyTcCacheDir = jest.spyOn(tc, 'cacheDir');
spyTcCacheDir.mockImplementation(
(archivePath: string, toolcacheFolderName: string, version: string, architecture: string) =>
path.join(toolcacheFolderName, version, architecture)
(
archivePath: string,
toolcacheFolderName: string,
version: string,
architecture: string | undefined
) => Promise.resolve(path.join(toolcacheFolderName, version, architecture || 'undefined'))
);

spyTcFindAllVersions = jest.spyOn(tc, 'findAllVersions');
Expand All @@ -73,13 +89,13 @@ describe('setupJava', () => {
spyFsReadDir.mockImplementation(() => ['JavaTest']);

spyFsStat = jest.spyOn(fs, 'statSync');
spyFsStat.mockImplementation((file: string) => {
spyFsStat.mockImplementation((file: fs.PathLike) => {
return { isFile: () => file === expectedJdkFile };
});

// Spy on util methods
spyUtilsExtractJdkFile = jest.spyOn(util, 'extractJdkFile');
spyUtilsExtractJdkFile.mockImplementation(() => 'some/random/path/');
spyUtilsExtractJdkFile.mockImplementation(() => Promise.resolve('some/random/path/'));

// Spy on path methods
spyPathResolve = jest.spyOn(path, 'resolve');
Expand Down
54 changes: 33 additions & 21 deletions __tests__/distributors/temurin-installer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { afterEach, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import { HttpClient } from '@actions/http-client';
import { IHeaders, ITypedResponse } from '@actions/http-client/interfaces';

import {
TemurinDistribution,
Expand All @@ -9,15 +13,17 @@ import { JavaInstallerOptions } from '../../src/distributions/base-models';
let manifestData = require('../data/temurin.json') as [];

describe('getAvailableVersions', () => {
let spyHttpClient: jest.SpyInstance;
let spyHttpClient: SpyInstance<Promise<ITypedResponse<unknown>>, [string, IHeaders?]>;

beforeEach(() => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient.mockReturnValue({
statusCode: 200,
headers: {},
result: []
});
spyHttpClient.mockReturnValue(
Promise.resolve({
statusCode: 200,
headers: {},
result: []
})
);
});

afterEach(() => {
Expand Down Expand Up @@ -69,21 +75,27 @@ describe('getAvailableVersions', () => {
it('load available versions', async () => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: manifestData
})
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: manifestData
})
.mockReturnValueOnce({
statusCode: 200,
headers: {},
result: []
});
.mockReturnValueOnce(
Promise.resolve({
statusCode: 200,
headers: {},
result: manifestData
})
)
.mockReturnValueOnce(
Promise.resolve({
statusCode: 200,
headers: {},
result: manifestData
})
)
.mockReturnValueOnce(
Promise.resolve({
statusCode: 200,
headers: {},
result: []
})
);

const distribution = new TemurinDistribution(
{ version: '8', architecture: 'x64', packageType: 'jdk', checkLatest: false },
Expand Down
21 changes: 13 additions & 8 deletions __tests__/distributors/zulu-installer.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { afterEach, beforeEach, expect, describe, it, jest } from '@jest/globals';
import { SpyInstance } from 'jest-mock';

import { HttpClient } from '@actions/http-client';
import * as semver from 'semver';
import { IHeaders, ITypedResponse } from '@actions/http-client/interfaces';
import { ZuluDistribution } from '../../src/distributions/zulu/installer';
import { IZuluVersions } from '../../src/distributions/zulu/models';
import * as utils from '../../src/util';

const manifestData = require('../data/zulu-releases-default.json') as [];

describe('getAvailableVersions', () => {
let spyHttpClient: jest.SpyInstance;
let spyUtilGetDownloadArchiveExtension: jest.SpyInstance;
let spyHttpClient: SpyInstance<Promise<ITypedResponse<unknown>>, [string, IHeaders?]>;
let spyUtilGetDownloadArchiveExtension: SpyInstance<string, []>;

beforeEach(() => {
spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson');
spyHttpClient.mockReturnValue({
statusCode: 200,
headers: {},
result: manifestData as IZuluVersions[]
});
spyHttpClient.mockReturnValue(
Promise.resolve({
statusCode: 200,
headers: {},
result: manifestData as IZuluVersions[]
})
);

spyUtilGetDownloadArchiveExtension = jest.spyOn(utils, 'getDownloadArchiveExtension');
spyUtilGetDownloadArchiveExtension.mockReturnValue('tar.gz');
Expand Down
2 changes: 2 additions & 0 deletions __tests__/gpg.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { afterAll, beforeEach, expect, describe, it, jest } from '@jest/globals';

import path = require('path');
import io = require('@actions/io');
import exec = require('@actions/exec');
Expand Down
1 change: 1 addition & 0 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, describe, it } from '@jest/globals';
import { isVersionSatisfies } from '../src/util';

describe('isVersionSatisfies', () => {
Expand Down

0 comments on commit 24f091a

Please sign in to comment.