Skip to content

Commit

Permalink
gut jest from ott-server (#1328)
Browse files Browse the repository at this point in the history
* do large find/replaces for switching to vitest

* fix type errors

* fix more types

* add import

* fix native modules not working with vitest

* add more imports

* move redis mock

* Revert "move redis mock"

This reverts commit f80e122.

* update types

* fix type error

* set up redis mock correctly

* change hook to isolate tests better

* make tests that touch the db more isolated: accountrecovery and user specs

* make tests that touch the db more isolated: room and roommanager specs

* update coverage excludes

* finish making all tests that touch the db isolated

* remove jest config

* fix test isolation again?

* fix type errors

* fix lints

* remove jest and babel

* fix ott-client linting

* fix jest related type errors in ott-vis and ott-vis-datasource
  • Loading branch information
dyc3 authored Feb 13, 2024
1 parent bbbd245 commit 4218fd8
Show file tree
Hide file tree
Showing 36 changed files with 431 additions and 956 deletions.
2 changes: 1 addition & 1 deletion client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"es6": true,
"browser": true
},
"plugins": ["prettier"],
"plugins": ["prettier", "vitest"],
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"rules": {
"no-console": "error",
Expand Down
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@vitest/coverage-v8": "^1.2.2",
"@vue/eslint-config-typescript": "^11.0.3",
"@vue/test-utils": "2.2.1",
"eslint-plugin-vitest": "0.3.22",
"eslint-plugin-vue": "9.7.0",
"jsdom": "^21.1.0",
"sass": "^1.41.1",
Expand Down
23 changes: 9 additions & 14 deletions client/tests/unit/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
{
"env": {
"jest": true,
"browser": true
},
"plugins": ["jest"],
"plugins": ["vitest"],
"rules": {
"no-console": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/require-await": "off",
"@typescript-eslint/no-floating-promises": "off",

"jest/consistent-test-it": ["error", { "fn": "it" }],
"jest/expect-expect": "warn",
"jest/no-duplicate-hooks": "error",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/no-if": "error",
"jest/no-expect-resolves": "error",
"jest/no-export": "error",
"jest/no-standalone-expect": "error",
"jest/no-truthy-falsy": "error",
"jest/prefer-spy-on": "error",
"jest/require-top-level-describe": "warn"
"vitest/consistent-test-it": ["error", { "fn": "it" }],
"vitest/expect-expect": "warn",
"vitest/no-conditional-in-test": "error",
"vitest/no-duplicate-hooks": "error",
"vitest/no-focused-tests": "error",
"vitest/no-identical-title": "error",
"vitest/prefer-spy-on": "error",
"vitest/require-top-level-describe": "warn"
}
}
4 changes: 2 additions & 2 deletions client/tests/unit/Room.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe.skip("Room UI spec", () => {

it("should render timestamps as 00:00 if there is nothing playing", () => {
store.state.room.currentSource = {};
jest.advanceTimersByTime(1000);
vi.advanceTimersByTime(1000);
const timestamp = wrapper.find(".video-controls .timestamp");
expect(timestamp.exists()).toBe(true);
expect(timestamp.text()).toEqual("00:00");
Expand All @@ -111,7 +111,7 @@ describe.skip("Room UI spec", () => {
it("should render timestamps if there is something playing", () => {
store.state.room.currentSource = { service: "youtube", id: "I3O9J02G67I", length: 10 };
store.state.room.playbackPosition = 3;
jest.advanceTimersByTime(1000);
vi.advanceTimersByTime(1000);
const timestamp = wrapper.find(".video-controls .timestamp");
expect(timestamp.exists()).toBe(true);
expect(timestamp.text()).toEqual("00:03");
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
"winston": "^3.10.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@types/jest": "29.5.0",
"@types/lodash": "^4.14.170",
"@types/node": "^16.11.7",
"@types/uuid": "^3.4.0",
Expand All @@ -58,14 +56,11 @@
"eslint": "7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-cypress": "^2.12.1",
"eslint-plugin-jest": "23.13.2",
"eslint-plugin-prettier": "^4.0.0",
"faker": "^5.5.3",
"jest": "29.5.0",
"prettier": "^2.5.1",
"start-server-and-test": "^1.12.3",
"supertest": "6.3.3",
"ts-jest": "29.1.1",
"ts-node": "^10.9.1"
}
}
6 changes: 5 additions & 1 deletion packages/ott-vis-datasource/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "./.config/tsconfig.json"
"extends": "./.config/tsconfig.json",
"compilerOptions": {
"strict": true,
"typeRoots": ["../../node_modules/@types"]
}
}
3 changes: 2 additions & 1 deletion packages/ott-vis/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./.config/tsconfig.json",
"compilerOptions": {
"strict": true
"strict": true,
"typeRoots": ["../../node_modules/@types"]
}
}
7 changes: 0 additions & 7 deletions server/babel.config.cjs

This file was deleted.

10 changes: 4 additions & 6 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "tsc",
"lint": "tsc --noEmit && eslint --fix .",
"lint-ci": "tsc --noEmit && eslint .",
"test": "jest --config ./tests/unit/jest.config.js -experimental-vm-modules --runInBand --detectOpenHandles --forceExit --coverage"
"test": "vitest run --coverage"
},
"dependencies": {
"@divine/synchronization": "^1.2.1",
Expand Down Expand Up @@ -48,8 +48,6 @@
"ws": "^7.4.6"
},
"devDependencies": {
"@babel/core": "^7.20.2",
"@babel/plugin-transform-modules-commonjs": "^7.19.6",
"@types/convict": "^6.1.1",
"@types/express": "^4.17.11",
"@types/express-session": "^1.17.3",
Expand All @@ -60,10 +58,10 @@
"@types/sequelize": "^4.28.9",
"@types/supertest": "^2.0.11",
"@types/ws": "^7.4.4",
"babel-jest": "29.5.0",
"jest-watch-typeahead": "^2.2.1",
"eslint-plugin-vitest": "0.3.22",
"nodemon": "^2.0.4",
"redis-mock": "^0.56.3",
"sqlite3": "5.1.5"
"sqlite3": "5.1.5",
"vitest": "^1.2.2"
}
}
25 changes: 10 additions & 15 deletions server/tests/unit/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
{
"env": {
"jest": true,
"browser": true
},
"plugins": ["jest"],
"plugins": ["vitest"],
"rules": {
"no-console": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/require-await": "off",
"@typescript-eslint/no-floating-promises": "off",

"jest/consistent-test-it": ["error", { "fn": "it" }],
"jest/expect-expect": "warn",
"jest/no-duplicate-hooks": "error",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/no-if": "error",
"jest/no-expect-resolves": "error",
"jest/no-export": "error",
"jest/no-standalone-expect": "error",
"jest/no-truthy-falsy": "error",
"jest/prefer-spy-on": "error",
"jest/require-top-level-describe": "warn"
"vitest/consistent-test-it": ["error", { "fn": "it" }],
"vitest/expect-expect": "warn",
"vitest/no-conditional-in-test": "error",
"vitest/no-duplicate-hooks": "error",
"vitest/no-focused-tests": "error",
"vitest/no-identical-title": "error",
"vitest/no-standalone-expect": "error",
"vitest/prefer-spy-on": "error",
"vitest/require-top-level-describe": "warn"
}
}
22 changes: 19 additions & 3 deletions server/tests/unit/api/accountrecovery.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import {
describe,
it,
expect,
beforeAll,
beforeEach,
afterAll,
afterEach,
vi,
MockInstance,
} from "vitest";
import request from "supertest";
import { main } from "../../../app";
import usermanager from "../../../usermanager";
import { User as UserModel } from "../../../models";
import { User } from "../../../models/user";
import { MockMailer } from "server/mailer";
import { conf } from "../../../ott-config";
import { redisClient } from "../../../redisclient";
Expand All @@ -10,17 +22,19 @@ import { OttApiRequestAccountRecoveryVerify } from "common/models/rest-api";
describe("Account Recovery", () => {
let token;
let app;
let emailUser: User;
let noEmailUser: User;

beforeAll(async () => {
app = (await main()).app;

await usermanager.registerUser({
emailUser = await usermanager.registerUser({
email: "email@localhost",
username: "email user",
password: "test1234",
});

await usermanager.registerUser({
noEmailUser = await usermanager.registerUser({
email: null,
username: "no email user",
password: "test1234",
Expand All @@ -40,7 +54,9 @@ describe("Account Recovery", () => {
});

afterAll(async () => {
await UserModel.destroy({ where: {} });
await emailUser.destroy();
await noEmailUser.destroy();
conf.set("mail.enabled", false);
});

it.each([
Expand Down
26 changes: 19 additions & 7 deletions server/tests/unit/api/announcement.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
import {
describe,
it,
expect,
beforeAll,
beforeEach,
afterAll,
afterEach,
vi,
MockInstance,
} from "vitest";
import request from "supertest";
import { main } from "../../../app";
import { setApiKey } from "../../../admin";
import { ANNOUNCEMENT_CHANNEL } from "../../../../common/constants";
import { redisClient } from "../../../redisclient";
import tokens from "../../../auth/tokens";
import tokens, { type SessionInfo } from "../../../auth/tokens";
import type { AuthToken } from "../../../../common/models/types";

const TEST_API_KEY = "TESTAPIKEY";

describe("Announcements API", () => {
let app;
let publishSpy;
let getSessionInfoSpy;
let validateSpy;
let publishSpy: MockInstance<any, Promise<number>>;
let getSessionInfoSpy: MockInstance<[AuthToken], Promise<SessionInfo>>;
let validateSpy: MockInstance<[AuthToken], Promise<boolean>>;

beforeAll(async () => {
setApiKey(TEST_API_KEY);
getSessionInfoSpy = jest.spyOn(tokens, "getSessionInfo").mockResolvedValue({
getSessionInfoSpy = vi.spyOn(tokens, "getSessionInfo").mockResolvedValue({
isLoggedIn: false,
username: "test",
});
validateSpy = jest.spyOn(tokens, "validate").mockResolvedValue(true);
validateSpy = vi.spyOn(tokens, "validate").mockResolvedValue(true);

app = (await main()).app;
});

beforeEach(() => {
publishSpy = jest.spyOn(redisClient, "publish").mockResolvedValue(1);
publishSpy = vi.spyOn(redisClient, "publish").mockResolvedValue(1);
});

afterEach(() => {
Expand Down
32 changes: 19 additions & 13 deletions server/tests/unit/api/data.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import {
describe,
it,
expect,
beforeAll,
beforeEach,
afterAll,
afterEach,
vi,
MockInstance,
} from "vitest";
import request from "supertest";
import { main } from "../../../app";
import InfoExtract, { AddPreview } from "../../../infoextractor";
Expand All @@ -9,11 +20,11 @@ describe("Data API", () => {
let validateSpy;

beforeAll(async () => {
getSessionInfoSpy = jest.spyOn(tokens, "getSessionInfo").mockResolvedValue({
getSessionInfoSpy = vi.spyOn(tokens, "getSessionInfo").mockResolvedValue({
isLoggedIn: false,
username: "test",
});
validateSpy = jest.spyOn(tokens, "validate").mockResolvedValue(true);
validateSpy = vi.spyOn(tokens, "validate").mockResolvedValue(true);
app = (await main()).app;
});

Expand All @@ -23,9 +34,9 @@ describe("Data API", () => {
});

it("GET /data/previewAdd", async () => {
let resolveQuerySpy = jest
let resolveQuerySpy = vi
.spyOn(InfoExtract, "resolveVideoQuery")
.mockReturnValue(Promise.resolve(new AddPreview([], 0)));
.mockResolvedValue(new AddPreview([], 0));

await request(app)
.get("/api/data/previewAdd")
Expand All @@ -40,14 +51,9 @@ describe("Data API", () => {
});

resolveQuerySpy.mockRestore();
resolveQuerySpy = jest
resolveQuerySpy = vi
.spyOn(InfoExtract, "resolveVideoQuery")
.mockImplementation(
() =>
new Promise((resolve, reject) =>
reject({ name: "UnsupportedServiceException", message: "error message" })
)
);
.mockRejectedValue({ name: "UnsupportedServiceException", message: "error message" });

await request(app)
.get("/api/data/previewAdd")
Expand All @@ -62,7 +68,7 @@ describe("Data API", () => {
});

resolveQuerySpy.mockRestore();
resolveQuerySpy = jest.spyOn(InfoExtract, "resolveVideoQuery").mockImplementation(
resolveQuerySpy = vi.spyOn(InfoExtract, "resolveVideoQuery").mockImplementation(
() =>
new Promise((resolve, reject) =>
reject({
Expand All @@ -85,7 +91,7 @@ describe("Data API", () => {
});

resolveQuerySpy.mockRestore();
resolveQuerySpy = jest
resolveQuerySpy = vi
.spyOn(InfoExtract, "resolveVideoQuery")
.mockImplementation(
() =>
Expand Down
Loading

0 comments on commit 4218fd8

Please sign in to comment.