From 4b31be62bbd6794596abb49f98c09c84d577e29b Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Thu, 11 Jul 2024 11:11:33 -0300 Subject: [PATCH 1/2] test: setup jest testing and gh workflows --- .github/workflows/test-e2e.yml | 31 +++++++++++++++++++++++++++ .github/workflows/test.yml | 33 ++++++++++++++++------------- .gitignore | 2 +- apps/api/src/api.controller.spec.ts | 15 ++++++++++--- apps/api/test/jest-e2e.json | 18 ++++++++++------ package.json | 7 ++++++ pnpm-lock.yaml | 8 +++++++ 7 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/test-e2e.yml diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml new file mode 100644 index 0000000..d1ef37d --- /dev/null +++ b/.github/workflows/test-e2e.yml @@ -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 + + - name: Run tests + run: pnpm test:e2e diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4e82995..c5d9021 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 + + - name: Run tests + run: pnpm test diff --git a/.gitignore b/.gitignore index 7df48f7..552dd92 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,4 @@ yarn-error.log* .DS_Store *.pem -packages/*/coverage +coverage diff --git a/apps/api/src/api.controller.spec.ts b/apps/api/src/api.controller.spec.ts index 635f08e..c1d1929 100644 --- a/apps/api/src/api.controller.spec.ts +++ b/apps/api/src/api.controller.spec.ts @@ -1,3 +1,4 @@ +import { createMock } from "@golevelup/ts-jest"; import { Test, TestingModule } from "@nestjs/testing"; import { EvmProviderService } from "@packages/providers"; @@ -5,19 +6,27 @@ 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(), + }, + ], }).compile(); apiController = app.get(ApiController); + evmProvider = app.get(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); }); }); }); diff --git a/apps/api/test/jest-e2e.json b/apps/api/test/jest-e2e.json index e9d912f..adbc02a 100644 --- a/apps/api/test/jest-e2e.json +++ b/apps/api/test/jest-e2e.json @@ -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": ["/apps/"], + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "moduleNameMapper": { + "^@packages/providers(|/.*)$": "/libs/providers/src/$1" + } } diff --git a/package.json b/package.json index a3f1656..8304b17 100644 --- a/package.json +++ b/package.json @@ -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", @@ -72,6 +73,12 @@ "**/*.(t|j)s" ], "coverageDirectory": "./coverage", + "coveragePathIgnorePatterns": [ + "/node_modules/", + ".e2e-spec.ts", + ".module.ts", + "main.ts" + ], "testEnvironment": "node", "roots": [ "/apps/", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 324ceaf..9f7385a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -27,6 +27,9 @@ importers: '@commitlint/config-conventional': specifier: 19.2.2 version: 19.2.2 + '@golevelup/ts-jest': + specifier: 0.5.0 + version: 0.5.0 '@ianvs/prettier-plugin-sort-imports': specifier: 4.3.0 version: 4.3.0(prettier@3.0.0) @@ -392,6 +395,9 @@ packages: resolution: {integrity: sha512-6SWlXpWU5AvId8Ac7zjzmIOqMOba/JWY8XZ4A7q7Gn1Vlfg/SFFIlrtHXt9nPn4op9ZPAkl91Jao+QQv3r/ukw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + '@golevelup/ts-jest@0.5.0': + resolution: {integrity: sha512-UniUNOBraDD8vf6QNUPkpWMzhUXBtw40nCHekgBlaHy2p99MDV0aYLp4ZXifiyPOsFmg4BZQGs60lF6EpV7JpA==} + '@humanwhocodes/config-array@0.11.14': resolution: {integrity: sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg==} engines: {node: '>=10.10.0'} @@ -3590,6 +3596,8 @@ snapshots: '@eslint/js@8.42.0': {} + '@golevelup/ts-jest@0.5.0': {} + '@humanwhocodes/config-array@0.11.14': dependencies: '@humanwhocodes/object-schema': 2.0.3 From 8da04a360457efc0fcf5a74d3ed6201ea0ed8668 Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Thu, 11 Jul 2024 11:48:45 -0300 Subject: [PATCH 2/2] fix: add --froze-lockfile to workflows --- .github/workflows/build.yml | 2 +- .github/workflows/lint.yml | 2 +- .github/workflows/main-workflow.yml | 4 ++++ .github/workflows/test-e2e.yml | 2 +- .github/workflows/test.yml | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index adb474f..73b9522 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,7 +26,7 @@ jobs: cache: "pnpm" - name: Install dependencies - run: pnpm install + run: pnpm install --frozen-lockfile - name: Run Build run: pnpm build diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 2a79f53..3f9ffd4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -32,7 +32,7 @@ jobs: cache: "pnpm" - name: Install dependencies - run: pnpm install + run: pnpm install --frozen-lockfile - name: Run Prettier run: pnpm format diff --git a/.github/workflows/main-workflow.yml b/.github/workflows/main-workflow.yml index f93afdc..f207ba4 100644 --- a/.github/workflows/main-workflow.yml +++ b/.github/workflows/main-workflow.yml @@ -22,3 +22,7 @@ jobs: test: uses: ./.github/workflows/test.yml needs: lint + + test-e2e: + uses: ./.github/workflows/test-e2e.yml + needs: lint diff --git a/.github/workflows/test-e2e.yml b/.github/workflows/test-e2e.yml index d1ef37d..553b58d 100644 --- a/.github/workflows/test-e2e.yml +++ b/.github/workflows/test-e2e.yml @@ -25,7 +25,7 @@ jobs: cache: "pnpm" - name: Install dependencies - run: pnpm install + run: pnpm install --frozen-lockfile - name: Run tests run: pnpm test:e2e diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c5d9021..19573dc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: cache: "pnpm" - name: Install dependencies - run: pnpm install + run: pnpm install --frozen-lockfile - name: Run tests run: pnpm test