Skip to content

Commit

Permalink
chore: replace jest with vitest in backend project (#2191)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellmueller authored Dec 2, 2024
1 parent eb57ef0 commit 227a8cb
Show file tree
Hide file tree
Showing 12 changed files with 1,896 additions and 3,301 deletions.
4,734 changes: 1,682 additions & 3,052 deletions backend/package-lock.json

Large diffs are not rendered by default.

50 changes: 9 additions & 41 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint:staged": "./node_modules/.bin/lint-staged",
"make-badges": "istanbul-badges-readme --logo=jest --exitCode=1",
"make-badges": "istanbul-badges-readme --logo=vitest --exitCode=1",
"make-badges:ci": "npm run make-badges -- --ci",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage --detectOpenHandles --forceExit",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test": "vitest --dir src",
"test:cov": "vitest run --coverage",
"test:e2e": "vitest --dir test"
},
"dependencies": {
"@nestjs/cli": "^10.4.7",
Expand Down Expand Up @@ -50,11 +48,11 @@
"@swc/cli": "^0.5.0",
"@swc/core": "^1.9.1",
"@types/express": "^5.0.0",
"@types/jest": "^29.0.0",
"@types/node": "^22.0.0",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.0.0",
Expand All @@ -64,48 +62,18 @@
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-promise": "^6.1.1",
"istanbul-badges-readme": "^1.9.0",
"jest": "^29.7.0",
"jest-badges": "^0.1.3",
"jest-sonar-reporter": "^2.0.0",
"lint-staged": "^15.2.4",
"prettier": "^3.0.3",
"prisma": "^5.7.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.1.1",
"ts-loader": "^9.5.0",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.2.2"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverage": true,
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"coveragePathIgnorePatterns": [
"/node_modules/",
"/test/"
],
"coverageReporters": [
"text",
"lcov",
"cobertura",
"jest-badges"
],
"testEnvironment": "node",
"testResultsProcessor": "jest-sonar-reporter"
"typescript": "^5.2.2",
"unplugin-swc": "^1.5.1",
"vitest": "^2.1.8",
"vitest-sonar-reporter": "^2.0.0"
},
"lint-staged": {
"*.{js,ts}": "./node_modules/.bin/eslint --cache --fix"
Expand Down
27 changes: 13 additions & 14 deletions backend/src/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {NestExpressApplication} from '@nestjs/platform-express';
import {bootstrap} from "./app";
jest.mock('prom-client', () => ({
Registry: jest.fn().mockImplementation(() => ({
})),
collectDefaultMetrics: jest.fn().mockImplementation(() => ({
})),
import { NestExpressApplication } from "@nestjs/platform-express";
import { bootstrap } from "./app";

vi.mock("prom-client", () => ({
Registry: vi.fn().mockImplementation(() => ({})),
collectDefaultMetrics: vi.fn().mockImplementation(() => ({})),
}));
jest.mock('express-prom-bundle', () => ({
default: jest.fn().mockImplementation(() => ({
})),
vi.mock("express-prom-bundle", () => ({
default: vi.fn().mockImplementation(() => ({})),
}));
jest.mock('./prom', () => ({
metricsMiddleware: jest.fn().mockImplementation((req, res, next) => next()),
vi.mock("./prom", () => ({
metricsMiddleware: vi.fn().mockImplementation((_req, _res, next) => next()),
}));
describe('main', () => {

describe("main", () => {
let app: NestExpressApplication;

beforeAll(async () => {
Expand All @@ -24,7 +23,7 @@ describe('main', () => {
await app.close();
});

it('should start the application', async () => {
it("should start the application", async () => {
expect(app).toBeDefined();
});
});
15 changes: 7 additions & 8 deletions backend/src/common/logger.config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {customLogger} from './logger.config';
import { customLogger } from "./logger.config";

describe('CustomLogger', () => {
it('should be defined', () => {
describe("CustomLogger", () => {
it("should be defined", () => {
expect(customLogger).toBeDefined();
});

it('should log a message', () => {

const spy = jest.spyOn(customLogger, 'verbose');
customLogger.verbose('Test message');
expect(spy).toHaveBeenCalledWith('Test message');
it("should log a message", () => {
const spy = vi.spyOn(customLogger, "verbose");
customLogger.verbose("Test message");
expect(spy).toHaveBeenCalledWith("Test message");
spy.mockRestore();
});
});
33 changes: 17 additions & 16 deletions backend/src/middleware/req.res.logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Test} from '@nestjs/testing';
import {HTTPLoggerMiddleware} from './req.res.logger';
import {Request, Response} from 'express';
import {Logger} from '@nestjs/common';
import { Test } from "@nestjs/testing";
import { HTTPLoggerMiddleware } from "./req.res.logger";
import { Request, Response } from "express";
import { Logger } from "@nestjs/common";

describe('HTTPLoggerMiddleware', () => {
describe("HTTPLoggerMiddleware", () => {
let middleware: HTTPLoggerMiddleware;
let logger: Logger;

Expand All @@ -15,28 +15,29 @@ describe('HTTPLoggerMiddleware', () => {
middleware = module.get<HTTPLoggerMiddleware>(HTTPLoggerMiddleware);
logger = module.get<Logger>(Logger);
});
it('should log the correct information', () => {
it("should log the correct information", () => {
const request: Request = {
method: 'GET',
originalUrl: '/test',
get: () => 'Test User Agent'
method: "GET",
originalUrl: "/test",
get: () => "Test User Agent",
} as unknown as Request;

const response: Response = {
statusCode: 200,
get: () => '100',
get: () => "100",
on: (event: string, cb: () => void) => {
if (event === 'finish') {
if (event === "finish") {
cb();
}
}
},
} as unknown as Response;

const loggerSpy = jest.spyOn(middleware['logger'], 'log');
const loggerSpy = vi.spyOn(middleware["logger"], "log");

middleware.use(request, response, () => {
});
middleware.use(request, response, () => {});

expect(loggerSpy).toHaveBeenCalledWith(`GET /test 200 100 - Test User Agent`);
expect(loggerSpy).toHaveBeenCalledWith(
`GET /test 200 100 - Test User Agent`,
);
});
});
4 changes: 2 additions & 2 deletions backend/src/middleware/req.res.logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export class HTTPLoggerMiddleware implements NestMiddleware {

response.on("finish", () => {
const { statusCode } = response;
const contentLength = response.get("content-length") || '-';
const contentLength = response.get("content-length") || "-";
const hostedHttpLogFormat = `${method} ${originalUrl} ${statusCode} ${contentLength} - ${request.get(
"user-agent"
"user-agent",
)}`;
this.logger.log(hostedHttpLogFormat);
});
Expand Down
Loading

0 comments on commit 227a8cb

Please sign in to comment.