Skip to content

Commit

Permalink
chore(deps): update to Vitest v0.31.0 and fix test breaking changes (#…
Browse files Browse the repository at this point in the history
…598)

* chore(deps): update to Vitest v0.31.0 and fix test breaking changes
  • Loading branch information
ghiscoding authored May 4, 2023
1 parent 48f8ad6 commit cacb405
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 111 deletions.
11 changes: 0 additions & 11 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@
packageNames: ['nx'],
allowedVersions: '15.4.8', // newer version seems to cause problems with Vitest
},
{
packageNames: ['vitest'],
allowedVersions: '0.30.1',
},
{
packageNames: ['@vitest/coverage-c8'],
allowedVersions: '0.30.1',
},
],
ignoreDeps: [
'vite', // Vite is installed only because required by Vitest VSCode debugger to work, see open issue: https://github.com/vitest-dev/vitest/issues/3112
],
schedule: ['on Thursday'],
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"@types/yargs": "^17.0.24",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"@vitest/coverage-c8": "^0.30.1",
"@vitest/coverage-c8": "^0.31.0",
"conventional-changelog-conventionalcommits": "^5.0.0",
"cross-env": "^7.0.3",
"eslint": "^8.39.0",
Expand Down Expand Up @@ -111,8 +111,7 @@
"tslib": "^2.5.0",
"tsm": "^2.3.0",
"typescript": "^5.0.4",
"vite": "^4.2.1",
"vitest": "^0.30.1",
"vitest": "^0.31.0",
"write-json-file": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';

const cpuCount = 42;
const cpuCount = vi.hoisted(() => 42);

