Home  >  Q&A  >  body text

Master the correct use of chai test.catch() block

<p>I'm trying to achieve good coverage with end-to-end testing of my code base, so I want to test the <code>.catch()</code> code as well. </p><p> My API uses node.js and mongoose. </p><p> I use chai and mocha for testing.</p> <p>I tried something like: </p> <p><em>File src/controllers/user.controller.js:</em></p> <pre class="brush:php;toolbar:false;">const User = require("../models/user.model"); const getUser = async(req, res) => { try { const user = await User.findOne({name: req.name}); return res.status(200).json(user); } catch (err) { //This is the code I want to test console.error(`Error finding user ${req.name}:`, err); return res.status(err.code).json({ message: err }); } }</pre> <p><em>File src/models/user.model.js:</em></p> <pre class="brush:php;toolbar:false;">const mongoose = require("mongoose"); const UserSchema = mongoose.Schema({ name: { type: String, required: "Name is required", }, }); module.exports = mongoose.model("User", UserSchema);</pre> <p><em>File test/user.test.js:</em></p> <pre class="brush:php;toolbar:false;">const chai = require("chai"); const chaiHttp = require("chai-http"); const spies = require("chai-spies"); const User = require("../src/models/user.model"); chai.use(chaiHttp); chai.use(spies); chai.should(); describe("mongoose errors should be handled", function() { describe("Problematic User.findOne method", function() { const _User_findOne_Backup = User.findOne; beforeEach(function() { // This function should override the real findOne function, but it didn't succeed!User.findOne = function() { return Promise.reject("Forced error"); }; }); afterEach(function() { //Restore the real function after each test User.findOne = _User_findOne_Backup; }); it("Registration should return a server error", function() { const spy = chai.spy(); return chai .request(server) .post("/api/getUser") .send({name: "Alice"}) .then(spy) .catch((err) => { const res = err.response; res.should.have.status(500); }) .then(() => { spy.should.not.have.been.called(); }) ; }); }); });</pre> <p>The problem is that in my tests, the fake <code>User.findOne()</code> method is never called: the original mongoose <code>findOne</code> method is called successfully , so the <code>getUser</code> method never throws an exception, causing my test to fail...</p> <p>Maybe I'm missing something obvious, but I really can't find it... :-(</p><p> If more code or context is needed, please let me know...</p> <p><strong>Update: </strong> Following @Bergi's suggestion, I added complete information about my (simplified) model and required modules...</p>
P粉242535777P粉242535777437 days ago553

reply all(1)I'll reply

  • P粉610028841

    P粉6100288412023-09-03 00:41:51

    Works great for me.

    For example:

    user.model.js

    const mongoose = require("mongoose");
    
    const UserSchema = mongoose.Schema({
      name: {
        type: String,
        required: "Name is required",
      },
    });
    module.exports = mongoose.model("User", UserSchema);
    

    user.controller.js

    const User = require("./user.model");
    
    const getUser = async (req, res) => {
      try {
        const user = await User.findOne({ name: req.body.name });
        return res.status(200).json(user);
      } catch (err) {
        console.error(`Error finding user ${req.body.name}:`, err);
        return res.status(500).json({ message: err });
      }
    }
    
    module.exports = {
      getUser
    }
    

    server.js

    const express = require('express');
    const userController = require('./user.controller');
    
    const app = express();
    
    app.use(express.json())
    app.post('/api/getUser', userController.getUser)
    
    module.exports = app;
    

    user.test.js

    const chai = require("chai");
    const chaiHttp = require("chai-http");
    const User = require("./user.model");
    const server = require('./server');
    chai.use(chaiHttp);
    chai.should();
    
    describe("should handle mongoose errors", function () {
      describe("faulty User.findOne method", function () {
        const _User_findOne_Backup = User.findOne;
        beforeEach(function () {
          User.findOne = function () {
            return Promise.reject("forced error");
          };
        });
        afterEach(function () {
          User.findOne = _User_findOne_Backup;
        });
    
        it("signup should respond with a server error", function () {
          return chai
            .request(server)
            .post("/api/getUser")
            .send({ name: "Alice" })
            .catch((err) => {
              const res = err.response;
              res.should.have.status(500);
            })
            ;
        });
      });
    });
    

    Test Results:

      should handle mongoose errors
        faulty User.findOne method
    Error finding user Alice: forced error
          ✓ signup should respond with a server error
    
    
      1 passing (21ms)
    
    --------------------|----------|----------|----------|----------|-------------------|
    File                |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    --------------------|----------|----------|----------|----------|-------------------|
    All files           |    89.19 |      100 |    88.89 |    88.57 |                   |
     server.js          |      100 |      100 |      100 |      100 |                   |
     user.controller.js |       80 |      100 |      100 |       75 |               6,7 |
     user.model.js      |      100 |      100 |      100 |      100 |                   |
     user.test.js       |    88.89 |      100 |    85.71 |    88.89 |             26,27 |
    --------------------|----------|----------|----------|----------|-------------------|
    

    Package version:

    "chai": "^4.2.0",
    "chai-http": "^4.3.0",
    "mongodb": "^3.6.3",
    "mongoose": "^5.11.9",
    "express": "^4.17.1"
    

    reply
    0
  • Cancelreply