Estoy tratando de realizar pruebas de Chai en varias funciones y he encontrado una especie de obstáculo. La función particular a la que estoy llamando debería generar una excepción bajo ciertas condiciones. Establecí puntos de interrupción y pude ver dónde arrojó la excepción, pero nunca se propaga a la función de expectativa. Sigo recibiendo este error:
AssertionError: expected [Function] to throw 'InvalidInviteException: This is not a valid invite.' Expected :"InvalidInviteException: This is not a valid invite." Actual :[undefined] export const createInviteDomainOwner = () => { const inviteDomainOwner = async ({ emailAddress, firstName, lastName, domainId }) => { try { throw new InvalidInviteException('This is not a valid invite.'); } catch (e) { throw e; } } return { inviteDomainOwner } } it('invite domain owner', async function() { const {inviteDomainOwner} = createInviteDomainOwner(); await expect(() => inviteDomainOwner({ emailAddress: 'abc123abc@test.com', firstName: 'John', lastName: 'Doe', domainId: '1111-1111-1111-1111' }).to.throw(new InvalidInviteException('This is not a valid invite.')); });¿Cómo debo organizar mi manejo de excepciones try/catch para que esto funcione como se espera?
La sugerencia de @ jonrsharpe es correcta, chai-as-promised hará esto. Te doy un ejemplo completamente funcional.
Chai as Promised amplía Chai con un lenguaje fluido para afirmar hechos sobre promesas.
import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; chai.use(chaiAsPromised); class InvalidInviteException extends Error {} export const createInviteDomainOwner = () => { const inviteDomainOwner = async ({ emailAddress, firstName, lastName, domainId }) => { try { throw new InvalidInviteException('This is not a valid invite.'); } catch (e) { throw e; } }; return { inviteDomainOwner, }; }; it('invite domain owner', async function () { const { inviteDomainOwner } = createInviteDomainOwner(); await expect( inviteDomainOwner({ emailAddress: 'abc123abc@test.com', firstName: 'John', lastName: 'Doe', domainId: '1111-1111-1111-1111', }), ).to.eventually.rejectedWith(InvalidInviteException, 'This is not a valid invite.'); }); "chai": "^4.2.0", "chai-as-promised": "^7.1.1",