#javascript #node.js #chai #sinon #sinon-chai
#javascript #node.js #chai #sinon #sinon-chai
Вопрос:
Я пытаюсь написать модульный тест с обещанием chai, sinon и chai. Вот мой код
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import dotenv from "dotenv";
import { request, response } from "express";
import { describe, it } from "mocha";
import sinon from "sinon";
it("test", () => {
sandbox.stub(resultsDatastore, "search").resolves({
rows: [
{
fields: {
"network_traffic.dst_ref.resolves_to_refs.value":
"00:00:00:00:00:00",
"user_account.is_privileged": "false",
"x_com_ibm_ariel.category_id": "8052",
},
},
],
});
resultService
.extractIndexesFromRow("test", { query: {} })
.should.eventually.deep.include(
"111network_traffic.dst_ref.resolves_to_refs.value",
);
в моей консоли после запуска теста я вижу, что журнал оценки теста:
(node:68149) UnhandledPromiseRejectionWarning: AssertionError: expected 'network_traffic.dst_ref.resolves_to_refs.value' to deep include '111network_traffic.dst_ref.resolves_to_refs.value'
at /Users/Hamed.Minaee@ibm.com/Desktop/IBM/temp/investigate-backend/tests/unit-tests/api/v1/services/ResultService.test.ts:1345:48
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:68149) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 22)
(node:68149) UnhandledPromiseRejectionWarning: AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but object given
(node:68149) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 23)
Однако ничего не происходит, и все отображается как проходящее. Как я могу заставить его завершиться сбоем при возникновении ошибки, подобной приведенной выше?
Ответ №1:
Верните обещание результата теста, как показано ниже.
it("test", () => {
sandbox.stub(resultsDatastore, "search").resolves({
rows: [
{
fields: {
"network_traffic.dst_ref.resolves_to_refs.value":
"00:00:00:00:00:00",
"user_account.is_privileged": "false",
"x_com_ibm_ariel.category_id": "8052",
},
},
],
});
return resultService
.extractIndexesFromRow("test", { query: {} })
.should.eventually.deep.include(
"111network_traffic.dst_ref.resolves_to_refs.value",
);