Skip to content

Commit

Permalink
✨ (vitest) [NO-ISSUE]: Switch from jest to vitest (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman authored Feb 4, 2025
2 parents 9a97b6f + a2075f4 commit 2e6aa74
Show file tree
Hide file tree
Showing 249 changed files with 10,563 additions and 10,121 deletions.
6 changes: 3 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ updates:
changesets:
patterns:
- "@changesets*"
jest:
vitest:
patterns:
- "jest*"
- "@jest*"
- "vitest*"
- "@vitest*"
typescript:
patterns:
- "typescript*"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/merge_queue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Tests
id: unit-tests
if: ${{ steps.health-check.conclusion == 'success' }}
run: pnpm test -- -- --maxWorkers=50%
run: pnpm test
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Tests
id: unit-tests
if: ${{ steps.health-check.conclusion == 'success' }}
run: pnpm test:coverage -- --maxWorkers=50%
run: pnpm test:coverage

- uses: sonarsource/sonarqube-scan-action@v4
if: ${{ steps.unit-tests.conclusion == 'success' && github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ A brief description of this project packages:
|-------------------------------------------------------|--------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| @ledgerhq/device-management-kit-sample | apps/sample | React Next web app used to test & demonstrate the Web Device Management Kit |
| @ledgerhq/eslint-config-dsdk | packages/config/eslint | internal package which contains eslint shared config. Used by `extends: ["@ledgerhq/dsdk"]` in `.eslintrc`. |
| @ledgerhq/jest-config-dsdk | packages/config/jest | internal package which contains jest shared config. Used by `preset: "@ledgerhq/jest-config-dsdk"` in `jest.config.ts` |
| @ledgerhq/vitest-config-dmk | packages/config/vitest | internal package which contains vitest shared config. Used in `vitest.config.mjs` |
| @ledgerhq/tsconfig-dsdk | packages/config/typescript | internal package which contains typescript shared config. Used by `"extends": "@ledgerhq/tsconfig-dsdk/tsconfig.sdk"` in `tsconfig.json` |
| @ledgerhq/device-management-kit | packages/device-management-kit | external package that contains the core of the Web Device Management Kit |
| @ledgerhq/device-signer-kit-ethereum | packages/signer/signer-eth | external package that contains device ethereum coin application dedicated handlers |
Expand Down
25 changes: 5 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"context-module": "pnpm --filter @ledgerhq/context-module",
"signer-btc": "pnpm --filter @ledgerhq/device-signer-kit-bitcoin",
"signer-eth": "pnpm --filter @ledgerhq/device-signer-kit-ethereum",
"signer-solana": "pnpm --filter @ledgerhq/device-signer-kit-solana",
"signer-sol": "pnpm --filter @ledgerhq/device-signer-kit-solana",
"signer-utils": "pnpm --filter @ledgerhq/signer-utils",
"mockserver-client": "pnpm --filter @ledgerhq/device-mockserver-client",
"trusted-apps": "pnpm --filter @ledgerhq/device-sdk-trusted-apps",
Expand All @@ -46,41 +46,26 @@
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.12",
"@types/jest": "^29.5.14",
"@types/node": "^22.10.1",
"@vitest/coverage-istanbul": "^3.0.4",
"concurrently": "^9.1.0",
"danger": "^12.3.3",
"esbuild": "^0.24.0",
"esbuild-node-externals": "^1.14.0",
"eslint": "9.19.0",
"gitmoji-cli": "^9.5.0",
"hygen": "^6.2.11",
"jest": "^29.7.0",
"jsdom": "^26.0.0",
"prettier": "^3.4.2",
"rimraf": "^6.0.1",
"ts-jest": "^29.2.5",
"tsc-alias": "^1.8.10",
"turbo": "^2.3.3",
"typescript": "^5.7.3",
"vitest": "^3.0.4",
"zx": "^8.2.4"
},
"engines": {
"node": ">=20"
},
"packageManager": "[email protected]",
"pnpm": {
"overrides": {
"ejs@<3.1.10": ">=3.1.10",
"ws@>=7.0.0 <7.5.10": ">=7.5.10",
"elliptic@>=4.0.0 <=6.5.6": ">=6.5.7",
"elliptic@>=2.0.0 <=6.5.6": ">=6.5.7",
"elliptic@>=5.2.1 <=6.5.6": ">=6.5.7",
"micromatch@<4.0.8": ">=4.0.8",
"webpack@>=5.0.0-alpha.0 <5.94.0": ">=5.94.0",
"elliptic@<6.5.6": ">=6.5.6",
"elliptic@<6.6.0": ">=6.6.0",
"cross-spawn@<6.0.6": ">=6.0.6",
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5"
}
}
"packageManager": "[email protected]"
}
7 changes: 0 additions & 7 deletions packages/config/jest/CHANGELOG.md

This file was deleted.

9 changes: 0 additions & 9 deletions packages/config/jest/jest-preset.js

This file was deleted.

11 changes: 0 additions & 11 deletions packages/config/jest/package.json

This file was deleted.

10 changes: 10 additions & 0 deletions packages/config/vitest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
globals: true,
exclude: ["node_modules", "lib"],
printConsoleTrace: true,
silent: false,
},
});
8 changes: 8 additions & 0 deletions packages/config/vitest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@ledgerhq/vitest-config-dmk",
"version": "0.0.0",
"main": "index.js",
"license": "Apache-2.0",
"type": "module",
"private": true
}
2 changes: 1 addition & 1 deletion packages/device-management-kit/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import config from "@ledgerhq/eslint-config-dsdk";
export default [
...config,
{
ignores: ["eslint.config.mjs", "scripts/*.mjs", "lib/*"],
ignores: ["eslint.config.mjs", "vitest.*.mjs", "scripts/*.mjs", "lib/*"],
languageOptions: {
parserOptions: {
project: "./tsconfig.json",
Expand Down
26 changes: 0 additions & 26 deletions packages/device-management-kit/jest.config.ts

This file was deleted.

8 changes: 4 additions & 4 deletions packages/device-management-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"postpack": "find . -name '*.tgz' -exec cp {} ../../dist/ \\; ",
"prettier": "prettier . --check",
"prettier:fix": "prettier . --write",
"test": "jest",
"test:watch": "pnpm test -- --watch",
"test:coverage": "pnpm test -- --coverage",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"typecheck": "tsc --noEmit",
"module:create": "pnpm hygen core-module with-prompt"
},
Expand All @@ -51,9 +51,9 @@
"devDependencies": {
"@ledgerhq/esbuild-tools": "workspace:*",
"@ledgerhq/eslint-config-dsdk": "workspace:*",
"@ledgerhq/jest-config-dsdk": "workspace:*",
"@ledgerhq/prettier-config-dsdk": "workspace:*",
"@ledgerhq/tsconfig-dsdk": "workspace:*",
"@ledgerhq/vitest-config-dmk": "workspace:*",
"@types/semver": "^7.5.8",
"@types/uuid": "^10.0.0",
"@types/ws": "^8.5.13",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { commandTypes } from "./command/di/commandTypes";
import { ConsoleLogger } from "./logger-subscriber/service/ConsoleLogger";
import { DeviceManagementKit } from "./DeviceManagementKit";

jest.mock("./logger-subscriber/service/ConsoleLogger");
vi.mock("./logger-subscriber/service/ConsoleLogger");

let dmk: DeviceManagementKit;
let logger: ConsoleLogger;
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("DeviceManagementKit", () => {
[discoveryTypes.ListConnectedDevicesUseCase],
[discoveryTypes.ListenToConnectedDeviceUseCase],
])(
"should have %p use case",
"should have %s use case",
(diSymbol: interfaces.ServiceIdentifier<StubUseCase>) => {
const uc = dmk.container.get<StubUseCase>(diSymbol);
expect(uc).toBeInstanceOf(StubUseCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ConsoleLogger } from "./logger-subscriber/service/ConsoleLogger";
import { DeviceManagementKit } from "./DeviceManagementKit";
import { DeviceManagementKitBuilder } from "./DeviceManagementKitBuilder";

jest.mock("./logger-subscriber/service/ConsoleLogger");
vi.mock("./logger-subscriber/service/ConsoleLogger");

let builder: DeviceManagementKitBuilder;
let logger: ConsoleLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("ByteArrayParser", () => {

describe("clean", () => {
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
});

it("should create an instance", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("LoadCertificateCommand", () => {

// THEN
if (isSuccessCommandResult(result)) {
fail("Expected error command result");
assert.fail("Expected error command result");
} else {
expect(result.error).toBeInstanceOf(GlobalCommandError);
}
Expand Down Expand Up @@ -106,10 +106,10 @@ describe("LoadCertificateCommand", () => {

// THEN
if (isSuccessCommandResult(result)) {
fail("Expected error command result");
assert.fail("Expected error command result");
} else {
expect(result.error).toBeInstanceOf(LoadCertificateCommandError);
// @ts-ignore
// @ts-expect-error intended
expect(result.error.errorCode).toBe(errorCode);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ describe("SendCommandUseCase", () => {
);
secureChannel = new DefaultSecureChannelService(secureChannelDataSource);
command = {
getApdu: jest.fn(),
parseResponse: jest.fn(),
getApdu: vi.fn(),
parseResponse: vi.fn(),
};
});

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

it("should send a command to a connected device", async () => {
Expand All @@ -58,7 +58,7 @@ describe("SendCommandUseCase", () => {
sessionService.addDeviceSession(deviceSession);
const useCase = new SendCommandUseCase(sessionService, () => logger);

jest.spyOn(deviceSession, "sendCommand").mockResolvedValue({
vi.spyOn(deviceSession, "sendCommand").mockResolvedValue({
status: CommandResultStatus.Success,
data: undefined,
});
Expand All @@ -78,9 +78,9 @@ describe("SendCommandUseCase", () => {

it("should throw an error if the session is not found", async () => {
const useCase = new SendCommandUseCase(sessionService, () => logger);
jest
.spyOn(sessionService, "getDeviceSessionById")
.mockReturnValue(Left({ _tag: "DeviceSessionNotFound" }));
vi.spyOn(sessionService, "getDeviceSessionById").mockReturnValue(
Left({ _tag: "DeviceSessionNotFound" }),
);

const res = useCase.execute<{ status: string }, void, void>({
sessionId: fakeSessionId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { type Mocked } from "vitest";

import { type InternalApi } from "@api/device-action/DeviceAction";

const sendCommandMock = jest.fn();
const apiGetDeviceSessionStateMock = jest.fn();
const apiGetDeviceSessionStateObservableMock = jest.fn();
const setDeviceSessionStateMock = jest.fn();
const getManagerApiServiceMock = jest.fn();
const getSecureChannelServiceMock = jest.fn();
const sendCommandMock = vi.fn();
const apiGetDeviceSessionStateMock = vi.fn();
const apiGetDeviceSessionStateObservableMock = vi.fn();
const setDeviceSessionStateMock = vi.fn();
const getManagerApiServiceMock = vi.fn();
const getSecureChannelServiceMock = vi.fn();

export function makeDeviceActionInternalApiMock(): jest.Mocked<InternalApi> {
export function makeDeviceActionInternalApiMock(): Mocked<InternalApi> {
return {
sendCommand: sendCommandMock,
getDeviceSessionState: apiGetDeviceSessionStateMock,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Left, Right } from "purify-ts";
import { type Mock } from "vitest";
import { assign, createMachine } from "xstate";

import { UserInteractionRequired } from "@api/device-action/model/UserInteractionRequired";
Expand All @@ -13,8 +14,8 @@ import { type BTC_APP } from "./data";
type App = typeof BTC_APP;

export const setupListAppsMock = (apps: App[], error = false) => {
(ListAppsDeviceAction as jest.Mock).mockImplementation(() => ({
makeStateMachine: jest.fn().mockImplementation(() =>
(ListAppsDeviceAction as Mock).mockImplementation(() => ({
makeStateMachine: vi.fn().mockImplementation(() =>
createMachine({
id: "MockListAppsDeviceAction",
initial: "ready",
Expand Down Expand Up @@ -44,8 +45,8 @@ export const setupListAppsMock = (apps: App[], error = false) => {
};

export const setupGoToDashboardMock = (error: boolean = false) => {
(GoToDashboardDeviceAction as jest.Mock).mockImplementation(() => ({
makeStateMachine: jest.fn().mockImplementation(() =>
(GoToDashboardDeviceAction as Mock).mockImplementation(() => ({
makeStateMachine: vi.fn().mockImplementation(() =>
createMachine({
id: "MockGoToDashboardDeviceAction",
initial: "ready",
Expand Down Expand Up @@ -84,15 +85,15 @@ export const setupGetDeviceStatusMock = (
},
],
) => {
const outputFn = jest.fn();
const outputFn = vi.fn();

for (const output of outputs) {
outputFn.mockImplementationOnce(() =>
"currentApp" in output ? Right(output) : Left(output),
);
}
(GetDeviceStatusDeviceAction as jest.Mock).mockImplementation(() => ({
makeStateMachine: jest.fn().mockImplementation(() =>
(GetDeviceStatusDeviceAction as Mock).mockImplementation(() => ({
makeStateMachine: vi.fn().mockImplementation(() =>
createMachine({
id: "MockGetDeviceStatusDeviceAction",
initial: "ready",
Expand Down
Loading

0 comments on commit 2e6aa74

Please sign in to comment.