Nestjs E2E测试:预期201“创建”,“找不到404”。

发布于 2025-02-10 23:20:52 字数 20604 浏览 0 评论 0原文

我是E2E测试和Nestjs的新手。 我正在尝试执行一个简单的E2E测试,但我一直遇到此错误:

AppController(e2e)› /(发布 - 创建新的用户首选项)

expect(received).toBe(expected) // Object.is equality

Expected: 201
Received: 404

  46 |         console.log('res: ', res);
  47 |         // expect(res.body._id).toBeDefined();
> 48 |         expect(res.status).toBe(201);
     |                            ^
  49 |       });
  50 |   });
  51 |

据我所知, ,以及在这里阅读了一些答案之后。造成此问题的主要原因是提供的URL是错误的。

当然,我有一个env.test,并且在E2E运行中配置了测试过程。 我在Mongo Atlas中看到的是,即使我删除mongoose.connection.db.dropdatabase();,它即使我删除了mongoose.connection.db.dropdatabase;,也没有数据。

这是E2E测试文件内容:

import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import * as mongoose from 'mongoose';

describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

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

  beforeAll(() => {
    mongoose.connect(
      `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@${process.env.DB_NAME}.hucjifz.mongodb.net/${process.env.DB_COLLECTION}?retryWrites=true&w=majority`,
      function () {
        mongoose.connection.db.dropDatabase();
      },
    );
  });

      afterAll(() => mongoose.disconnect());

  const eUserIdeMock = '123456';
  const userPreferencesMock = {
    uiTheme: 'dark',
    panelWidth: 300,
    editingHandles: false,
    enableLightboxInEditor: true,
    hiddenElements: true,
    defaultDeviceView: 'mobile',
    exitTo: 'dashboard',
  };
  const data = {
    eUserIdeMock,
    userPreferencesMock,
  };

  it('/ (POST - create new user preferences)', () => {
    return request(app.getHttpServer())
      .post('/api/v1/user-preferences')
      .send(data)
      .expect(201)
      .then((res) => {
        console.log('res: ', res);
        // expect(res.body._id).toBeDefined();
        expect(res.status).toBe(201);
      });
  });

  it('/ (GET)', () => {
    return request(app.getHttpServer())
      .get('/api/v1/user-preferences')
      .send(eUserIdeMock)
      .expect(200);
  });
});

这是我在Postman中使用的API:

{{DOMAIN}}/api/v1/user-preferences

