Mengapa kaedah penolakan chai-as-promised saya tidak berfungsi dengan baik?
P粉270891688
P粉270891688 2024-04-02 19:14:00
0
1
412

Saya menggunakan mocha, chai dan promised chai. Ujian sepatutnya gagal tetapi tidak, saya tidak tahu apa yang berlaku, apa-apa cadangan?

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

Saya cuba menguji janji yang patut ditolak, ujian harus gagal, tetapi ujian itu berjaya

P粉270891688
P粉270891688

membalas semua(1)
P粉515066518

Dipetik daripadaChai sebagai dokumen yang Dijanjikan

Anda boleh menggunakan async /await.then(() => {}) untuk memasukkan berbilang Janji dalam ujian anda.

Empat ujian ini akan gagal:

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

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

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan