Skip to content

Commit

Permalink
Migrate jest to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiazya committed Feb 5, 2024
1 parent e300ed5 commit d30e381
Show file tree
Hide file tree
Showing 4 changed files with 651 additions and 1,946 deletions.
9 changes: 0 additions & 9 deletions jest.config.ts

This file was deleted.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,22 @@
"lint": "eslint src && prettier --check src/**/*.ts",
"format": "prettier --write src/**/*.ts",
"build": "tsc",
"test": "jest"
"test": "vitest"
},
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/tool-cache": "^2.0.1"
},
"devDependencies": {
"@types/jest": "^28.1.7",
"@types/node": "^20.11.13",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.8.3",
"jest": "^28.1.3",
"prettier": "^2.7.1",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
"typescript": "^5.3.2",
"vitest": "^1.2.2"
}
}
115 changes: 58 additions & 57 deletions src/__tests__/GraphvizInstaller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
jest.mock('@actions/core');
jest.mock('@actions/exec');
import { vi, describe, beforeEach, beforeAll, afterAll, it, test, expect, MockInstance } from 'vitest';
vi.mock('@actions/core');
vi.mock('@actions/exec');

import { getInput, getBooleanInput } from '@actions/core';
import * as exec from '@actions/exec';
Expand All @@ -13,7 +14,7 @@ describe('class GraphvizInstaller', () => {
Object.defineProperty(process, 'platform', { value: platform });
};

const mockNamedInputs = (mock: jest.Mock, name: string, value: string | boolean) => {
const mockNamedInputs = (mock: MockInstance, name: string, value: string | boolean) => {
mock.mockImplementation((input: string) => {
if (input == name)
return value;
Expand All @@ -23,7 +24,7 @@ describe('class GraphvizInstaller', () => {

beforeEach(() => {
installer = new GraphvizInstaller();
jest.clearAllMocks();
vi.clearAllMocks();
});

describe('Supported platforms', () => {
Expand All @@ -33,7 +34,7 @@ describe('class GraphvizInstaller', () => {
});

it('brewInstall method called on "darwin" platform', async () => {
const brewInstall = jest.fn();
const brewInstall = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(installer as any).brewInstall = brewInstall;

Expand All @@ -44,41 +45,41 @@ describe('class GraphvizInstaller', () => {

describe('inputs works', () => {
test('default', async () => {
mockNamedInputs(getBooleanInput as jest.Mock, 'macos-skip-brew-update', false);
const execSpy = jest.spyOn(exec, 'exec');
mockNamedInputs(getBooleanInput as unknown as MockInstance, 'macos-skip-brew-update', false);
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(2);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"brew",
Array [
[
"update",
],
]
`);
expect(execSpy.mock.calls[1]).toMatchInlineSnapshot(`
Array [
[
"brew",
Array [
[
"install",
"graphviz",
],
]
`);
});
test('skip brew update', async () => {
mockNamedInputs(getBooleanInput as jest.Mock, 'macos-skip-brew-update', true);
const execSpy = jest.spyOn(exec, 'exec');
mockNamedInputs(getBooleanInput as unknown as MockInstance, 'macos-skip-brew-update', true);
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(1);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"brew",
Array [
[
"install",
"graphviz",
],
Expand All @@ -94,7 +95,7 @@ describe('class GraphvizInstaller', () => {
});

it('getAptInstall method called on "linux" platform', async () => {
const getAptInstall = jest.fn();
const getAptInstall = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(installer as any).getAptInstall = getAptInstall;

Expand All @@ -105,16 +106,16 @@ describe('class GraphvizInstaller', () => {

describe('inputs works', () => {
test('skip apt update', async () => {
mockNamedInputs(getBooleanInput as jest.Mock, 'ubuntu-skip-apt-update', true);
const execSpy = jest.spyOn(exec, 'exec');
mockNamedInputs(getBooleanInput as unknown as MockInstance, 'ubuntu-skip-apt-update', true);
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(1);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"install",
"-y",
Expand All @@ -127,26 +128,26 @@ describe('class GraphvizInstaller', () => {
});

test('graphviz version not set', async () => {
(getInput as jest.Mock).mockReturnValue('');
(getBooleanInput as jest.Mock).mockReturnValue(false);
const execSpy = jest.spyOn(exec, 'exec');
(getInput as unknown as MockInstance).mockReturnValue('');
(getBooleanInput as unknown as MockInstance).mockReturnValue(false);
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(2);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"update",
],
]
`);
expect(execSpy.mock.calls[1]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"install",
"-y",
Expand All @@ -159,7 +160,7 @@ describe('class GraphvizInstaller', () => {
});

test('input graphviz version set to "1.1.1" and libgraphviz_dev version set to "2.2.2"', async () => {
(getInput as jest.Mock).mockImplementation((input) => {
(getInput as unknown as MockInstance).mockImplementation((input) => {
switch (input) {
case 'ubuntu-graphviz-version':
return '1.1.1';
Expand All @@ -169,25 +170,25 @@ describe('class GraphvizInstaller', () => {
return '';
}
});
(getBooleanInput as jest.Mock).mockReturnValue(false);
const execSpy = jest.spyOn(exec, 'exec');
(getBooleanInput as unknown as MockInstance).mockReturnValue(false);
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(2);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"update",
],
]
`);
expect(execSpy.mock.calls[1]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"install",
"-y",
Expand All @@ -200,33 +201,33 @@ describe('class GraphvizInstaller', () => {
});

test('input graphviz version set to "3.3.3" and libgraphviz_dev version not set', async () => {
(getInput as jest.Mock).mockImplementation((input) => {
(getInput as unknown as MockInstance).mockImplementation((input) => {
switch (input) {
case 'ubuntu-graphviz-version':
return '3.3.3';
default:
return '';
}
});
(getBooleanInput as jest.Mock).mockReturnValue(false);
const execSpy = jest.spyOn(exec, 'exec');
(getBooleanInput as unknown as MockInstance).mockReturnValue(false);
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(2);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"update",
],
]
`);
expect(execSpy.mock.calls[1]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"install",
"-y",
Expand All @@ -239,33 +240,33 @@ describe('class GraphvizInstaller', () => {
});

test('input graphviz not set and libgraphviz_dev set to 4.4.4', async () => {
(getInput as jest.Mock).mockImplementation((input) => {
(getInput as unknown as MockInstance).mockImplementation((input) => {
switch (input) {
case 'ubuntu-libgraphvizdev-version':
return '4.4.4';
default:
return '';
}
});
(getBooleanInput as jest.Mock).mockReturnValue(false);
const execSpy = jest.spyOn(exec, 'exec');
(getBooleanInput as unknown as MockInstance).mockReturnValue(false);
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(2);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"update",
],
]
`);
expect(execSpy.mock.calls[1]).toMatchInlineSnapshot(`
Array [
[
"sudo",
Array [
[
"apt-get",
"install",
"-y",
Expand All @@ -285,7 +286,7 @@ describe('class GraphvizInstaller', () => {
});

it('chocoInstall method called on "win32" platform', async () => {
const chocoInstall = jest.fn();
const chocoInstall = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(installer as any).chocoInstall = chocoInstall;

Expand All @@ -296,16 +297,16 @@ describe('class GraphvizInstaller', () => {

describe('inputs works', () => {
test('graphviz version not set', async () => {
(getInput as jest.Mock).mockReturnValue('');
const execSpy = jest.spyOn(exec, 'exec');
(getInput as unknown as MockInstance).mockReturnValue('');
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(1);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"choco",
Array [
[
"install",
"graphviz",
],
Expand All @@ -314,16 +315,16 @@ describe('class GraphvizInstaller', () => {
});

test('graphviz version seted to "1.1.1"', async () => {
mockNamedInputs(getInput as jest.Mock, 'windows-graphviz-version', '1.1.1');
const execSpy = jest.spyOn(exec, 'exec');
mockNamedInputs(getInput as unknown as MockInstance, 'windows-graphviz-version', '1.1.1');
const execSpy = vi.spyOn(exec, 'exec');

await installer.get();

expect(execSpy).toBeCalledTimes(1);
expect(execSpy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[
"choco",
Array [
[
"install",
"graphviz",
"--version=1.1.1",
Expand Down
Loading

0 comments on commit d30e381

Please sign in to comment.