我执行CRUD任务而没有任何问题。 也许这可以有所帮助,这是RES的整个输出:

 <ref *2> Response {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    res: <ref *1> IncomingMessage {
      _readableState: ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: BufferList { head: null, tail: null, length: 0 },
        length: 0,
        pipes: [],
        flowing: true,
        ended: true,
        endEmitted: true,
        reading: false,
        sync: true,
        needReadable: false,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        errorEmitted: false,
        emitClose: true,
        autoDestroy: false,
        destroyed: false,
        errored: null,
        closed: false,
        closeEmitted: false,
        defaultEncoding: 'utf8',
        awaitDrainWriters: null,
        multiAwaitDrain: false,
        readingMore: true,
        dataEmitted: true,
        decoder: [StringDecoder],
        encoding: 'utf8',
        [Symbol(kPaused)]: false
      },
      _events: [Object: null prototype] {
        end: [Array],
        data: [Array],
        error: [Array],
        close: [Function: bound emit]
      },
      _eventsCount: 4,
      _maxListeners: undefined,
      socket: Socket {
        connecting: false,
        _hadError: false,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [WritableState],
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: null,
        _httpMessage: [ClientRequest],
        [Symbol(async_id_symbol)]: 232,
        [Symbol(kHandle)]: [TCP],
        [Symbol(kSetNoDelay)]: true,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBuffer)]: null,
        [Symbol(kBufferCb)]: null,
        [Symbol(kBufferGen)]: null,
        [Symbol(kCapture)]: false,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(RequestTimeout)]: undefined
      },
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: {
        'x-powered-by': 'Express',
        'content-type': 'application/json; charset=utf-8',
        'content-length': '87',
        etag: 'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
        date: 'Mon, 27 Jun 2022 07:40:21 GMT',
        connection: 'close'
      },
      rawHeaders: [
        'X-Powered-By',
        'Express',
        'Content-Type',
        'application/json; charset=utf-8',
        'Content-Length',
        '87',
        'ETag',
        'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
        'Date',
        'Mon, 27 Jun 2022 07:40:21 GMT',
        'Connection',
        'close'
      ],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 404,
      statusMessage: 'Not Found',
      client: Socket {
        connecting: false,
        _hadError: false,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [WritableState],
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: null,
        _httpMessage: [ClientRequest],
        [Symbol(async_id_symbol)]: 232,
        [Symbol(kHandle)]: [TCP],
        [Symbol(kSetNoDelay)]: true,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBuffer)]: null,
        [Symbol(kBufferCb)]: null,
        [Symbol(kBufferGen)]: null,
        [Symbol(kCapture)]: false,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(RequestTimeout)]: undefined
      },
      _consuming: false,
      _dumped: false,
      req: ClientRequest {
        _events: [Object: null prototype],
        _eventsCount: 3,
        _maxListeners: undefined,
        outputData: [],
        outputSize: 0,
        writable: true,
        destroyed: false,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        _defaultKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 208,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Socket],
        _header: 'POST /api/v1/user-preferences HTTP/1.1\r\n' +
          'Host: 127.0.0.1:52789\r\n' +
          'Accept-Encoding: gzip, deflate\r\n' +
          'Content-Type: application/json\r\n' +
          'Content-Length: 208\r\n' +
          'Connection: close\r\n' +
          '\r\n',
        _keepAliveTimeout: 0,
        _onPendingData: [Function: noopPendingOutput],
        agent: [Agent],
        socketPath: undefined,
        method: 'POST',
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        path: '/api/v1/user-preferences',
        _ended: true,
        res: [Circular *1],
        aborted: false,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        reusedSocket: false,
        host: '127.0.0.1',
        protocol: 'http:',
        [Symbol(kCapture)]: false,
        [Symbol(kNeedDrain)]: false,
        [Symbol(corked)]: 0,
        [Symbol(kOutHeaders)]: [Object: null prototype]
      },
      text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
      [Symbol(kCapture)]: false,
      [Symbol(RequestTimeout)]: undefined
    },
    request: Test {
      _events: [Object: null prototype] { abort: [Function (anonymous)] },
      _eventsCount: 1,
      _maxListeners: undefined,
      _enableHttp2: false,
      _agent: false,
      _formData: null,
      method: 'POST',
      url: 'http://127.0.0.1:52789/api/v1/user-preferences',
      _header: { 'content-type': 'application/json' },
      header: { 'Content-Type': 'application/json' },
      writable: true,
      _redirects: 0,
      _maxRedirects: 0,
      cookies: '',
      qs: {},
      _query: [],
      qsRaw: [],
      _redirectList: [],
      _streamRequest: false,
      _lookup: undefined,
      _buffer: true,
      app: Server {
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        _events: [Object: null prototype],
        _eventsCount: 2,
        _maxListeners: undefined,
        _connections: 0,
        _handle: null,
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 0,
        keepAliveTimeout: 5000,
        maxHeadersCount: null,
        headersTimeout: 60000,
        requestTimeout: 0,
        _connectionKey: '6::::0',
        [Symbol(IncomingMessage)]: [Function: IncomingMessage],
        [Symbol(ServerResponse)]: [Function: ServerResponse],
        [Symbol(kCapture)]: false,
        [Symbol(async_id_symbol)]: 230
      },
      _asserts: [],
      _server: Server {
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        _events: [Object: null prototype],
        _eventsCount: 2,
        _maxListeners: undefined,
        _connections: 0,
        _handle: null,
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 0,
        keepAliveTimeout: 5000,
        maxHeadersCount: null,
        headersTimeout: 60000,
        requestTimeout: 0,
        _connectionKey: '6::::0',
        [Symbol(IncomingMessage)]: [Function: IncomingMessage],
        [Symbol(ServerResponse)]: [Function: ServerResponse],
        [Symbol(kCapture)]: false,
        [Symbol(async_id_symbol)]: 230
      },
      _data: { eUserIdeMock: '123456', userPreferencesMock: [Object] },
      req: ClientRequest {
        _events: [Object: null prototype],
        _eventsCount: 3,
        _maxListeners: undefined,
        outputData: [],
        outputSize: 0,
        writable: true,
        destroyed: false,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        _defaultKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 208,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Socket],
        _header: 'POST /api/v1/user-preferences HTTP/1.1\r\n' +
          'Host: 127.0.0.1:52789\r\n' +
          'Accept-Encoding: gzip, deflate\r\n' +
          'Content-Type: application/json\r\n' +
          'Content-Length: 208\r\n' +
          'Connection: close\r\n' +
          '\r\n',
        _keepAliveTimeout: 0,
        _onPendingData: [Function: noopPendingOutput],
        agent: [Agent],
        socketPath: undefined,
        method: 'POST',
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        path: '/api/v1/user-preferences',
        _ended: true,
        res: [IncomingMessage],
        aborted: false,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        reusedSocket: false,
        host: '127.0.0.1',
        protocol: 'http:',
        [Symbol(kCapture)]: false,
        [Symbol(kNeedDrain)]: false,
        [Symbol(corked)]: 0,
        [Symbol(kOutHeaders)]: [Object: null prototype]
      },
      protocol: 'http:',
      host: '127.0.0.1:52789',
      _endCalled: true,
      _callback: [Function (anonymous)],
      _fullfilledPromise: Promise { [Circular *2] },
      res: <ref *1> IncomingMessage {
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 4,
        _maxListeners: undefined,
        socket: [Socket],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        headers: [Object],
        rawHeaders: [Array],
        trailers: {},
        rawTrailers: [],
        aborted: false,
        upgrade: false,
        url: '',
        method: null,
        statusCode: 404,
        statusMessage: 'Not Found',
        client: [Socket],
        _consuming: false,
        _dumped: false,
        req: [ClientRequest],
        text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
        [Symbol(kCapture)]: false,
        [Symbol(RequestTimeout)]: undefined
      },
      _resBuffered: true,
      response: [Circular *2],
      called: true,
      [Symbol(kCapture)]: false
    },
    req: <ref *3> ClientRequest {
      _events: [Object: null prototype] {
        drain: [Function],
        error: [Function (anonymous)],
        prefinish: [Function: requestOnPrefinish]
      },
      _eventsCount: 3,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 208,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: Socket {
        connecting: false,
        _hadError: false,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [WritableState],
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: null,
        _httpMessage: [Circular *3],
        [Symbol(async_id_symbol)]: 232,
        [Symbol(kHandle)]: [TCP],
        [Symbol(kSetNoDelay)]: true,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBuffer)]: null,
        [Symbol(kBufferCb)]: null,
        [Symbol(kBufferGen)]: null,
        [Symbol(kCapture)]: false,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(RequestTimeout)]: undefined
      },
      _header: 'POST /api/v1/user-preferences HTTP/1.1\r\n' +
        'Host: 127.0.0.1:52789\r\n' +
        'Accept-Encoding: gzip, deflate\r\n' +
        'Content-Type: application/json\r\n' +
        'Content-Length: 208\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: Agent {
        _events: [Object: null prototype],
        _eventsCount: 2,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256,
        scheduling: 'lifo',
        maxTotalSockets: Infinity,
        totalSocketCount: 1,
        [Symbol(kCapture)]: false
      },
      socketPath: undefined,
      method: 'POST',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/api/v1/user-preferences',
      _ended: true,
      res: <ref *1> IncomingMessage {
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 4,
        _maxListeners: undefined,
        socket: [Socket],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        headers: [Object],
        rawHeaders: [Array],
        trailers: {},
        rawTrailers: [],
        aborted: false,
        upgrade: false,
        url: '',
        method: null,
        statusCode: 404,
        statusMessage: 'Not Found',
        client: [Socket],
        _consuming: false,
        _dumped: false,
        req: [Circular *3],
        text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
        [Symbol(kCapture)]: false,
        [Symbol(RequestTimeout)]: undefined
      },
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: '127.0.0.1',
      protocol: 'http:',
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype] {
        host: [Array],
        'accept-encoding': [Array],
        'content-type': [Array],
        'content-length': [Array]
      }
    },
    text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
    files: undefined,
    buffered: true,
    headers: {
      'x-powered-by': 'Express',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '87',
      etag: 'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
      date: 'Mon, 27 Jun 2022 07:40:21 GMT',
      connection: 'close'
    },
    header: {
      'x-powered-by': 'Express',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '87',
      etag: 'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
      date: 'Mon, 27 Jun 2022 07:40:21 GMT',
      connection: 'close'
    },
    statusCode: 404,
    status: 404,
    statusType: 4,
    info: false,
    ok: false,
    redirect: false,
    clientError: true,
    serverError: false,
    error: Error: cannot POST /api/v1/user-preferences (404)
        at Response.Object.<anonymous>.Response.toError (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/response.js:110:17)
        at Response.toError [as _setStatusProperties] (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/response-base.js:107:48)
        at new _setStatusProperties (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/response.js:41:8)
        at Test.Object.<anonymous>.Request._emitResponse (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/index.js:952:20)
        at _emitResponse (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/index.js:1153:38)
        at IncomingMessage.fn (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/parsers/json.js:19:7)
        at IncomingMessage.emit (events.js:412:35)
        at endReadableNT (internal/streams/readable.js:1334:12)
        at processTicksAndRejections (internal/process/task_queues.js:82:21) {
      status: 404,
      text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
      method: 'POST',
      path: '/api/v1/user-preferences'
    },
    created: false,
    accepted: false,
    noContent: false,
    badRequest: false,
    unauthorized: false,
    notAcceptable: false,
    forbidden: false,
    notFound: true,
    unprocessableEntity: false,
    type: 'application/json',
    charset: 'utf-8',
    links: {},
    setEncoding: [Function: bound ],
    redirects: [],
    _body: {
      statusCode: 404,
      message: 'Cannot POST /api/v1/user-preferences',
      error: 'Not Found'
    },
    pipe: [Function (anonymous)],
    [Symbol(kCapture)]: false
  }

