Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
Chai testing await/async arrow function with parameters

I am trying to perform Chai testing on several functions and have hit a roadblock of sorts. The particular function I am calling should throw an exception under certain conditions. I set breakpoints and could see where it threw the exception but it never propagates out to the expect function. I keep getting this 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.'));
});

How should I organize my try/catch exception handling for this to work as expected?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

@jonrsharpe's suggestion is correct, chai-as-promised will do this. I give you a fully working example.

Chai as Promised extends Chai with a fluent language for asserting facts about promises.

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",
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda