Skip to content

Commit

Permalink
Migrated MoveState test to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Jan 15, 2025
1 parent a82ad50 commit 0504e3a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { expect, jest, test } from "@jest/globals";
import { mocked } from "jest-mock";
import { expect, test, vi } from "vitest";

import { DriveBackedValue_ } from "../../../src/backend/utils/DriveBackedValue";
import { MoveState_ } from "../../../src/backend/utils/MoveState";
import { mockedDriveBackedValue } from "../../test-utils/DriveBackedValue-stub";
import { mockedSafeDriveService } from "../../test-utils/SafeDriveService-stub";

jest.mock<{ DriveBackedValue_: jest.Mock }>(
"../../../src/backend/utils/DriveBackedValue",
() => ({
DriveBackedValue_: jest.fn(),
}),
);
vi.mock("../../../src/backend/utils/DriveBackedValue");

test("MoveState constructs correctly", () => {
const driveBackedValueMock = mockedDriveBackedValue();
const driveServiceMock = mockedSafeDriveService();
mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);
vi.mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);

const state = new MoveState_(
"SRC_BASE_ID",
Expand All @@ -26,8 +20,8 @@ test("MoveState constructs correctly", () => {
driveServiceMock,
);

expect(mocked(DriveBackedValue_).mock.calls).toHaveLength(1);
expect(mocked(DriveBackedValue_).mock.calls[0][0]).toBe(
expect(vi.mocked(DriveBackedValue_).mock.calls).toHaveLength(1);
expect(vi.mocked(DriveBackedValue_).mock.calls[0][0]).toBe(
JSON.stringify({
copyComments: false,
destinationID: "DEST_BASE_ID",
Expand Down Expand Up @@ -230,7 +224,7 @@ test("MoveState.tryOrLog works correctly", () => {
driveServiceMock,
);

const fn = jest.fn<() => void>().mockReturnValueOnce();
const fn = vi.fn<() => void>().mockReturnValueOnce();

state.tryOrLog(context, fn);

Expand Down Expand Up @@ -275,7 +269,7 @@ test("MoveState.tryOrLog handles errors gracefully", () => {
driveServiceMock,
);

const fn = jest.fn<() => void>().mockImplementationOnce(() => {
const fn = vi.fn<() => void>().mockImplementationOnce(() => {
throw new Error("ERROR_MESSAGE");
});

Expand Down Expand Up @@ -304,7 +298,7 @@ test("MoveState.tryOrLog handles errors gracefully with a filename", () => {
driveServiceMock,
);

const fn = jest.fn<() => void>().mockImplementationOnce(() => {
const fn = vi.fn<() => void>().mockImplementationOnce(() => {
throw new Error("ERROR_MESSAGE");
});

Expand All @@ -321,7 +315,7 @@ test("MoveState.tryOrLog handles errors gracefully with a filename", () => {
test("MoveState saves the state correctly", () => {
const driveBackedValueMock = mockedDriveBackedValue();
const driveServiceMock = mockedSafeDriveService();
mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);
vi.mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);

const state = new MoveState_(
"SRC_BASE_ID",
Expand Down Expand Up @@ -380,7 +374,7 @@ test("MoveState saves the state correctly", () => {
test("MoveState doesn't save empty state", () => {
const driveBackedValueMock = mockedDriveBackedValue();
const driveServiceMock = mockedSafeDriveService();
mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);
vi.mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);

const state = new MoveState_(
"SRC_BASE_ID",
Expand Down Expand Up @@ -414,7 +408,7 @@ test("MoveState loads the state correctly", () => {
pathsToProcess: [path],
});
const driveServiceMock = mockedSafeDriveService();
mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);
vi.mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);

const state = new MoveState_(
"SRC_BASE_ID",
Expand All @@ -435,7 +429,7 @@ test("MoveState handles empty state load correctly", () => {
const driveBackedValueMock = mockedDriveBackedValue();
driveBackedValueMock.loadValue.mockReturnValueOnce(null);
const driveServiceMock = mockedSafeDriveService();
mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);
vi.mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);

const state = new MoveState_(
"SRC_BASE_ID",
Expand All @@ -453,7 +447,7 @@ test("MoveState handles empty state load correctly", () => {
test("MoveState destroys state correctly", () => {
const driveBackedValueMock = mockedDriveBackedValue();
const driveServiceMock = mockedSafeDriveService();
mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);
vi.mocked(DriveBackedValue_).mockReturnValue(driveBackedValueMock);

const state = new MoveState_(
"SRC_BASE_ID",
Expand Down
23 changes: 23 additions & 0 deletions tests/test-utils/DriveBackedValue-stub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type MockedObject, vi } from "vitest";

import type { DriveBackedValue_ } from "../../src/backend/utils/DriveBackedValue";
import type { MoveContext } from "../../src/interfaces/MoveContext";
import type { MoveError } from "../../src/interfaces/MoveError";

export function mockedDriveBackedValue(): MockedObject<
DriveBackedValue_<{
errors: Array<MoveError>;
pathsToProcess: Array<MoveContext>;
}>
> {
return {
deleteValue: vi.fn(),
loadValue: vi.fn(),
saveValue: vi.fn(),
} as unknown as MockedObject<
DriveBackedValue_<{
errors: Array<MoveError>;
pathsToProcess: Array<MoveContext>;
}>
>;
}

0 comments on commit 0504e3a

Please sign in to comment.