开玩笑“匹配器错误:期望值必须是一个函数”

发布于 2025-01-19 06:46:17 字数 1894 浏览 4 评论 0原文

我正在使用带Jest的Nestjs,我会收到此错误:

匹配器错误:预期值必须是函数

...在单元测试之后运行时。

我已经将无效的电子邮件设置在模拟机体中。我在这里错过了什么吗?

app.service.ts

@Injectable()
export class UserService {
  constructor(private emailService: EmailService) {}

  async registerUserInquiry(user: UserDto): Promise<{ email: string }> {
    try {
      await sendEmail(user);
    } catch (error) {
      throw new HttpException('Something went wrong!', HttpStatus.BAD_REQUEST);
    }
    return {
      email: user.email,
    };
  }
}

app.service.service.spec.ts

describe("registerUser()", () => {
  it("Should throw bad request error when passing invalid data", async () => {
    const mockBody: UserDto = {
      name: "John Doe",
      message: "Example inquiry message",
      email: "@example",
      mobile: "+60121234567",
    };

    expect(async () => await service.registerUserInquiry(mockBody)).toThrow(
      new HttpException("Something went wrong!", HttpStatus.BAD_REQUEST)
    );
  });
});

email.config.ts

export const sendEmail = async (user: User) => {
  const transporter = nodemailer.createTransport({
    ... // service & auth
  });

  const options = {
   ... // email info
  };

  await transporter.sendMail(options, function (error, info) {
    try {
      console.info(error);
      return info;
    } catch (error) {
      console.error(error);
      throw error;
    }
  });
};

错误:

I'm using NestJS with Jest and I get this error:

Matcher error: expected value must be a function

... when run following unit test.

I have set invalid email in mockBody. Did I missed anything here?

app.service.ts

@Injectable()
export class UserService {
  constructor(private emailService: EmailService) {}

  async registerUserInquiry(user: UserDto): Promise<{ email: string }> {
    try {
      await sendEmail(user);
    } catch (error) {
      throw new HttpException('Something went wrong!', HttpStatus.BAD_REQUEST);
    }
    return {
      email: user.email,
    };
  }
}

app.service.spec.ts

describe("registerUser()", () => {
  it("Should throw bad request error when passing invalid data", async () => {
    const mockBody: UserDto = {
      name: "John Doe",
      message: "Example inquiry message",
      email: "@example",
      mobile: "+60121234567",
    };

    expect(async () => await service.registerUserInquiry(mockBody)).toThrow(
      new HttpException("Something went wrong!", HttpStatus.BAD_REQUEST)
    );
  });
});

email.config.ts

export const sendEmail = async (user: User) => {
  const transporter = nodemailer.createTransport({
    ... // service & auth
  });

  const options = {
   ... // email info
  };

  await transporter.sendMail(options, function (error, info) {
    try {
      console.info(error);
      return info;
    } catch (error) {
      console.error(error);
      throw error;
    }
  });
};

Error:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2025-01-26 06:46:17

您的功能不是尝试此操作,而不是

expect(async () => await service.registerUserInquiry(mockBody)).toThrow(
  new HttpException("Something went wrong!", HttpStatus.BAD_REQUEST)
);

尝试此功能

await expect(service.registerUserInquiry(mockBody)).rejects.toThrowError(...)

,这意味着它没有丢弃错误,而是拒绝。

Instead of this

expect(async () => await service.registerUserInquiry(mockBody)).toThrow(
  new HttpException("Something went wrong!", HttpStatus.BAD_REQUEST)
);

Try this one

await expect(service.registerUserInquiry(mockBody)).rejects.toThrowError(...)

Your function is a promise which means it is not throwing an error but instead it rejects.

双手揣兜 2025-01-26 06:46:17
try {
    await service.registerUserInquiry(mockBody)
} catch(err) {
expect(err).toBeInstanceOf(BAD_REQUEST);
expect(err.message).toEqual{'Something went wrong!'}
}
try {
    await service.registerUserInquiry(mockBody)
} catch(err) {
expect(err).toBeInstanceOf(BAD_REQUEST);
expect(err.message).toEqual{'Something went wrong!'}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文