chai and sinon test does not fail but it should

192 views Asked by At

I am trying to write a unit test with chai,sinon and chai promise. Here is my code

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

in my console after running the test I can see that the test evaluation log:

    (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/[email protected]/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)

However nothing is failing and everything shows as passing. How can I force it to fail when error like the above happens?

1

There are 1 answers

0
cuspymd On BEST ANSWER

Return promise of test result like below.

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