Skip to content

Commit

Permalink
test(core): migrate tests from jest to vitest (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
gperdomor authored Dec 11, 2024
1 parent dd3e363 commit 97a7f89
Show file tree
Hide file tree
Showing 18 changed files with 1,973 additions and 328 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ Thumbs.db
.nx/workspace-data

test-output

vite.config.*.timestamp*
5 changes: 4 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ module.exports = [
files: ['**/*.json'],
// Override or add rules here
rules: {},
languageOptions: { parser: require('jsonc-eslint-parser') },
languageOptions: {
parser: require('jsonc-eslint-parser'),
},
},

...nx.configs['flat/base'],
...nx.configs['flat/typescript'],
...nx.configs['flat/javascript'],
Expand Down
4 changes: 4 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
},
"@nx/vite:test": {
"cache": true,
"inputs": ["default", "^production"]
}
},
"release": {
Expand Down
2,126 changes: 1,863 additions & 263 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@actions/exec": "1.1.1",
"@actions/github": "6.0.0",
"@actions/io": "1.1.3",
"@nx/devkit": "20.2.1",
"@nx/devkit": "20.2.2",
"@octokit/openapi-types": "22.2.0",
"@renovate/pep440": "1.0.0",
"@types/tmp": "0.2.6",
Expand All @@ -39,11 +39,13 @@
"@commitlint/config-nx-scopes": "19.5.0",
"@commitlint/cz-commitlint": "19.5.0",
"@eslint/js": "^9.8.0",
"@nx/eslint": "20.2.1",
"@nx/eslint-plugin": "20.2.1",
"@nx/jest": "20.2.1",
"@nx/js": "20.2.1",
"@nx/plugin": "20.2.1",
"@nx/eslint": "20.2.2",
"@nx/eslint-plugin": "20.2.2",
"@nx/jest": "20.2.2",
"@nx/js": "20.2.2",
"@nx/plugin": "20.2.2",
"@nx/vite": "20.2.2",
"@nx/web": "20.2.2",
"@prisma/client": "5.18.0",
"@swc-node/register": "1.9.2",
"@swc/cli": "0.3.14",
Expand All @@ -52,6 +54,8 @@
"@types/jest": "29.5.12",
"@types/node": "18.19.43",
"@types/semver": "7.5.8",
"@vitest/coverage-v8": "^1.0.4",
"@vitest/ui": "^1.3.1",
"commitizen": "4.3.1",
"dotenv": "16.4.5",
"eslint": "^9.8.0",
Expand All @@ -63,15 +67,17 @@
"jest-environment-node": "29.7.0",
"lint-staged": "15.2.10",
"mocked-env": "1.3.5",
"nx": "20.2.1",
"nx": "20.2.2",
"prettier": "2.8.8",
"ts-jest": "29.2.4",
"ts-node": "10.9.2",
"tslib": "^2.3.0",
"tsx": "^4.17.0",
"typescript": "5.6.3",
"typescript-eslint": "^8.13.0",
"verdaccio": "^5.31.1"
"verdaccio": "^5.31.1",
"vite": "^5.0.0",
"vitest": "^1.3.1"
},
"workspaces": [
"packages/**",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Run `nx build core` to build the library.

## Running unit tests

Run `nx test core` to execute the unit tests via [Jest](https://jestjs.io).
Run `nx test core` to execute the unit tests via [Vitest](https://vitest.dev/).
2 changes: 1 addition & 1 deletion packages/core/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = [
'@nx/dependency-checks': [
'error',
{
ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs}'],
ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs}', '{projectRoot}/vite.config.{js,ts,mjs,mts}'],
},
],
},
Expand Down
10 changes: 0 additions & 10 deletions packages/core/jest.config.ts

This file was deleted.

11 changes: 9 additions & 2 deletions packages/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"sourceRoot": "packages/core/src",
"projectType": "library",
"tags": ["type:lib", "scope:shared"],
"// targets": "to see all targets run: nx show project core --web",
"targets": {}
"targets": {
"test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"reportsDirectory": "../../coverage/packages/core"
}
}
}
}
4 changes: 0 additions & 4 deletions packages/core/src/lib/get-input-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import * as path from 'node:path';
import { getPosixName } from './get-input';
import { getInputList } from './get-input-list';

beforeEach(() => {
jest.clearAllMocks();
});