I am new to e2e tests and nestjs.
I am trying to perform a simple e2e test but I keep getting this error:

AppController (e2e) › / (POST - create new user preferences)

expect(received).toBe(expected) // Object.is equality

Expected: 201
Received: 404

  46 |         console.log('res: ', res);
  47 |         // expect(res.body._id).toBeDefined();
> 48 |         expect(res.status).toBe(201);
     |                            ^
  49 |       });
  50 |   });
  51 |

as far as I understand, and after reading some answers here. the main cause for this issue is that the URL that was provided was wrong.

of course I have an env.test and I have configured the test process on e2e run.
Furthermore I see in mongo atlas that a new collection is created but it contains no data even when I remove the mongoose.connection.db.dropDatabase();.

this is the e2e test file content:

import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import * as mongoose from 'mongoose';

describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

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

  beforeAll(() => {
    mongoose.connect(
      `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@${process.env.DB_NAME}.hucjifz.mongodb.net/${process.env.DB_COLLECTION}?retryWrites=true&w=majority`,
      function () {
        mongoose.connection.db.dropDatabase();
      },
    );
  });

      afterAll(() => mongoose.disconnect());

  const eUserIdeMock = '123456';
  const userPreferencesMock = {
    uiTheme: 'dark',
    panelWidth: 300,
    editingHandles: false,
    enableLightboxInEditor: true,
    hiddenElements: true,
    defaultDeviceView: 'mobile',
    exitTo: 'dashboard',
  };
  const data = {
    eUserIdeMock,
    userPreferencesMock,
  };

  it('/ (POST - create new user preferences)', () => {
    return request(app.getHttpServer())
      .post('/api/v1/user-preferences')
      .send(data)
      .expect(201)
      .then((res) => {
        console.log('res: ', res);
        // expect(res.body._id).toBeDefined();
        expect(res.status).toBe(201);
      });
  });

  it('/ (GET)', () => {
    return request(app.getHttpServer())
      .get('/api/v1/user-preferences')
      .send(eUserIdeMock)
      .expect(200);
  });
});

