javascript - 使用 mocha 和 chaiAsPromised 测试异步函数时出现断言错误

标签 javascript node.js mocha.js chai chai-as-promised

所以我正在尝试测试我的异步函数是否会在我将 s3GetObject = Promise.promisify(s3.getObject.bind(s3)) stub 时抛出错误 blah 但是我发现我的函数不是异步的,它不会抛出错误。

下面是我的 main.js 文件,其中包含 tests.js:

const Promise = require('bluebird');
const AWS = require('aws-sdk');

const s3 = new AWS.S3({
});

const s3GetObject = Promise.promisify(s3.getObject.bind(s3));

async function getS3File(){
  try {
    const contentType = await s3GetObject(s3Params);
    console.log('CONTENT:', contentType);
    return contentType;
  } catch (err) {
    console.log(err);
   throw new Error(err);
  }
};

测试:

    /* eslint-env mocha */
const rewire = require('rewire');
const chai = require('chai');
const sinonChai = require('sinon-chai');
const sinon = require('sinon');
const chaiAsPromised = require('chai-as-promised');

chai.should();
chai.use(sinonChai);
chai.use(chaiAsPromised);

describe('Main', () => {

  describe('getFileFromS3', () => {
    let sut, getS3File, callS3Stub;

    beforeEach(() => {
      sut = rewire('../../main');
      getS3File = sut.__get__('getS3File');
      sinon.spy(console, 'log');
    });

    afterEach(() => {
      console.log.restore();
    });

    it('should be a function', () => {
      getS3File.should.be.a('AsyncFunction');
    });

    describe('with error', () => {
      beforeEach(() => {
        callS3Stub = sinon.stub().rejects('blah');
        sut.__set__('s3GetObject', callS3Stub);
        getS3File = sut.__get__('getS3File');
      });

      it('should error with blah', async () => {
        await getS3File.should.throw();
        //await console.log.should.be.calledWith('blah');

      });
    });
  });
});

我得到的错误是: 1)主要

getFileFromS3 should be a function: AssertionError: expected [Function: getS3File] to be an asyncfunction at Context.it (test\unit\main.spec.js:28:27)

2)主要

getFileFromS3 with error should error with blah: AssertionError: expected [Function: getS3File] to throw an error

UnhandledPromiseRejectionWarning: Error: blah UnhandledPromiseRejectionWarning: Unhandled promise rejection.

这个错误要么是在没有 catch block 的情况下在异步函数中抛出,要么是因为拒绝了一个没有用 .catch() 处理的 promise 。 (拒绝 ID:228)

最佳答案

this answer 中所述,函数是否 async 并不重要,只要它返回一个 promise。 Chai 依依type-detect检测类型并将 async 函数检测为 function

应该是:

getS3File.should.be.a('function');

async 函数是 promise 的语法糖,它们不会抛出错误但会返回被拒绝的 promise。

应该是:

getS3File().should.be.rejectedWith(Error); 

关于javascript - 使用 mocha 和 chaiAsPromised 测试异步函数时出现断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52331180/

相关文章:

javascript - 拖放后设置图像 src

javascript - 关于快速 session 和 cookie,我缺少什么?

node.js - 获取 Azure Blob 存储中可用元素数量的总文件大小

javascript - 如何在不将 JavaScript 放在网站上的情况下对其进行测试?

javascript - 使用嵌套的 then 语句传递值

javascript - ASP.NET 如何让这个 jQuery 跨页面加载/回发持续工作?

javascript - localhost 不输出我使用的路由器和 Controller 的 JSON

unit-testing - 为什么 VueX 存储在多个单元测试中保持状态?

javascript - 如何使用squire模拟内联requirejs依赖项以进行单元测试?

javascript - Chai-As-Promised 即使错误也会通过