describe('getInputList', () => {
it('single line correctly', async () => {
await setInput('foo', 'bar');
Expand Down
15 changes: 4 additions & 11 deletions packages/core/src/lib/get-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import mockedEnv, { RestoreFn } from 'mocked-env';
import * as path from 'node:path';
import { getBooleanInput, getInput, getMultilineInput, getPosixName } from './get-input';

const testEnvVars = {
const testEnvVars: NodeJS.ProcessEnv = {
'my var': '',
'special char var \r\n];': '',
'my var2': '',
Expand Down Expand Up @@ -47,16 +46,14 @@ describe('getPosixName', () => {
});

describe('getInputs', () => {
let restore: RestoreFn;
beforeEach(() => {
restore = mockedEnv({
...testEnvVars,
Object.keys(testEnvVars).forEach((env) => {
vi.stubEnv(env, testEnvVars[env] as string);
});
});

afterEach(() => {
jest.restoreAllMocks();
restore();
vi.unstubAllEnvs();
});

describe('getInput', () => {
Expand All @@ -80,10 +77,6 @@ describe('getInputs', () => {
expect(getInput('My InPuT')).toBe('val');
});

// it('getInput handles special characters', () => {
// expect(getInput('special chars_\'\t"\\')).toBe('\'\t"\\ response');
// });

it('getInput handles multiple spaces', () => {
expect(getInput('multiple spaces variable')).toBe('I have multiple spaces');
});
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/lib/interpolate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import mockedEnv, { RestoreFn } from 'mocked-env';
import { vi } from 'vitest';
import { interpolate } from './interpolate';

describe('String interpolation', () => {
let restore: RestoreFn;

beforeAll(() => {
restore = mockedEnv({
TAG1: 'main',
TAG2: '1.0.0',
SUFFIX: '-beta.1',
});
vi.stubEnv('TAG1', 'main');
vi.stubEnv('TAG2', '1.0.0');
vi.stubEnv('SUFFIX', '-beta.1');
});

afterAll(() => {
restore();
vi.unstubAllEnvs();
});

test.each([
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/load-package.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { loadPackage } from './load-package';

describe('loadPackage', () => {
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => true);
vi.spyOn(console, 'error').mockImplementation(() => true);
});

afterEach(() => {
jest.restoreAllMocks();
vi.restoreAllMocks();
});

describe('when package is available', () => {
Expand All @@ -23,7 +23,7 @@ describe('loadPackage', () => {
});

it('should call process.exit if package not exists', async () => {
const mockExit = jest.spyOn(process, 'exit').mockImplementation(jest.fn() as never);
const mockExit = vi.spyOn(process, 'exit').mockImplementation(vi.fn());
await loadPackage('not-existent-package', 'ctx');
expect(mockExit).toHaveBeenCalledWith(1);
});
Expand Down
23 changes: 12 additions & 11 deletions packages/core/src/lib/logging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const chalk = require('chalk');

describe('Logging', () => {
beforeEach(() => {
jest.spyOn(console, 'debug').mockImplementation(() => true);
jest.spyOn(console, 'error').mockImplementation(() => true);
jest.spyOn(console, 'info').mockImplementation(() => true);
jest.spyOn(console, 'log').mockImplementation(() => true);
jest.spyOn(console, 'warn').mockImplementation(() => true);
vi.spyOn(console, 'debug').mockImplementation(() => true);
vi.spyOn(console, 'error').mockImplementation(() => true);
vi.spyOn(console, 'info').mockImplementation(() => true);
vi.spyOn(console, 'log').mockImplementation(() => true);
vi.spyOn(console, 'warn').mockImplementation(() => true);
});

afterEach(() => {
jest.restoreAllMocks();
vi.restoreAllMocks();
});

it('debug should call console debug method', () => {
Expand Down Expand Up @@ -53,13 +53,14 @@ describe('Logging', () => {

describe('startGroup', () => {
it('should use github action syntaxt in Github Actions', () => {
jest.replaceProperty(ci, 'GITHUB_ACTIONS', true);
vi.spyOn(ci, 'GITHUB_ACTIONS', 'get').mockReturnValue(true);

logger.startGroup('this is a group message');
expect(console.info).toHaveBeenCalledWith(`::group::this is a group message${os.EOL}`);
});

it('should use fallback systaxt', () => {
jest.replaceProperty(ci, 'GITHUB_ACTIONS', false);
vi.spyOn(ci, 'GITHUB_ACTIONS', 'get').mockReturnValue(false);

logger.startGroup('this is a group message');
expect(console.info).toHaveBeenCalledWith(
Expand All @@ -70,13 +71,13 @@ describe('Logging', () => {

describe('endGroup', () => {
it('should use github action syntaxt in Github Actions', () => {
jest.replaceProperty(ci, 'GITHUB_ACTIONS', true);
vi.spyOn(ci, 'GITHUB_ACTIONS', 'get').mockReturnValue(true);
logger.endGroup('this is a group message');
expect(console.info).toHaveBeenCalledWith(`::endgroup::${os.EOL}`);
});

it('should use fallback systaxt', () => {
jest.replaceProperty(ci, 'GITHUB_ACTIONS', false);
vi.spyOn(ci, 'GITHUB_ACTIONS', 'get').mockReturnValue(false);

logger.endGroup('this is a group message');
expect(console.info).not.toHaveBeenCalled();
Expand All @@ -85,7 +86,7 @@ describe('Logging', () => {

describe('group wraps an async call in a group', () => {
it('Github Actions', async () => {
jest.replaceProperty(ci, 'GITHUB_ACTIONS', true);
vi.spyOn(ci, 'GITHUB_ACTIONS', 'get').mockReturnValue(true);

const result = await logger.group('mygroup', async () => {
console.info('in my group\n');
Expand Down
15 changes: 14 additions & 1 deletion packages/core/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,18 @@
},
"include": ["src/**/*.ts"],
"references": [],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
"exclude": [
"vite.config.ts",
"vite.config.mts",
"vitest.config.ts",
"vitest.config.mts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx"
]
}
20 changes: 17 additions & 3 deletions packages/core/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./out-tsc/jest",
"types": ["jest", "node"]
"outDir": "./out-tsc/vitest",
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node", "vitest"]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"],
"include": [
"vite.config.ts",
"vite.config.mts",
"vitest.config.ts",
"vitest.config.mts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
],
"references": [
{
"path": "./tsconfig.lib.json"
Expand Down
19 changes: 19 additions & 0 deletions packages/core/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from 'vite';

export default defineConfig({
root: __dirname,
cacheDir: '../../node_modules/.vite/packages/core',
plugins: [],
// Uncomment this if you are using workers.
// worker: {
// plugins: [ nxViteTsPaths() ],
// },
test: {
watch: false,
globals: true,
environment: 'node',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: { reportsDirectory: '../../coverage/packages/core', provider: 'v8' },
},
});
1 change: 1 addition & 0 deletions vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ['**/*/vite.config.{ts,mts}', '**/*/vitest.config.{ts,mts}'];

0 comments on commit 97a7f89

Please sign in to comment.