Skip to content

Commit

Permalink
test: setup jest testing and gh workflows (#21)
Browse files Browse the repository at this point in the history
# 🤖 Linear

Closes ZKS-85 ZKS-86

## Description

Properly configure Jest, install mocking helper and configure GH
Workflows for test and e2e test
  • Loading branch information
0xnigir1 authored Jul 11, 2024
1 parent 7f8cd4c commit 1dd745e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
cache: "pnpm"

- name: Install dependencies
run: pnpm install
run: pnpm install --frozen-lockfile

- name: Run Build
run: pnpm build
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
cache: "pnpm"

- name: Install dependencies
run: pnpm install
run: pnpm install --frozen-lockfile

- name: Run Prettier
run: pnpm format
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/main-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ jobs:
test:
uses: ./.github/workflows/test.yml
needs: lint

test-e2e:
uses: ./.github/workflows/test-e2e.yml
needs: lint
31 changes: 31 additions & 0 deletions .github/workflows/test-e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Tests

on:
workflow_call:

jobs:
unit:
name: Run unit tests
runs-on: ubuntu-latest
steps:
- name: Check out github repository
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Use PNPM
uses: pnpm/action-setup@v4
with:
run_install: false

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Run tests
run: pnpm test:e2e
33 changes: 18 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ jobs:
name: Run unit tests
runs-on: ubuntu-latest
steps:
- name: Log Not Implemented Message
run: echo "Not implemented yet"
# - name: Check out github repository
# uses: actions/checkout@v3
# with:
# fetch-depth: 1
- name: Check out github repository
uses: actions/checkout@v4
with:
fetch-depth: 1

# - name: Use Node.js
# uses: actions/setup-node@v3
# with:
# node-version: 18.x
# cache: "yarn"
- name: Use PNPM
uses: pnpm/action-setup@v4
with:
run_install: false

# - name: Install dependencies
# run: yarn --immutable
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"

# - name: Run tests
# run: yarn test
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Run tests
run: pnpm test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ yarn-error.log*
.DS_Store
*.pem

packages/*/coverage
coverage
15 changes: 12 additions & 3 deletions apps/api/src/api.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { createMock } from "@golevelup/ts-jest";
import { Test, TestingModule } from "@nestjs/testing";
import { EvmProviderService } from "@packages/providers";

import { ApiController } from "./api.controller";

describe("ApiController", () => {
let apiController: ApiController;
let evmProvider: EvmProviderService;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [ApiController],
providers: [EvmProviderService],
providers: [
{
provide: EvmProviderService,
useValue: createMock<EvmProviderService>(),
},
],
}).compile();

apiController = app.get<ApiController>(ApiController);
evmProvider = app.get<EvmProviderService>(EvmProviderService);
});

describe("root", () => {
it("should return 1", async () => {
expect(await apiController.getTvl()).toBe(1);
it("should return 50", async () => {
jest.spyOn(evmProvider, "getTvl").mockResolvedValue(50);
expect(await apiController.getTvl()).toBe(50);
});
});
});
18 changes: 11 additions & 7 deletions apps/api/test/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "../../..",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"roots": ["<rootDir>/apps/"],
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"^@packages/providers(|/.*)$": "<rootDir>/libs/providers/src/$1"
}
}
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"devDependencies": {
"@commitlint/config-conventional": "19.2.2",
"@golevelup/ts-jest": "0.5.0",
"@ianvs/prettier-plugin-sort-imports": "4.3.0",
"@nestjs/cli": "10.0.0",
"@nestjs/schematics": "10.0.0",
Expand Down Expand Up @@ -72,6 +73,12 @@
"**/*.(t|j)s"
],
"coverageDirectory": "./coverage",
"coveragePathIgnorePatterns": [
"/node_modules/",
".e2e-spec.ts",
".module.ts",
"main.ts"
],
"testEnvironment": "node",
"roots": [
"<rootDir>/apps/",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1dd745e

Please sign in to comment.