Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: switch to vitest #127

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions jest.json

This file was deleted.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"roll-node": "DEBUG=roller* node lib/node-cron.js",
"roll-orb": "DEBUG=roller* node lib/orb-cron.js",
"start": "DEBUG=roller* probot run ./lib/index.js",
"test": "jest --config=jest.json --coverage"
"test": "vitest run --coverage --reporter=verbose"
},
"repository": {
"type": "git",
Expand All @@ -28,15 +28,14 @@
"homepage": "https://github.com/electron/roller#readme",
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/jest": "^29.5.14",
"@types/node": "^22.9.0",
"@types/semver": "^7.5.8",
"@vitest/coverage-v8": "3.0.2",
"husky": "^9.1.6",
"jest": "^29.7.0",
"lint-staged": "^15.2.10",
"prettier": "^3.3.3",
"ts-jest": "^29.2.5",
"typescript": "^5.6.3"
"typescript": "^5.6.3",
"vitest": "^3.0.2"
},
"dependencies": {
"@octokit/auth-app": "^6.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/chromium-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as debug from 'debug';
import debug from 'debug';

import { MAIN_BRANCH, REPOS, ROLL_TARGETS } from './constants';
import { compareChromiumVersions } from './utils/compare-chromium-versions';
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as debug from 'debug';
import debug from 'debug';
import { Context, Probot } from 'probot';
import { IssueCommentCreatedEvent, PullRequestClosedEvent } from '@octokit/webhooks-types';
import { handleNodeCheck } from './node-handler';
Expand Down
2 changes: 1 addition & 1 deletion src/node-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as debug from 'debug';
import debug from 'debug';
import * as semver from 'semver';

import { MAIN_BRANCH, REPOS, ROLL_TARGETS } from './constants';
Expand Down
2 changes: 1 addition & 1 deletion src/orb-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as debug from 'debug';
import debug from 'debug';

import * as semver from 'semver';
import { ORB_TARGETS, OrbTarget, REPO_OWNER } from './constants';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/roll-orb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as debug from 'debug';
import debug from 'debug';
import * as yamljs from 'yaml';

import { ORB_KEY, OrbTarget, Repository } from '../constants';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/roll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as debug from 'debug';
import debug from 'debug';
import * as semver from 'semver';

import {
Expand Down
80 changes: 41 additions & 39 deletions tests/handlers-spec.ts → tests/handlers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { MAIN_BRANCH, REPOS, ROLL_TARGETS } from '../src/constants';
import * as orbHandler from '../src/orb-handler';
import * as rollOrb from '../src/utils/roll-orb';
Expand All @@ -9,26 +11,26 @@ import { getOctokit } from '../src/utils/octokit';
import { roll } from '../src/utils/roll';
import { getLatestLTSVersion } from '../src/utils/get-nodejs-lts';

jest.mock('../src/utils/get-chromium-tags');
jest.mock('../src/utils/octokit');
jest.mock('../src/utils/roll');
jest.mock('../src/utils/get-nodejs-lts');
vi.mock('../src/utils/get-chromium-tags');
vi.mock('../src/utils/octokit');
vi.mock('../src/utils/roll');
vi.mock('../src/utils/get-nodejs-lts');

describe('handleChromiumCheck()', () => {
let mockOctokit: any;

beforeEach(() => {
mockOctokit = {
paginate: jest.fn(),
paginate: vi.fn(),
repos: {
listBranches: {
endpoint: {
merge: jest.fn(),
merge: vi.fn(),
},
},
getContent: jest.fn(),
get: jest.fn(),
getBranch: jest.fn().mockReturnValue({
getContent: vi.fn(),
get: vi.fn(),
getBranch: vi.fn().mockReturnValue({
data: {
name: MAIN_BRANCH,
commit: {
Expand All @@ -38,7 +40,7 @@ describe('handleChromiumCheck()', () => {
}),
},
};
(getOctokit as jest.Mock).mockReturnValue(mockOctokit);
vi.mocked(getOctokit).mockReturnValue(mockOctokit);
});

describe('release branches', () => {
Expand All @@ -58,7 +60,7 @@ describe('handleChromiumCheck()', () => {
sha: '1234',
},
});
(getChromiumReleases as jest.Mock).mockReturnValue([
vi.mocked(getChromiumReleases).mockResolvedValue([
{
time: 1577869261000,
version: '1.1.0.0',
Expand Down Expand Up @@ -167,7 +169,7 @@ describe('handleChromiumCheck()', () => {
mockOctokit.repos.getBranch.mockReturnValue(null);

const invalid = 'i-am-not-a-valid-release-branch';
expect(handleChromiumCheck(invalid)).rejects.toThrow(
await expect(handleChromiumCheck(invalid)).rejects.toThrow(
'One or more upgrade checks failed - see logs for more details',
);
});
Expand Down Expand Up @@ -224,7 +226,7 @@ describe('handleChromiumCheck()', () => {
});

it('updates to main', async () => {
(getChromiumReleases as jest.Mock).mockReturnValue([
vi.mocked(getChromiumReleases).mockResolvedValue([
{
time: 1577869261000,
version: '1.1.0.0',
Expand Down Expand Up @@ -259,7 +261,7 @@ describe('handleChromiumCheck()', () => {
});

it('takes no action if main is already in DEPS', async () => {
(getChromiumReleases as jest.Mock).mockReturnValue([
vi.mocked(getChromiumReleases).mockResolvedValue([
{
time: 1577869261000,
version: '1.1.0.0',
Expand Down Expand Up @@ -298,7 +300,7 @@ describe('handleChromiumCheck()', () => {
sha: '1234',
},
});
(getChromiumReleases as jest.Mock).mockReturnValue([
vi.mocked(getChromiumReleases).mockResolvedValue([
{
time: 1577869261000,
version: '1.1.0.0',
Expand All @@ -322,7 +324,7 @@ describe('handleChromiumCheck()', () => {
},
]);

(roll as jest.Mock).mockImplementationOnce(() => {
vi.mocked(roll).mockImplementationOnce(() => {
throw new Error('');
});
await expect(handleChromiumCheck()).rejects.toThrowError(
Expand All @@ -337,17 +339,17 @@ describe('handleNodeCheck()', () => {

beforeEach(() => {
mockOctokit = {
paginate: jest.fn().mockReturnValue([]),
paginate: vi.fn().mockReturnValue([]),
repos: {
getBranch: jest.fn().mockReturnValue({
getBranch: vi.fn().mockReturnValue({
data: {
name: MAIN_BRANCH,
commit: {
sha: '1234',
},
},
}),
listReleases: jest.fn().mockReturnValue({
listReleases: vi.fn().mockReturnValue({
data: [
{
tag_name: 'v11.2.0',
Expand All @@ -363,19 +365,19 @@ describe('handleNodeCheck()', () => {
},
],
}),
getContent: jest.fn(),
getContent: vi.fn(),
listBranches: {
endpoint: {
merge: jest.fn(),
merge: vi.fn(),
},
},
},
};
(getOctokit as jest.Mock).mockReturnValue(mockOctokit);
vi.mocked(getOctokit).mockReturnValue(mockOctokit);
});

it('rolls even major versions of Node.js with latest minor/patch update', async () => {
(getLatestLTSVersion as jest.Mock).mockReturnValue('14.0.0');
vi.mocked(getLatestLTSVersion).mockResolvedValue('14.0.0');

mockOctokit.paginate.mockReturnValue([
{
Expand Down Expand Up @@ -420,7 +422,7 @@ describe('handleNodeCheck()', () => {
});

it('does not roll for uneven major versions of Node.js', async () => {
(getLatestLTSVersion as jest.Mock).mockReturnValue('12.0.0');
vi.mocked(getLatestLTSVersion).mockResolvedValue('12.0.0');

mockOctokit.repos.getContent.mockReturnValue({
data: {
Expand All @@ -434,7 +436,7 @@ describe('handleNodeCheck()', () => {
});

it('does not roll if no newer release found', async () => {
(getLatestLTSVersion as jest.Mock).mockReturnValue('12.0.0');
vi.mocked(getLatestLTSVersion).mockResolvedValue('12.0.0');

mockOctokit.repos.getContent.mockReturnValue({
data: {
Expand All @@ -448,7 +450,7 @@ describe('handleNodeCheck()', () => {
});

it('throws error if roll() process failed', async () => {
(getLatestLTSVersion as jest.Mock).mockReturnValue('14.0.0');
vi.mocked(getLatestLTSVersion).mockResolvedValue('14.0.0');

mockOctokit.repos.getContent.mockReturnValue({
data: {
Expand All @@ -457,7 +459,7 @@ describe('handleNodeCheck()', () => {
},
});

(roll as jest.Mock)
vi.mocked(roll)
.mockImplementationOnce(() => {
throw new Error('');
})
Expand Down Expand Up @@ -490,9 +492,9 @@ describe('node-orb', () => {
};

beforeEach(() => {
// mockReturnValue getReleveantReposList to return a list of repos that use the node orb
jest.spyOn(rollOrb, 'rollOrb').mockImplementation(async () => {});
jest.spyOn(orbHandler, 'getRelevantReposList').mockImplementation(async () => {
// mockReturnValue getRelevantReposList to return a list of repos that use the node orb
vi.spyOn(rollOrb, 'rollOrb').mockImplementation(async () => {});
vi.spyOn(orbHandler, 'getRelevantReposList').mockImplementation(async () => {
return [
{
repo: 'fiddle',
Expand All @@ -506,7 +508,7 @@ describe('node-orb', () => {
});

mockOctokit = {
paginate: jest.fn().mockReturnValue([
paginate: vi.fn().mockReturnValue([
{
name: 'fiddle',
owner: 'electron',
Expand All @@ -524,31 +526,31 @@ describe('node-orb', () => {
},
]),
pulls: {
create: jest.fn().mockReturnValue({ data: { html_url: 'https://google.com' } }),
create: vi.fn().mockReturnValue({ data: { html_url: 'https://google.com' } }),
},
git: {
createRef: jest.fn(),
createRef: vi.fn(),
},
repos: {
get: jest.fn().mockReturnValue({ data: { default_branch: 'main' } }),
getContent: jest.fn().mockReturnValue({
get: vi.fn().mockReturnValue({ data: { default_branch: 'main' } }),
getContent: vi.fn().mockReturnValue({
data: {
type: 'file',
content: Buffer.from('orbs:\n node: electronjs/[email protected]\n').toString('base64'),
},
}),
createOrUpdateFileContents: jest.fn(),
getLatestRelease: jest.fn().mockReturnValue({
createOrUpdateFileContents: vi.fn(),
getLatestRelease: vi.fn().mockReturnValue({
data: {
tag_name: '2.0.0',
},
}),
getBranch: jest.fn().mockReturnValue({
getBranch: vi.fn().mockReturnValue({
data: branch,
}),
},
};
(getOctokit as jest.Mock).mockReturnValue(mockOctokit);
vi.mocked(getOctokit).mockReturnValue(mockOctokit);
});

it('rolls relevant repos only', async () => {
Expand Down
10 changes: 6 additions & 4 deletions tests/mocks/octokit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { vi } from 'vitest';

export const mockOctokit = {
git: {
createRef: jest.fn(),
getRef: jest.fn(),
createRef: vi.fn(),
getRef: vi.fn(),
},
repos: {
getContent: jest.fn(),
updateFile: jest.fn(),
getContent: vi.fn(),
updateFile: vi.fn(),
},
};
3 changes: 0 additions & 3 deletions tests/setup.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, expect, it, vi } from 'vitest';

import { getOrbPRText } from '../../src/utils/pr-text-orb';
import { ORB_TARGETS } from '../../src/constants';

jest.mock('../../src/utils/octokit');
vi.mock('../../src/utils/octokit');

describe('getOrbPRText', () => {
describe('node-orb target', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { describe, expect, it, vi } from 'vitest';

import { getPRText } from '../../src/utils/pr-text';
import { ROLL_TARGETS, RollTarget } from '../../src/constants';

jest.mock('../../src/utils/octokit');
vi.mock('../../src/utils/octokit');

describe('getPRText()', () => {
describe('Node.js target', () => {
Expand Down
Loading
Loading