vi.mock('../child-process', async () => ({
...(await vi.importActual<any>('../child-process')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ vi.mock('../../describe-ref');
vi.mock('../lib/has-tags');
vi.mock('../lib/make-diff-predicate');

const globMock = vi.fn();
const { globMock } = vi.hoisted(() => ({ globMock: vi.fn() }));
vi.mock('globby', async () => ({
...(await vi.importActual<any>('globby')),
globbySync: globMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ vi.mock('../../../child-process');
// mocked modules
import * as childProcesses from '../../../child-process';

const globMock = vi.fn();
const { globMock } = vi.hoisted(() => ({ globMock: vi.fn() }));
vi.mock('globby', async () => ({
...(await vi.importActual<any>('globby')),
globbySync: globMock,
Expand Down
3 changes: 1 addition & 2 deletions packages/diff/src/__tests__/lib/get-last-commit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, expect, it, Mock, vi } from 'vitest';

const execSyncMock = vi.fn();

const { execSyncMock } = vi.hoisted(() => ({ execSyncMock: vi.fn() }));
vi.mock('@lerna-lite/core', async () => ({
...(await vi.importActual<any>('@lerna-lite/core')), // return the other real methods, below we'll mock only 2 of the methods
execSync: execSyncMock,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeAll, expect, test, vi } from 'vitest';

const mockNotice = vi.fn();
const { mockNotice } = vi.hoisted(() => ({ mockNotice: vi.fn() }));
vi.mock('npmlog', async () => ({
...(await vi.importActual<any>('npmlog')),
notice: mockNotice,
Expand Down
43 changes: 19 additions & 24 deletions packages/version/src/__tests__/version-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,26 @@ vi.mock('../conventional-commits/get-commits-since-last-release', async () => aw
// also point to the local version command so that all mocks are properly used even by the command-runner
vi.mock('@lerna-lite/version', async () => await vi.importActual('../version-command'));

let collectUpdatesActual: any = null;
vi.mock('@lerna-lite/core', async (coreOriginal) => {
const mod = (await coreOriginal()) as any;
collectUpdatesActual = mod.collectUpdates;
return {
...mod,
Command: (await vi.importActual<any>('../../../core/src/command')).Command,
conf: (await vi.importActual<any>('../../../core/src/command')).conf,
logOutput: (await vi.importActual<any>('../../../core/src/__mocks__/output')).logOutput,
collectUpdates: (await vi.importActual<any>('../../../core/src/__mocks__/collect-updates')).collectUpdates,
promptConfirmation: (await vi.importActual<any>('../../../core/src/__mocks__/prompt')).promptConfirmation,
promptSelectOne: (await vi.importActual<any>('../../../core/src/__mocks__/prompt')).promptSelectOne,
promptTextInput: (await vi.importActual<any>('../../../core/src/__mocks__/prompt')).promptTextInput,
checkWorkingTree: (await vi.importActual<any>('../../../core/src/__mocks__/check-working-tree')).checkWorkingTree,
runTopologically: (await vi.importActual<any>('../../../core/src/utils/run-topologically')).runTopologically,
throwIfReleased: (await vi.importActual<any>('../../../core/src/__mocks__/check-working-tree')).throwIfReleased,
throwIfUncommitted: (await vi.importActual<any>('../../../core/src/__mocks__/check-working-tree')).throwIfUncommitted,
npmConf: (await vi.importActual<any>('../../../core/src/utils/npm-conf')).npmConf,
writeLogFile: (await vi.importActual<any>('../../../core/src/utils/write-log-file')).writeLogFile,
QueryGraph: (await vi.importActual<any>('../../../core/src/utils/query-graph')).QueryGraph,
};
});
vi.mock('@lerna-lite/core', async (coreOriginal) => ({
...((await coreOriginal()) as any),
Command: (await vi.importActual<any>('../../../core/src/command')).Command,
conf: (await vi.importActual<any>('../../../core/src/command')).conf,
logOutput: (await vi.importActual<any>('../../../core/src/__mocks__/output')).logOutput,
collectUpdates: (await vi.importActual<any>('../../../core/src/__mocks__/collect-updates')).collectUpdates,
promptConfirmation: (await vi.importActual<any>('../../../core/src/__mocks__/prompt')).promptConfirmation,
promptSelectOne: (await vi.importActual<any>('../../../core/src/__mocks__/prompt')).promptSelectOne,
promptTextInput: (await vi.importActual<any>('../../../core/src/__mocks__/prompt')).promptTextInput,
checkWorkingTree: (await vi.importActual<any>('../../../core/src/__mocks__/check-working-tree')).checkWorkingTree,
runTopologically: (await vi.importActual<any>('../../../core/src/utils/run-topologically')).runTopologically,
throwIfReleased: (await vi.importActual<any>('../../../core/src/__mocks__/check-working-tree')).throwIfReleased,
throwIfUncommitted: (await vi.importActual<any>('../../../core/src/__mocks__/check-working-tree')).throwIfUncommitted,
npmConf: (await vi.importActual<any>('../../../core/src/utils/npm-conf')).npmConf,
writeLogFile: (await vi.importActual<any>('../../../core/src/utils/write-log-file')).writeLogFile,
QueryGraph: (await vi.importActual<any>('../../../core/src/utils/query-graph')).QueryGraph,
}));
vi.mock('write-pkg', async () => await vi.importActual('../lib/__mocks__/write-pkg'));

import { outputFile, outputJson } from 'fs-extra/esm';
import { outputFile, outputJson } from 'fs-extra';
import { promises as fsPromises } from 'node:fs';
import { dirname, join, resolve as pathResolve } from 'node:path';
import { execa } from 'execa';
Expand Down Expand Up @@ -89,7 +84,7 @@ async function loadYamlFile<T>(filePath: string) {
}

// certain tests need to use the real thing
// const collectUpdatesActual = (await vi.importActual('@lerna-lite/core')).collectUpdates;
const collectUpdatesActual = (await vi.importActual<any>('@lerna-lite/core')).collectUpdates;

// assertion helpers
const listDirty = (cwd) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
vi.mock('@lerna-lite/core');
vi.mock('../get-github-commits');

const execSyncMock = vi.fn();
const describeRefSyncMock = vi.fn();
const { execSyncMock, describeRefSyncMock } = vi.hoisted(() => ({ execSyncMock: vi.fn(), describeRefSyncMock: vi.fn() }));
vi.mock('@lerna-lite/core', async () => ({
...(await vi.importActual<any>('@lerna-lite/core')), // return the other real methods, below we'll mock only 2 of the methods
execSync: execSyncMock,
Expand Down
56 changes: 28 additions & 28 deletions packages/watch/src/__tests__/watch-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@ let watchUnlinkHandler;
let watchUnlinkDirHandler;
let watchChangeHandler;
let watchErrorHandler;
const closeMock = vi.fn();
const watchMock = vi.fn().mockImplementation(() => ({
close: closeMock,
on: vi.fn().mockImplementation(function (this, event, handler) {
switch (event) {
case 'error':
watchErrorHandler = handler;
break;
case 'all':
default:
watchAddHandler = handler;
watchAddDirHandler = handler;
watchUnlinkHandler = handler;
watchUnlinkDirHandler = handler;
watchChangeHandler = handler;
watchErrorHandler = handler;
break;
}
return this;
}),
}));
const { closeMock } = vi.hoisted(() => ({ closeMock: vi.fn() }));

vi.mock('chokidar', () => ({
default: {
watch: watchMock,
watch: vi.fn().mockImplementation(() => ({
close: closeMock,
on: vi.fn().mockImplementation(function (this, event, handler) {
switch (event) {
case 'error':
watchErrorHandler = handler;
break;
case 'all':
default:
watchAddHandler = handler;
watchAddDirHandler = handler;
watchUnlinkHandler = handler;
watchUnlinkDirHandler = handler;
watchChangeHandler = handler;
watchErrorHandler = handler;
break;
}
return this;
}),
})),
},
}));

// also point to the local watch command so that all mocks are properly used even by the command-runner
vi.mock('@lerna-lite/watch', async () => await vi.importActual<any>('../watch-command.js'));

// vi.useFakeTimers({ timerLimit: 100 });

import chokidar from 'chokidar';
import mockStdin from 'mock-stdin';
import { basename, dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('Watch Command', () => {
it('should take glob input option, without slash prefix, and expect it to be appended to the file path being watch by chokidar', async () => {
await lernaWatch(testDir)('--debounce', '0', '--glob', 'src/**/*.{ts,tsx}', '--', 'lerna run build');

expect(watchMock).toHaveBeenCalledWith(
expect(chokidar.watch).toHaveBeenCalledWith(
[join(testDir, 'packages/package-1', '/src/**/*.{ts,tsx}'), join(testDir, 'packages/package-2', '/src/**/*.{ts,tsx}')],
{
ignored: ['**/.git/**', '**/dist/**', '**/node_modules/**'],
Expand All @@ -186,7 +186,7 @@ describe('Watch Command', () => {
it('should take glob input option, with slash prefix, and expect same appended to the file path being watch by chokidar', async () => {
await lernaWatch(testDir)('--debounce', '0', '--glob', '/src/**/*.{ts,tsx}', '--', 'lerna run build');

expect(watchMock).toHaveBeenCalledWith(
expect(chokidar.watch).toHaveBeenCalledWith(
[join(testDir, 'packages/package-1', '/src/**/*.{ts,tsx}'), join(testDir, 'packages/package-2', '/src/**/*.{ts,tsx}')],
{
ignored: ['**/.git/**', '**/dist/**', '**/node_modules/**'],
Expand All @@ -200,7 +200,7 @@ describe('Watch Command', () => {
it('should be able to take --await-write-finish options as a boolean', async () => {
await lernaWatch(testDir)('--debounce', '0', '--await-write-finish', '--', 'lerna run build');

expect(watchMock).toHaveBeenCalledWith([join(testDir, 'packages/package-1'), join(testDir, 'packages/package-2')], {
expect(chokidar.watch).toHaveBeenCalledWith([join(testDir, 'packages/package-1'), join(testDir, 'packages/package-2')], {
ignored: ['**/.git/**', '**/dist/**', '**/node_modules/**'],
ignoreInitial: true,
ignorePermissionErrors: true,
Expand All @@ -212,7 +212,7 @@ describe('Watch Command', () => {
it('should take options prefixed with "awf" (awfPollInterval) and transform them into a valid chokidar "awaitWriteFinish" option', async () => {
await lernaWatch(testDir)('--debounce', '0', '--awf-poll-interval', '500', '--', 'lerna run build');

expect(watchMock).toHaveBeenCalledWith([join(testDir, 'packages/package-1'), join(testDir, 'packages/package-2')], {
expect(chokidar.watch).toHaveBeenCalledWith([join(testDir, 'packages/package-1'), join(testDir, 'packages/package-2')], {
ignored: ['**/.git/**', '**/dist/**', '**/node_modules/**'],
ignoreInitial: true,
ignorePermissionErrors: true,
Expand All @@ -224,7 +224,7 @@ describe('Watch Command', () => {
it('should take options prefixed with "awf" (awfStabilityThreshold) and transform them into a valid chokidar "awaitWriteFinish" option', async () => {
await lernaWatch(testDir)('--debounce', '0', '--awf-stability-threshold', '275', '--', 'lerna run build');

expect(watchMock).toHaveBeenCalledWith([join(testDir, 'packages/package-1'), join(testDir, 'packages/package-2')], {
expect(chokidar.watch).toHaveBeenCalledWith([join(testDir, 'packages/package-1'), join(testDir, 'packages/package-2')], {
ignored: ['**/.git/**', '**/dist/**', '**/node_modules/**'],
ignoreInitial: true,
ignorePermissionErrors: true,
Expand Down
Loading

0 comments on commit cacb405

Please sign in to comment.