From 4091339a8372019958f7014623c5eb92bcfe2ac0 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 23 Sep 2021 11:49:03 +0800 Subject: [PATCH] test: add e2e --- .github/workflows/build.yml | 1 + jest.config.js | 8 ++++- package.json | 4 +-- test/app.e2e-spec.ts | 24 -------------- test/jest-e2e.js | 30 +++++++++++++++++ test/jest-e2e.json | 9 ----- test/src/app.controller.e2e-spec.ts | 51 +++++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 36 deletions(-) delete mode 100644 test/app.e2e-spec.ts create mode 100644 test/jest-e2e.js delete mode 100644 test/jest-e2e.json create mode 100644 test/src/app.controller.e2e-spec.ts diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7ada011e..6774e945 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -42,5 +42,6 @@ jobs: yarn build yarn lint yarn test + yarn test:e2e env: CI: true diff --git a/jest.config.js b/jest.config.js index e1740f9b..e2a56d0b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,7 +3,7 @@ const { pathsToModuleNameMapper } = require('ts-jest/utils') // In the following statement, replace `./tsconfig` with the path to your `tsconfig` file // which contains the path mapping (ie the `compilerOptions.paths` option): const { compilerOptions } = require('./tsconfig.json') - +const { cd, $, chalk } = require('zx') module.exports = { moduleFileExtensions: ['js', 'json', 'ts'], rootDir: '.', @@ -14,6 +14,12 @@ module.exports = { collectCoverageFrom: ['**/*.(t|j)s'], coverageDirectory: '../coverage', testEnvironment: 'node', + globals: { + isDev: process.env.NODE_ENV === 'development', + $, + chalk, + cd, + }, moduleNameMapper: { ...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '/' }), '^src/(.*)$': '/src/$1', diff --git a/package.json b/package.json index 31596768..c4e2b486 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "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:e2e": "jest --config ./test/jest-e2e.js", "patch": "node bin/patch.js", "docs": "npx @compodoc/compodoc -p tsconfig.json -s -d docs", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", @@ -162,4 +162,4 @@ "resolutions": { "typescript": "4.4.3" } -} \ No newline at end of file +} diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts deleted file mode 100644 index f46d53e8..00000000 --- a/test/app.e2e-spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { INestApplication } from '@nestjs/common' -import { Test, TestingModule } from '@nestjs/testing' -import request from 'supertest' -import { AppModule } from './../src/app.module' - -describe('AppController (e2e)', () => { - let app: INestApplication - - beforeEach(async () => { - const moduleFixture: TestingModule = await Test.createTestingModule({ - imports: [AppModule], - }).compile() - - app = moduleFixture.createNestApplication() - await app.init() - }) - - it('/ (GET)', () => { - return request(app.getHttpServer()) - .get('/') - .expect(200) - .expect('Hello World!') - }) -}) diff --git a/test/jest-e2e.js b/test/jest-e2e.js new file mode 100644 index 00000000..b02b9223 --- /dev/null +++ b/test/jest-e2e.js @@ -0,0 +1,30 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const { pathsToModuleNameMapper } = require('ts-jest/utils') +// In the following statement, replace `./tsconfig` with the path to your `tsconfig` file +// which contains the path mapping (ie the `compilerOptions.paths` option): +const { compilerOptions } = require('../tsconfig.json') +const { $, chalk, cd } = require('zx') +module.exports = { + moduleFileExtensions: ['js', 'json', 'ts'], + rootDir: '..', + testEnvironment: 'node', + testRegex: '.e2e-spec.ts$', + transform: { + '^.+\\.(t|j)s$': 'ts-jest', + }, + globals: { + isDev: process.env.NODE_ENV === 'development', + $, + chalk, + cd, + }, + moduleNameMapper: { + ...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '/' }), + '^src/(.*)$': '/src/$1', + '^test/(.*)$': '/test/$1', + '^src$': '/src', + '^~/(.*)$': '/src/$1', + '^~$': '/src', + '^test$': '/test', + }, +} diff --git a/test/jest-e2e.json b/test/jest-e2e.json deleted file mode 100644 index e9d912f3..00000000 --- a/test/jest-e2e.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "moduleFileExtensions": ["js", "json", "ts"], - "rootDir": ".", - "testEnvironment": "node", - "testRegex": ".e2e-spec.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - } -} diff --git a/test/src/app.controller.e2e-spec.ts b/test/src/app.controller.e2e-spec.ts new file mode 100644 index 00000000..84339bc3 --- /dev/null +++ b/test/src/app.controller.e2e-spec.ts @@ -0,0 +1,51 @@ +import { NestFastifyApplication } from '@nestjs/platform-fastify' +import { Test } from '@nestjs/testing' +import { getModelToken } from 'nestjs-typegoose' +import { AppController } from '~/app.controller' +import { fastifyApp } from '~/common/adapt/fastify' +import { OptionModel } from '~/modules/configs/configs.model' +import { CacheService } from '~/processors/cache/cache.service' +describe('AppController (e2e)', () => { + let app: NestFastifyApplication + + beforeAll(async () => { + const moduleRef = await Test.createTestingModule({ + controllers: [AppController], + providers: [ + CacheService, + + { + provide: getModelToken(OptionModel.name), + useValue: {}, + }, + ], + }) + .overrideProvider(CacheService) + .useValue({}) + + .compile() + + app = moduleRef.createNestApplication(fastifyApp) + await app.init() + await app.getHttpAdapter().getInstance().ready() + }) + + it('GET /ping', () => { + return app + .inject({ + method: 'GET', + url: '/ping', + }) + .then((res) => { + expect(res.statusCode).toBe(200) + expect(res.payload).toBe('pong') + }) + }) + + it('GET /', () => { + return app.inject({ url: '/' }).then((res) => { + expect(res.statusCode).toBe(200) + expect(res.payload).toBeDefined() + }) + }) +})