搜尋

首頁  >  問答  >  主體

為什麼我的 chai-as-promised 拒絕方法不能正常運作?

我使用摩卡、柴和承諾的柴。 測試應該失敗,但沒有,我不知道出了什麼問題,有什麼建議嗎?

const { describe, it } = require('mocha')
const chai = require('chai')
const { expect } = require('chai')
const chaiAsPromised = require('chai-as-promised')

chai.use(chaiAsPromised)

describe('test', () => {
    it('must be rejected', async () => {
        expect(Promise.resolve('success')).to.rejected
    })
})

我試著測試一個應該被拒絕的承諾,測試應該失敗,但測試成功了

P粉270891688P粉270891688260 天前415

全部回覆(1)我來回復

  • P粉515066518

    P粉5150665182024-04-03 00:15:38

    摘自Chai as Promised 文件

    #

    您可以使用 async /await.then(() => {}) 在測試中包含多個 Promise。

    這四個測試將會失敗:

    const { describe, it } = require('mocha');
    const chai = require('chai');
    const { expect } = require('chai');
    const chaiAsPromised = require('chai-as-promised');
    
    chai.use(chaiAsPromised);
    
    describe('test', () => {
      it('must be rejected (1)', () => {
        return expect(Promise.resolve('success')).to.eventually.be.rejected;
      });
    
      it('must be rejected (2)', (done) => {
        expect(Promise.resolve('success')).to.eventually.be.rejected.notify(done);
      });
    
      it('must be rejected (3)', async () => {
        await expect(Promise.resolve('success1')).to.eventually.be.fulfilled;
        return expect(Promise.resolve('success2')).to.eventually.be.rejected;
      });
    
      it('must be rejected (4)', () => {
        return expect(Promise.resolve('success1')).to.eventually.be.fulfilled.then(
          () => expect(Promise.resolve('success2')).to.eventually.be.rejected
        );
      });
    });
    

    實例:https://stackblitz.com/edit/node- a7t3tx?file=index.js

    #

    回覆
    0
  • 取消回覆