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]: fix unit test command & add delay test #828

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"electron": "concurrently --kill-others \"vite dev\" \"electron electron/dist/electron.cjs\"",
"build": "npm run build:main && npm run svelte:build && cp svelte/build/app.html svelte/build/index.html",
"preview": "vite preview",
"unit:test": "vitest",
"coverage": "vitest run --coverage",
"unit:test": "cd svelte && vitest",
"coverage": "cd svelte && vitest run --coverage",
"test": "playwright test",
"check": "cd svelte && svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --compiler-warnings \"css-unused-selector:ignore\"",
"check:watch": "cd svelte && svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --compiler-warnings \"css-unused-selector:ignore\" --watch",
Expand Down
30 changes: 30 additions & 0 deletions svelte/src/libs/utils/delay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, describe, beforeEach, afterEach, test, vi } from "vitest";
import withDelay from "./delay";

describe("withDelay", () => {
beforeEach(() => {
vi.useFakeTimers();
});

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

test("should delay time to execute callback", async () => {
const callback = vi.fn();
const timer = 100;
const promise = withDelay(callback, timer);
expect(callback).not.toHaveBeenCalled();
vi.runAllTimers();
await promise;
expect(callback).toHaveBeenCalled();
});

test("should return the result of callback", async () => {
const result = "result";
const callback = vi.fn().mockImplementation(() => result);
const promise = withDelay(callback, 0);
vi.runAllTimers();
expect(await promise).toEqual(result);
});
});
4 changes: 3 additions & 1 deletion svelte/src/libs/utils/delay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// withDelay adds a delay before calling the provided function.
export const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export default async function withDelay<T>(f: () => T, delayMs: number) {
await new Promise((resolve) => setTimeout(resolve, delayMs));
await wait(delayMs);
return f();
}
3 changes: 1 addition & 2 deletions svelte/src/libs/utils/retry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import log from "$libs/logger";
import { wait } from "./delay";

export type RetryOptions = {
// Number of times to retry. default 10
Expand Down Expand Up @@ -30,5 +31,3 @@ export default async function withRetry<T>(
}
throw new Error(`Failed after ${maxRetries} retries`);
}

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));