Chai 测试 await/async 带参数的箭头函数

Chai testing await/async arrow function with parameters

我正在尝试对多个函数执行 Chai 测试,但遇到了各种障碍。我正在调用的特定函数应该在特定条件下抛出异常。我设置了断点并可以看到它抛出异常的位置,但它永远不会传播到 expect 函数。我不断收到此错误:

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

我应该如何组织我的 try/catch 异常处理才能使其按预期工作?

@jonrsharpe 的建议是正确的, chai-as-promised 会这样做。我给你一个完整的例子。

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",