this is the API I use to in postman:

{{DOMAIN}}/api/v1/user-preferences

and I perform CRUD tasks without any issue.
maybe this can help, this is the entire output of the res:

 <ref *2> Response {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    res: <ref *1> IncomingMessage {
      _readableState: ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: BufferList { head: null, tail: null, length: 0 },
        length: 0,
        pipes: [],
        flowing: true,
        ended: true,
        endEmitted: true,
        reading: false,
        sync: true,
        needReadable: false,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        errorEmitted: false,
        emitClose: true,
        autoDestroy: false,
        destroyed: false,
        errored: null,
        closed: false,
        closeEmitted: false,
        defaultEncoding: 'utf8',
        awaitDrainWriters: null,
        multiAwaitDrain: false,
        readingMore: true,
        dataEmitted: true,
        decoder: [StringDecoder],
        encoding: 'utf8',
        [Symbol(kPaused)]: false
      },
      _events: [Object: null prototype] {
        end: [Array],
        data: [Array],
        error: [Array],
        close: [Function: bound emit]
      },
      _eventsCount: 4,
      _maxListeners: undefined,
      socket: Socket {
        connecting: false,
        _hadError: false,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [WritableState],
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: null,
        _httpMessage: [ClientRequest],
        [Symbol(async_id_symbol)]: 232,
        [Symbol(kHandle)]: [TCP],
        [Symbol(kSetNoDelay)]: true,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBuffer)]: null,
        [Symbol(kBufferCb)]: null,
        [Symbol(kBufferGen)]: null,
        [Symbol(kCapture)]: false,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(RequestTimeout)]: undefined
      },
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: {
        'x-powered-by': 'Express',
        'content-type': 'application/json; charset=utf-8',
        'content-length': '87',
        etag: 'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
        date: 'Mon, 27 Jun 2022 07:40:21 GMT',
        connection: 'close'
      },
      rawHeaders: [
        'X-Powered-By',
        'Express',
        'Content-Type',
        'application/json; charset=utf-8',
        'Content-Length',
        '87',
        'ETag',
        'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
        'Date',
        'Mon, 27 Jun 2022 07:40:21 GMT',
        'Connection',
        'close'
      ],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 404,
      statusMessage: 'Not Found',
      client: Socket {
        connecting: false,
        _hadError: false,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [WritableState],
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: null,
        _httpMessage: [ClientRequest],
        [Symbol(async_id_symbol)]: 232,
        [Symbol(kHandle)]: [TCP],
        [Symbol(kSetNoDelay)]: true,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBuffer)]: null,
        [Symbol(kBufferCb)]: null,
        [Symbol(kBufferGen)]: null,
        [Symbol(kCapture)]: false,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(RequestTimeout)]: undefined
      },
      _consuming: false,
      _dumped: false,
      req: ClientRequest {
        _events: [Object: null prototype],
        _eventsCount: 3,
        _maxListeners: undefined,
        outputData: [],
        outputSize: 0,
        writable: true,
        destroyed: false,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        _defaultKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 208,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Socket],
        _header: 'POST /api/v1/user-preferences HTTP/1.1\r\n' +
          'Host: 127.0.0.1:52789\r\n' +
          'Accept-Encoding: gzip, deflate\r\n' +
          'Content-Type: application/json\r\n' +
          'Content-Length: 208\r\n' +
          'Connection: close\r\n' +
          '\r\n',
        _keepAliveTimeout: 0,
        _onPendingData: [Function: noopPendingOutput],
        agent: [Agent],
        socketPath: undefined,
        method: 'POST',
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        path: '/api/v1/user-preferences',
        _ended: true,
        res: [Circular *1],
        aborted: false,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        reusedSocket: false,
        host: '127.0.0.1',
        protocol: 'http:',
        [Symbol(kCapture)]: false,
        [Symbol(kNeedDrain)]: false,
        [Symbol(corked)]: 0,
        [Symbol(kOutHeaders)]: [Object: null prototype]
      },
      text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
      [Symbol(kCapture)]: false,
      [Symbol(RequestTimeout)]: undefined
    },
    request: Test {
      _events: [Object: null prototype] { abort: [Function (anonymous)] },
      _eventsCount: 1,
      _maxListeners: undefined,
      _enableHttp2: false,
      _agent: false,
      _formData: null,
      method: 'POST',
      url: 'http://127.0.0.1:52789/api/v1/user-preferences',
      _header: { 'content-type': 'application/json' },
      header: { 'Content-Type': 'application/json' },
      writable: true,
      _redirects: 0,
      _maxRedirects: 0,
      cookies: '',
      qs: {},
      _query: [],
      qsRaw: [],
      _redirectList: [],
      _streamRequest: false,
      _lookup: undefined,
      _buffer: true,
      app: Server {
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        _events: [Object: null prototype],
        _eventsCount: 2,
        _maxListeners: undefined,
        _connections: 0,
        _handle: null,
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 0,
        keepAliveTimeout: 5000,
        maxHeadersCount: null,
        headersTimeout: 60000,
        requestTimeout: 0,
        _connectionKey: '6::::0',
        [Symbol(IncomingMessage)]: [Function: IncomingMessage],
        [Symbol(ServerResponse)]: [Function: ServerResponse],
        [Symbol(kCapture)]: false,
        [Symbol(async_id_symbol)]: 230
      },
      _asserts: [],
      _server: Server {
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        _events: [Object: null prototype],
        _eventsCount: 2,
        _maxListeners: undefined,
        _connections: 0,
        _handle: null,
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 0,
        keepAliveTimeout: 5000,
        maxHeadersCount: null,
        headersTimeout: 60000,
        requestTimeout: 0,
        _connectionKey: '6::::0',
        [Symbol(IncomingMessage)]: [Function: IncomingMessage],
        [Symbol(ServerResponse)]: [Function: ServerResponse],
        [Symbol(kCapture)]: false,
        [Symbol(async_id_symbol)]: 230
      },
      _data: { eUserIdeMock: '123456', userPreferencesMock: [Object] },
      req: ClientRequest {
        _events: [Object: null prototype],
        _eventsCount: 3,
        _maxListeners: undefined,
        outputData: [],
        outputSize: 0,
        writable: true,
        destroyed: false,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        _defaultKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 208,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Socket],
        _header: 'POST /api/v1/user-preferences HTTP/1.1\r\n' +
          'Host: 127.0.0.1:52789\r\n' +
          'Accept-Encoding: gzip, deflate\r\n' +
          'Content-Type: application/json\r\n' +
          'Content-Length: 208\r\n' +
          'Connection: close\r\n' +
          '\r\n',
        _keepAliveTimeout: 0,
        _onPendingData: [Function: noopPendingOutput],
        agent: [Agent],
        socketPath: undefined,
        method: 'POST',
        maxHeaderSize: undefined,
        insecureHTTPParser: undefined,
        path: '/api/v1/user-preferences',
        _ended: true,
        res: [IncomingMessage],
        aborted: false,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        reusedSocket: false,
        host: '127.0.0.1',
        protocol: 'http:',
        [Symbol(kCapture)]: false,
        [Symbol(kNeedDrain)]: false,
        [Symbol(corked)]: 0,
        [Symbol(kOutHeaders)]: [Object: null prototype]
      },
      protocol: 'http:',
      host: '127.0.0.1:52789',
      _endCalled: true,
      _callback: [Function (anonymous)],
      _fullfilledPromise: Promise { [Circular *2] },
      res: <ref *1> IncomingMessage {
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 4,
        _maxListeners: undefined,
        socket: [Socket],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        headers: [Object],
        rawHeaders: [Array],
        trailers: {},
        rawTrailers: [],
        aborted: false,
        upgrade: false,
        url: '',
        method: null,
        statusCode: 404,
        statusMessage: 'Not Found',
        client: [Socket],
        _consuming: false,
        _dumped: false,
        req: [ClientRequest],
        text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
        [Symbol(kCapture)]: false,
        [Symbol(RequestTimeout)]: undefined
      },
      _resBuffered: true,
      response: [Circular *2],
      called: true,
      [Symbol(kCapture)]: false
    },
    req: <ref *3> ClientRequest {
      _events: [Object: null prototype] {
        drain: [Function],
        error: [Function (anonymous)],
        prefinish: [Function: requestOnPrefinish]
      },
      _eventsCount: 3,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 208,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: Socket {
        connecting: false,
        _hadError: false,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 7,
        _maxListeners: undefined,
        _writableState: [WritableState],
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: null,
        _httpMessage: [Circular *3],
        [Symbol(async_id_symbol)]: 232,
        [Symbol(kHandle)]: [TCP],
        [Symbol(kSetNoDelay)]: true,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBuffer)]: null,
        [Symbol(kBufferCb)]: null,
        [Symbol(kBufferGen)]: null,
        [Symbol(kCapture)]: false,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(RequestTimeout)]: undefined
      },
      _header: 'POST /api/v1/user-preferences HTTP/1.1\r\n' +
        'Host: 127.0.0.1:52789\r\n' +
        'Accept-Encoding: gzip, deflate\r\n' +
        'Content-Type: application/json\r\n' +
        'Content-Length: 208\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: Agent {
        _events: [Object: null prototype],
        _eventsCount: 2,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256,
        scheduling: 'lifo',
        maxTotalSockets: Infinity,
        totalSocketCount: 1,
        [Symbol(kCapture)]: false
      },
      socketPath: undefined,
      method: 'POST',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/api/v1/user-preferences',
      _ended: true,
      res: <ref *1> IncomingMessage {
        _readableState: [ReadableState],
        _events: [Object: null prototype],
        _eventsCount: 4,
        _maxListeners: undefined,
        socket: [Socket],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        headers: [Object],
        rawHeaders: [Array],
        trailers: {},
        rawTrailers: [],
        aborted: false,
        upgrade: false,
        url: '',
        method: null,
        statusCode: 404,
        statusMessage: 'Not Found',
        client: [Socket],
        _consuming: false,
        _dumped: false,
        req: [Circular *3],
        text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
        [Symbol(kCapture)]: false,
        [Symbol(RequestTimeout)]: undefined
      },
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: '127.0.0.1',
      protocol: 'http:',
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype] {
        host: [Array],
        'accept-encoding': [Array],
        'content-type': [Array],
        'content-length': [Array]
      }
    },
    text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
    files: undefined,
    buffered: true,
    headers: {
      'x-powered-by': 'Express',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '87',
      etag: 'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
      date: 'Mon, 27 Jun 2022 07:40:21 GMT',
      connection: 'close'
    },
    header: {
      'x-powered-by': 'Express',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '87',
      etag: 'W/"57-fJKdAAcKjvI7+1q+kvS8il37LfM"',
      date: 'Mon, 27 Jun 2022 07:40:21 GMT',
      connection: 'close'
    },
    statusCode: 404,
    status: 404,
    statusType: 4,
    info: false,
    ok: false,
    redirect: false,
    clientError: true,
    serverError: false,
    error: Error: cannot POST /api/v1/user-preferences (404)
        at Response.Object.<anonymous>.Response.toError (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/response.js:110:17)
        at Response.toError [as _setStatusProperties] (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/response-base.js:107:48)
        at new _setStatusProperties (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/response.js:41:8)
        at Test.Object.<anonymous>.Request._emitResponse (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/index.js:952:20)
        at _emitResponse (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/index.js:1153:38)
        at IncomingMessage.fn (/Users/davids/Developmet/editor-sync-user-preferences/node_modules/superagent/src/node/parsers/json.js:19:7)
        at IncomingMessage.emit (events.js:412:35)
        at endReadableNT (internal/streams/readable.js:1334:12)
        at processTicksAndRejections (internal/process/task_queues.js:82:21) {
      status: 404,
      text: '{"statusCode":404,"message":"Cannot POST /api/v1/user-preferences","error":"Not Found"}',
      method: 'POST',
      path: '/api/v1/user-preferences'
    },
    created: false,
    accepted: false,
    noContent: false,
    badRequest: false,
    unauthorized: false,
    notAcceptable: false,
    forbidden: false,
    notFound: true,
    unprocessableEntity: false,
    type: 'application/json',
    charset: 'utf-8',
    links: {},
    setEncoding: [Function: bound ],
    redirects: [],
    _body: {
      statusCode: 404,
      message: 'Cannot POST /api/v1/user-preferences',
      error: 'Not Found'
    },
    pipe: [Function (anonymous)],
    [Symbol(kCapture)]: false
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2025-02-17 23:20:52

