测试通过,但显示两条错误消息“错误:连接 ECONNREFUSED 127.0.0.1:80”和“在 Socket 实例上发出‘错误’事件”

时间:2021-03-02 10:14:28

标签: node.js typescript mocha.js chai nock

我正在使用 MochaChaiNock 编写单元测试代码来模拟 HTTP 请求。
所有测试都没有问题地通过,但它显示错误“错误:连接 ECONNREFUSED 127.0.0.1:80”和“在套接字实例上发出‘错误’事件”
这是代码:

import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import nock from 'nock';

use(chaiAsPromised);


  beforeEach(() => {
    foo = () => new Foo(`${hostname}/api/lock`);
    nock.disableNetConnect();
  });

  afterEach(() => {
    nock.cleanAll();
    nock.enableNetConnect();
  });

context('lockAsync()', () => {
    it('Should return true', async () => {
      // Arrange
      const foo = foo();

      // Act
      const expiration = new Date('2020/01/01 10:00:00');
      const scope = nock(hostname)
        .post('/api/foo/bar/',  expiration.toDateString())
        .reply(201, 'true');
      const result= await foo.lockAsync('value1', 'value2', 'value3', expiration);
      
      scope.done();

      // Assert
      expect(result).to.be.true;
    });
  });

// Other similar tests below

最后我有两个错误:
enter image description here


异常由:C:\Users\...\node_modules\mocha\lib\runner.js:962

引发
Runner.prototype._uncaught = function(err) {
  // this is defensive to prevent future developers from mis-calling this function.
  // it's more likely that it'd be called with the incorrect context--say, the global
  // `process` object--than it would to be called with a context that is not a "subclass"
  // of `Runner`.

...

 if (this.state === constants.STATE_STOPPED) {
    debug('uncaught(): throwing after run has completed!');
    throw err;
  }
...

我也在其他项目中使用了这个设置,但我没有问题。
有人可以帮助我吗?谢谢。

0 个答案:

没有答案