Я использую Jest для тестирования моего приложения-узла, и следующий случай проверяет, что некоторые данные были добавлены в БД (логика DB
работает правильно):
it("should add empty string hooks if none added", async () => {
const item = { type: "type" };
await DB.insert({ item });
const fetch = await DB.fetch({ type: "type" });
expect(fetch.data.items).toHaveLength(1);
expect(typeof fetch.data.items[0]).toEqual("object");
expect(fetch.data.items[0]).toMatchObject({
...item,
preHook: "",
postHook: "",
otherField: expect.anything(),
// ... other fields created by DB might go here
});
});
Я получаю эти результаты при запуске npm test
:
FAIL test/server/server.test.jsal
● Database › Items › should add empty string hooks if none added
TypeError: Cannot read property 'prototype' of undefined
236 | expect(fetch.data.items).toHaveLength(1);
237 | expect(typeof fetch.data.items[0]).toEqual("object");
> 238 | expect(fetch.data.items[0]).toMatchObject({
| ^
239 | ...item,
240 | preHook: "",
241 | postHook: ""
at _callee17$ (test/server/server.test.js:238:45)
at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)
at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22)
at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21)
at step (test/server/server.test.js:7:191)
at test/server/server.test.js:7:361
Используемые пакеты:
"babel-jest": "^23.4.0"
"@types/jest": "^23.3.0"
"jest": "^23.4.1"
"jest-enzyme": "^6.0.2"
"jest-transform-graphql": "^2.1.0"
Это странно, потому что я использую тот же самый помощник в других файлах, и он отлично работает, например:
it("should return all templates for a Postgres datasource", () => {
const templates = getTemplatesForDataSource(
{ type: DataSourceType.Postgres }
);
expect(templates).toMatchObject({
requestMappingTemplates: {
Custom: "",
"Postgres Query": expect.any(String)
},
responseMappingTemplates: {
Custom: "",
"Postgres Response": expect.any(String)
}
});
});
Любая идея, что здесь происходит?
fetch.data.items[0]
не имеет прототипа. Я предполагаю, что под капотом jest toMatchObject
требует прототипа и терпит неудачу. Я вижу, что есть проблема, связанная с Jest о том, что у вас есть "большие пальцы вверх": https://github.com/facebook/jest/issues/6730
Тем временем вы можете использовать Jest toEqual
. Это не требует, чтобы объекты имели прототипы.