Skip to content

Commit

Permalink
test: switch to vitest (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored May 1, 2024
1 parent 3aa83ba commit 8a3ff7a
Show file tree
Hide file tree
Showing 24 changed files with 3,321 additions and 4,218 deletions.
13 changes: 10 additions & 3 deletions .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
env:
node: true
es6: true
jest: true

globals:
Atomics: readonly
Expand All @@ -24,15 +23,23 @@ parserOptions:
- './tsconfig.json'

plugins:
- jest
- import
- vitest
- '@typescript-eslint'

settings:
import/resolver:
typescript:
project:
- './tsconfig.json'
- './__tests__/tsconfig.json'

extends:
- eslint:recommended
- plugin:@typescript-eslint/eslint-recommended
- plugin:@typescript-eslint/recommended
- plugin:github/recommended
- plugin:jest/recommended
- plugin:vitest/legacy-recommended

rules:
{
Expand Down
24 changes: 13 additions & 11 deletions __tests__/add-item.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import * as core from '@actions/core';

import * as index from '../src/add-item';
import { ProjectDetails, addItem, editItem, getProject } from '../src/lib';
import { mockGetInput } from './utils';

jest.mock('@actions/core');
jest.mock('../src/lib');
vi.mock('@actions/core');
vi.mock('../src/lib');

// Spy the action's entrypoint
const addItemActionSpy = jest.spyOn(index, 'addItemAction');
const addItemActionSpy = vi.spyOn(index, 'addItemAction');

const owner = 'dsanders11';
const projectNumber = '94';
Expand All @@ -18,7 +20,7 @@ const itemId = 'item-id';

describe('addItemAction', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('requires the project-number input', async () => {
Expand Down Expand Up @@ -85,7 +87,7 @@ describe('addItemAction', () => {
'project-number': projectNumber,
'content-url': contentUrl
});
jest.mocked(addItem).mockImplementation(() => {
vi.mocked(addItem).mockImplementation(() => {
throw new Error('Server error');
});

Expand All @@ -102,7 +104,7 @@ describe('addItemAction', () => {
'project-number': projectNumber,
'content-url': contentUrl
});
jest.mocked(addItem).mockImplementation(() => {
vi.mocked(addItem).mockImplementation(() => {
throw 42; // eslint-disable-line no-throw-literal
});

Expand All @@ -123,10 +125,10 @@ describe('addItemAction', () => {
field,
'field-value': fieldValue
});
jest.mocked(addItem).mockResolvedValue(itemId);
jest
.mocked(getProject)
.mockResolvedValue({ id: projectId } as ProjectDetails);
vi.mocked(addItem).mockResolvedValue(itemId);
vi.mocked(getProject).mockResolvedValue({
id: projectId
} as ProjectDetails);

await index.addItemAction();
expect(addItemActionSpy).toHaveReturned();
Expand All @@ -143,7 +145,7 @@ describe('addItemAction', () => {
'project-number': projectNumber,
'content-url': contentUrl
});
jest.mocked(addItem).mockResolvedValue(itemId);
vi.mocked(addItem).mockResolvedValue(itemId);

await index.addItemAction();
expect(addItemActionSpy).toHaveReturned();
Expand Down
47 changes: 25 additions & 22 deletions __tests__/archive-item.test.ts → __tests__/archive-item.test.mts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import * as core from '@actions/core';

import * as index from '../src/archive-item';
import { ItemDetails, archiveItem, getItem } from '../src/lib';
import { mockGetBooleanInput, mockGetInput } from './utils';
import * as index from '../src/archive-item.js';
import { ItemDetails, archiveItem, getItem } from '../src/lib.js';
import { mockGetBooleanInput, mockGetInput } from './utils.js';

jest.mock('@actions/core');
jest.mock('../src/lib');
vi.mock('@actions/core');
vi.mock('../src/lib');

const { ProjectNotFoundError } = jest.requireActual('../src/lib');
const { ProjectNotFoundError } =
await vi.importActual<typeof import('../src/lib.js')>('../src/lib');

// Spy the action's entrypoint
const archiveItemActionSpy = jest.spyOn(index, 'archiveItemAction');
const archiveItemActionSpy = vi.spyOn(index, 'archiveItemAction');

const owner = 'dsanders11';
const projectNumber = '94';
Expand All @@ -19,7 +22,7 @@ const itemId = 'item-id';

describe('archiveItemAction', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('requires the project-number input', async () => {
Expand Down Expand Up @@ -49,7 +52,7 @@ describe('archiveItemAction', () => {
it('handles item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': true });
jest.mocked(getItem).mockResolvedValue(null);
vi.mocked(getItem).mockResolvedValue(null);

await index.archiveItemAction();
expect(archiveItemActionSpy).toHaveReturned();
Expand All @@ -61,7 +64,7 @@ describe('archiveItemAction', () => {
it('can ignore item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': false });
jest.mocked(getItem).mockResolvedValue(null);
vi.mocked(getItem).mockResolvedValue(null);

await index.archiveItemAction();
expect(archiveItemActionSpy).toHaveReturned();
Expand All @@ -72,8 +75,8 @@ describe('archiveItemAction', () => {

it('handles project not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
jest.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
jest.mocked(archiveItem).mockImplementation(() => {
vi.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
vi.mocked(archiveItem).mockImplementation(() => {
throw new ProjectNotFoundError();
});

Expand All @@ -86,8 +89,8 @@ describe('archiveItemAction', () => {

it('handles generic errors', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
jest.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
jest.mocked(archiveItem).mockImplementation(() => {
vi.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
vi.mocked(archiveItem).mockImplementation(() => {
throw new Error('Server error');
});

Expand All @@ -100,8 +103,8 @@ describe('archiveItemAction', () => {

it('stringifies non-errors', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
jest.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
jest.mocked(archiveItem).mockImplementation(() => {
vi.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
vi.mocked(archiveItem).mockImplementation(() => {
throw 42; // eslint-disable-line no-throw-literal
});

Expand All @@ -115,8 +118,8 @@ describe('archiveItemAction', () => {
it('passes inputs correctly', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ archived: true });
jest.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
jest.mocked(archiveItem).mockResolvedValue();
vi.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
vi.mocked(archiveItem).mockResolvedValue();

await index.archiveItemAction();
expect(archiveItemActionSpy).toHaveReturned();
Expand All @@ -133,8 +136,8 @@ describe('archiveItemAction', () => {
it('can unarchive an item', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ archived: false });
jest.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
jest.mocked(archiveItem).mockResolvedValue();
vi.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
vi.mocked(archiveItem).mockResolvedValue();

await index.archiveItemAction();
expect(archiveItemActionSpy).toHaveReturned();
Expand All @@ -150,8 +153,8 @@ describe('archiveItemAction', () => {

it('sets output', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
jest.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
jest.mocked(archiveItem).mockResolvedValue();
vi.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
vi.mocked(archiveItem).mockResolvedValue();

await index.archiveItemAction();
expect(archiveItemActionSpy).toHaveReturned();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import * as core from '@actions/core';

import * as index from '../src/close-project';
import { ProjectDetails, closeProject } from '../src/lib';
import { mockGetInput } from './utils';
import * as index from '../src/close-project.js';
import { ProjectDetails, closeProject } from '../src/lib.js';
import { mockGetInput } from './utils.js';

jest.mock('@actions/core');
jest.mock('../src/lib');
vi.mock('@actions/core');
vi.mock('../src/lib');

const { ProjectNotFoundError } = jest.requireActual('../src/lib');
const { ProjectNotFoundError } =
await vi.importActual<typeof import('../src/lib.js')>('../src/lib');

// Spy the action's entrypoint
const closeProjectActionSpy = jest.spyOn(index, 'closeProjectAction');
const closeProjectActionSpy = vi.spyOn(index, 'closeProjectAction');

const owner = 'dsanders11';
const projectNumber = '94';
const projectId = 'project-id';

describe('closeProjectAction', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('requires the project-number input', async () => {
Expand All @@ -35,7 +38,7 @@ describe('closeProjectAction', () => {

it('handles project not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber });
jest.mocked(closeProject).mockImplementation(() => {
vi.mocked(closeProject).mockImplementation(() => {
throw new ProjectNotFoundError();
});

Expand All @@ -48,7 +51,7 @@ describe('closeProjectAction', () => {

it('handles generic errors', async () => {
mockGetInput({ owner, 'project-number': projectNumber });
jest.mocked(closeProject).mockImplementation(() => {
vi.mocked(closeProject).mockImplementation(() => {
throw new Error('Server error');
});

Expand All @@ -61,7 +64,7 @@ describe('closeProjectAction', () => {

it('stringifies non-errors', async () => {
mockGetInput({ owner, 'project-number': projectNumber });
jest.mocked(closeProject).mockImplementation(() => {
vi.mocked(closeProject).mockImplementation(() => {
throw 42; // eslint-disable-line no-throw-literal
});

Expand All @@ -74,9 +77,9 @@ describe('closeProjectAction', () => {

it('sets output', async () => {
mockGetInput({ owner, 'project-number': projectNumber });
jest
.mocked(closeProject)
.mockResolvedValue({ id: projectId } as ProjectDetails);
vi.mocked(closeProject).mockResolvedValue({
id: projectId
} as ProjectDetails);

await index.closeProjectAction();
expect(closeProjectActionSpy).toHaveReturned();
Expand Down
Loading

0 comments on commit 8a3ff7a

Please sign in to comment.