Warum funktioniert meine Chai-as-promised-Ablehnungsmethode nicht richtig?
P粉270891688
P粉270891688 2024-04-02 19:14:00
0
1
416

Ich verwende Mokka, Chai und versprochenen Chai. Der Test sollte fehlschlagen, tut dies aber nicht. Ich weiß nicht, was falsch läuft, irgendwelche Vorschläge?

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
    })
})

Ich versuche ein Versprechen zu testen, das abgelehnt werden sollte. Der Test sollte fehlschlagen, aber der Test ist erfolgreich

P粉270891688
P粉270891688

Antworte allen(1)
P粉515066518

摘自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

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage