Skip to content

Commit

Permalink
chore: Switched out jest for Vitest to get performance gains
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMachin1997 committed Oct 31, 2024
1 parent 0a5fa00 commit 5246ee4
Show file tree
Hide file tree
Showing 15 changed files with 1,586 additions and 2,326 deletions.
16 changes: 6 additions & 10 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
"tsconfigRootDir": ".",
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "sonarjs", "jest", "prettier"],
"plugins": ["@typescript-eslint", "sonarjs", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"airbnb-base",
"airbnb-typescript/base",
"plugin:jest/recommended",
"plugin:sonarjs/recommended",
"plugin:@typescript-eslint/strict-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:prettier/recommended"
],
"ignorePatterns": ["**/node_modules/**", "dist/**", "*.graphql"],
"rules": {
"jest/prefer-expect-assertions": "error",
"jest/no-identical-title": "error",
"jest/valid-expect": "error",
"@typescript-eslint/lines-between-class-members": "off",
"@typescript-eslint/no-throw-literal": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/no-duplicate-enum-values": "off",
// These rules are for reference only.
//#region eslint
"class-methods-use-this": "off",
Expand Down Expand Up @@ -68,11 +69,6 @@
"error",
{ "assertionStyle": "angle-bracket" }
],
"@typescript-eslint/lines-between-class-members": [
"error",
"always",
{ "exceptAfterSingleLine": true }
],
"@typescript-eslint/naming-convention": [
"error",
{ "selector": "default", "format": ["strictCamelCase"] },
Expand Down
51 changes: 26 additions & 25 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ on: [pull_request, push]
jobs:
test:
# Job display name
name: Running the jest tests
name: Running Vitest tests

# Runs on a Linux based OS
runs-on: ubuntu-latest

# Run the job on 2 different versions of Node (12, 14)
# Run the job on Node 18 and 20 which better support modern testing frameworks
strategy:
matrix:
node-version: [12.x, 14.x]
node-version: [18.x, 20.x]

# Steps involved for this particular task
steps:
Expand All @@ -30,35 +30,36 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

# Cache the node_modules
- name: Cache node modules
uses: actions/cache@v2
# Enable Corepack for Yarn
- name: Enable Corepack
run: corepack enable

# Defining the cache env config ie the key
env:
cache-name: cache-node-modules
# Set Yarn version
- name: Set Yarn version
run: yarn set version 4.2.2

# Caching options (https://github.com/actions/cache)
# Cache the yarn dependencies
- name: Cache yarn dependencies
uses: actions/cache@v2
env:
cache-name: cache-yarn-dependencies
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm

# Key for caching the files initally
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}

# Restore keys
path: |
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
${{ runner.os }}-yarn-
# Installs all the project dependencies e.g. prettier, eslint etc via a custom project script
- name: Install Node.js dependencies
run: npm run allDependencies
# Install dependencies using Yarn
- name: Install dependencies
run: yarn install

# Run the react-testing-library tests
# Run the Vitest tests
- name: Run all tests
env:
OPEN_MOVIE_DB_API_URI: ${{ secrets.OPEN_MOVIE_DB_API_URI }}
OPEN_MOVIE_DB_API_KEY: ${{ secrets.OPEN_MOVIE_DB_API_KEY }}
run: npm run test
run: yarn test
Binary file modified .yarn/install-state.gz
Binary file not shown.
41 changes: 13 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"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",
"test:watch": "vitest --watch",
"test:cov": "vitest --coverage",
"test:debug": "vitest --inspect",
"test:e2e": "vitest --config ./test/vitest-e2e.config.ts",
"prepare": "husky install",
"gql-to-interfaces": "cd ./src/scripts && ts-node schema-to-typings.ts"
},
Expand All @@ -33,60 +33,45 @@
"@nestjs/graphql": "^12.1.1",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@swc/core": "^1.7.40",
"@vitest/coverage-v8": "^2.1.4",
"@vitest/eslint-plugin": "^1.1.7",
"axios": "^1.7.2",
"date-fns": "^3.6.0",
"eslint-plugin-vitest": "^0.5.4",
"graphql": "^16.8.1",
"joi": "^17.13.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"ts-morph": "^22.0.0",
"unplugin-swc": "^1.5.1",
"vitest": "^2.1.4",
"zod": "^3.23.8"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^8.12.2",
"@typescript-eslint/parser": "^8.12.2",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-sonarjs": "^0.23.0",
"husky": "^8.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"packageManager": "[email protected]"
}
15 changes: 8 additions & 7 deletions src/core/entertainment/entertainment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { of } from 'rxjs';
import { describe, it, expect, beforeEach, vi } from 'vitest';

import { EntertainmentService } from './entertainment.service';
import {
Expand All @@ -19,21 +20,21 @@ describe('EntertainmentService', () => {
let service: EntertainmentService;

const mockHttpService = {
get: jest.fn()
get: vi.fn()
};

const mockConfigService = {
get: jest.fn()
get: vi.fn()
};

const mockUtilsService = {
getFullImageUrlPath: jest.fn(),
getNumberAsPercentage: jest.fn(),
getGender: jest.fn()
getFullImageUrlPath: vi.fn(),
getNumberAsPercentage: vi.fn(),
getGender: vi.fn()
};

const mockSocialsService = {
getSocials: jest.fn()
getSocials: vi.fn()
};

beforeEach(async () => {
Expand All @@ -60,7 +61,7 @@ describe('EntertainmentService', () => {
}).compile();

service = module.get<EntertainmentService>(EntertainmentService);
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should be defined', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/core/socials/socials.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { of } from 'rxjs';
import { describe, it, expect, beforeEach, vi } from 'vitest';

import { SocialsService } from './socials.service';
import { ENTERTAINMENT_TYPES } from '../../graphql/generated/schema';
Expand All @@ -10,11 +11,11 @@ describe('SocialsService', () => {
let service: SocialsService;

const mockHttpService = {
get: jest.fn()
get: vi.fn()
};

const mockConfigService = {
get: jest.fn()
get: vi.fn()
};

beforeEach(async () => {
Expand All @@ -33,7 +34,7 @@ describe('SocialsService', () => {
}).compile();

service = module.get<SocialsService>(SocialsService);
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should be defined', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { describe, it, expect, beforeEach, vi } from 'vitest';

import { DiscoverFilteringService } from './discover-filtering.service';
import { ENTERTAINMENT_TYPES } from '../../../graphql/generated/schema';
Expand All @@ -11,20 +12,20 @@ describe('DiscoverFilteringService', () => {
let service: DiscoverFilteringService;

const mockHttpService = {
get: jest.fn()
get: vi.fn()
};

const mockConfigService = {
get: jest.fn()
get: vi.fn()
};

const mockUtilsService = {
getFullImageUrlPath: jest.fn()
getFullImageUrlPath: vi.fn()
};

const mockFilteringOptionsService = {
getAvailabilityOptions: jest.fn(),
getReleaseTypeOptions: jest.fn()
getAvailabilityOptions: vi.fn(),
getReleaseTypeOptions: vi.fn()
};

beforeEach(async () => {
Expand Down
Loading

0 comments on commit 5246ee4

Please sign in to comment.