【发布时间】:2022-01-10 07:29:15
【问题描述】:
我创建了一个 Jest 自定义匹配器。它可以工作(意思是,它应该通过/失败),但我在 Jest 的输出中没有看到 message。
我做错了什么?我必须做些什么来“启用”消息吗?我是否完全误解了消息应该显示在哪里?
环境:NestJS、Prisma
执行命令:jest --watch
简化代码:
declare global {
namespace jest {
interface Matchers<R> {
toMatchHash(received: string, expected: string): R;
}
}
}
expect.extend({
toMatchJsonHash(received, expected) {
return {
pass: false,
message: () => `Why doesn't this work?!`,
};
},
});
expect(prisma.name.findMany).toHaveBeenCalledWith(expect.toMatchJsonHash('db0110285c148c77943f996a17cbaf27'));
输出:
● MyService › should pass a test using a custom matcher
expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: toMatchJsonHash<db0110285c148c77943f996a17cbaf27>
Received: {<Big ol' object redacted for conciseness>}
Number of calls: 1
178 |
179 | // @ts-ignore
> 180 | expect(prisma.name.findMany).toHaveBeenCalledWith(expect.toMatchJsonHash('db0110285c148c77943f996a17cbaf27'));
| ^
181 | // expect(prisma.name.findMany).toHaveBeenCalledWith({
182 | // select: { type: true, name: true },
183 | // where: {
at Object.<anonymous> (my/my.service.spec.ts:180:32)
我期待看到“为什么这不起作用?!”在输出的某个地方,但我没有。我错过了什么?
【问题讨论】:
-
显示消息的匹配器是
.toHaveBeenCalledWith,试试expect(some).toMatchHash(other)。 -
@jonrsharpe 一定是这样!说得通!不过,我还在“睡觉”;早上很兴奋尝试。但是,使用这种方法,“toMatchHash()”如何访问“调用”信息(参数)?