使用 nestjs 超级测试 e2e:请求不是函数

supertest e2e with nestjs: request is not a function

我尝试为我的简单 NestJS 后端服务引入端到端测试。我提供了一个自定义 userService 和一个用 sinon 模拟的自定义 UserRepository。

这是我的 user.e2e-spec.ts 文件:

import * as request from 'supertest';
import * as sinon from 'sinon';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { UserService } from '../../src/user/user.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { User } from '../../src/user/user.entity';
import { TestUtil } from '../../src/utils/TestUtil';
import { createFakeUser } from '../../src/user/test/userTestUtil';

let sandbox: sinon.SinonSandbox;
let testUtil;

describe('User', () => {
    let app: INestApplication;
    const fakeUser = createFakeUser();
    const userService = { findOne: () => fakeUser };

    beforeAll(async () => {
        sandbox = sinon.createSandbox();
        testUtil = new TestUtil(sandbox);
        const module = await Test.createTestingModule({
            providers: [
                {
                    provide: UserService,
                    useValue: userService,
                },
                {
                    provide: getRepositoryToken(User),
                    useValue: testUtil.getMockRepository().object,
                },
            ],
        }).compile();

        app = module.createNestApplication();
        await app.init();
    });

    it(`/GET user`, () => {
        return request(app.getHttpServer())
            .get('/user/:id')
            .expect(200)
            .expect({
                data: userService.findOne(),
            });
    });

    afterAll(async () => {
        await app.close();
    });
});

这是我的 user.controller.ts:

import { ApiBearerAuth, ApiUseTags } from '@nestjs/swagger';
import { Controller, Get, Param } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.entity';

@ApiUseTags('Users')
@ApiBearerAuth()
@Controller('user')
export class UserController {
    constructor(private readonly userService: UserService) {}

    @Get('/:id')
    findOne(@Param('id') id: number): Promise<User> {
        return this.userService.find(id);
    }
}

我用相同的模式编写了一堆单元测试并且它有效。不知道这个e2e超测有什么问题。

感谢您的帮助!

更新: 这是我收到的错误消息:

TypeError: request is not a function
    at Object.it (/Users/florian/Development/Houzy/nestjs-backend/e2e/user/user.e2e-spec.ts:40:16)
    at Object.asyncFn (/Users/florian/Development/Houzy/nestjs-backend/node_modules/jest-jasmine2/build/jasmine_async.js:124:345)
    at resolve (/Users/florian/Development/Houzy/nestjs-backend/node_modules/jest-jasmine2/build/queue_runner.js:46:12)
    at new Promise (<anonymous>)
    at mapper (/Users/florian/Development/Houzy/nestjs-backend/node_modules/jest-jasmine2/build/queue_runner.js:34:499)
    at promise.then (/Users/florian/Development/Houzy/nestjs-backend/node_modules/jest-jasmine2/build/queue_runner.js:74:39)
    at <anonymous>

将请求导入更改为:

import request from 'supertest';

在您的测试中,将 :id 替换为数字:

it(`/GET user`, () => {
        return request(app.getHttpServer())
            .get('/user/1') // pass here id, not a string
            .expect(200)
            .expect({
                data: userService.findOne(),
        });
});

在控制器中:

 @Get('/:id')
    findOne(@Param('id') id: number): Promise<User> {
        return this.userService.find(id);
 }

现在应该可以了。