问题在于,我的API包含两个部分:

  • GlobalPrefix:/api/v1
  • 控制器端点:/user-preferences

apperantley测试只能读取控制器端点。

现在,代码看起来像这样:

  it('/ (POST - create new user preferences)', () => {
    return request(app.getHttpServer())
      .post('/user-preferences') // this is the change that was made!
      .send(data)
      .expect(201)
      .then((res) => {
        console.log('res: ', res);
        // expect(res.body._id).toBeDefined();
        expect(res.status).toBe(201);
      });
  });

而不是这样:

  it('/ (POST - create new user preferences)', () => {
    return request(app.getHttpServer())
      .post('/api/v1/user-preferences')
      .send(data)
      .expect(201)
      .then((res) => {
        console.log('res: ', res);
        // expect(res.body._id).toBeDefined();
        expect(res.status).toBe(201);
      });
  });

The problem was that the my api contain two parts:

  • globalPrefix:/api/v1
  • controller endpoint: /user-preferences

apperantley the test can read only the controller endpoint.

now the code looks like this:

  it('/ (POST - create new user preferences)', () => {
    return request(app.getHttpServer())
      .post('/user-preferences') // this is the change that was made!
      .send(data)
      .expect(201)
      .then((res) => {
        console.log('res: ', res);
        // expect(res.body._id).toBeDefined();
        expect(res.status).toBe(201);
      });
  });

instead of this:

  it('/ (POST - create new user preferences)', () => {
    return request(app.getHttpServer())
      .post('/api/v1/user-preferences')
      .send(data)
      .expect(201)
      .then((res) => {
        console.log('res: ', res);
        // expect(res.body._id).toBeDefined();
        expect(res.status).toBe(201);
      });
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文