Skip to content

Commit

Permalink
Testing using vitest.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Nov 24, 2023
1 parent 29a04bc commit 4d4642e
Show file tree
Hide file tree
Showing 4 changed files with 551 additions and 1,745 deletions.
14 changes: 3 additions & 11 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
"lint": "eslint src",
"build": "rm -rf dist && tsc",
"build:client": "ENV=TEST yarn ts-node src/client.ts && yarn prettier --write ../frontend/src/generated/api-client.ts",
"test": "ENV=TEST yarn jest src/"
"test": "ENV=TEST yarn vitest run"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.2",
"@types/express": "^4.17.21",
"@types/fast-memory-cache": "^2.0.2",
"@types/http-errors": "^2.0.4",
"@types/jest": "^29.5.8",
"@types/node": "^20.9.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
Expand All @@ -27,11 +26,10 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^5.0.1",
"jest": "^29.6.4",
"prettier": "3.1.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"vitest": "^0.34.6"
},
"dependencies": {
"@octokit/auth-app": "^6.0.1",
Expand All @@ -45,11 +43,5 @@
"telegraf": "^4.15.0",
"winston": "^3.11.0",
"zod": "^3.22.4"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"verbose": true,
"forceExit": true
}
}
54 changes: 28 additions & 26 deletions backend/src/debounce.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import { describe, test, expect } from "vitest";
import { debounce } from "./debounce";

describe("Debounce", () => {
const returns: number[] = [];
const stack: number[] = [];

test("should not run function with the same args for specific time", (done) => {
const fn = (n: number) => {
stack.push(n * 2);
return n;
};
const debouncedFn = debounce({
fn,
name: "test",
seconds: 3,
mapper: (n) => `${n}`,
});

const pusher = () => {
[1, 2, 3].forEach((n) => {
returns.push(debouncedFn(n));
test("should not run function with the same args for specific time", () =>
new Promise<void>((done) => {
const fn = (n: number) => {
stack.push(n * 2);
return n;
};
const debouncedFn = debounce({
fn,
name: "test",
seconds: 3,
mapper: (n) => `${n}`,
});
};

pusher();
setTimeout(pusher, 1000); // cached
setTimeout(pusher, 2000); // cached
setTimeout(pusher, 3500); // not cached
setTimeout(() => {
expect(returns).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]);
expect(stack).toEqual([2, 4, 6, 2, 4, 6]);
done();
}, 4500);
});
const pusher = () => {
[1, 2, 3].forEach((n) => {
returns.push(debouncedFn(n));
});
};

pusher();
setTimeout(pusher, 1000); // cached
setTimeout(pusher, 2000); // cached
setTimeout(pusher, 3500); // not cached
setTimeout(() => {
expect(returns).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]);
expect(stack).toEqual([2, 4, 6, 2, 4, 6]);
done();
}, 4500);
}));
});
6 changes: 4 additions & 2 deletions backend/src/endpoints/begin-auth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
jest.mock("mongoose", () => ({
...jest.requireActual("mongoose"),
import { vi, describe, test, expect } from "vitest";

vi.mock("mongoose", async () => ({
...((await vi.importActual("mongoose")) as any).default,
connect: async () => ({
version: "1.2.3.mock",
}),
Expand Down
Loading

0 comments on commit 4d4642e

Please sign in to comment.