Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking express and mongoose with jest in typescript

I've been having a hard time finding good resources on how to use jest.fn() to mock typescript classes and methods on classes (such as express' Request, Response and NextFunction, and the save() method on a mongoose model.)

As an example, let's say I have the following model and controller:

models/Foo.ts:

import * as mongoose from "mongoose"

export type FooModel = mongoose.Document & {
  owner: mongoose.Schema.Types.ObjectId,
  bars: string[]
}

const fooSchema = new mongoose.Schema({
  owner: { type: mongoose.Schema.Types.ObjectId, ref: "User", index: true },
  bars: [String]
}

export const Foo = mongoose.model<FooModel>("Foo", fooSchema)

controllers/foo.ts:

import { Request, Response, NextFunction } from "express";
import { Foo, FooModel } from "../models/Foo";

export let createFoo = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const foo = new Foo({
      owner: req.user._id,
      bars: req.body.bars
    });
    await foo.save();
    res.status(200).json(foo);
  } catch (err) {
    next(err)
  }
}

And I'd like to add the some unit tests:

import { Request, Response, NextFunction } from "express";
import { Foo } from "../../src/models/Foo";
import * as fooController from "../../src/controllers/foo";
import {} from "jest";

describe("createFoo", async () => {
  let req: Request;
  let res: Response;
  let next: NextFunction;
  const bars = ["baz", "qux", "quux"];

  beforeEach(() => {
    // Mock req, res and next
    // Set req.body.bars equal to bars
    // Stub out Foo.save() so that we don't actually save to the db
  });

  it("should add bars to the foo", async () => {
    await fooController.createFoo(req, res, next);
    responseData = JSON.parse(res.json)
    expect(responseData.bars).toEqual(bars);
  });

  it("should save the foo", async () => {
    await fooController.createFoo(req, res, next);
    expect(Foo.prototype.save).toHaveBeenCalled();
  }

  it("should call next on error", async () => {
    const err = new Error();
    // Set up Foo.save() to throw err
    await fooController.createFoo(req, res, next);
    expect(next).toHaveBeenCalledWith(err);
  }
});

The main problems I'm having are with the commented out parts: I haven't figured out how to actually mock out req, res and next, or how to stub out Foo.save() or make it throw an error. How can I accomplish this?

like image 909
Katie Avatar asked Jan 12 '18 01:01

Katie


1 Answers

To mock the Request and Response objects, you simply just pass them in with the values that you are using. In your case, this looks like this:

import { Request, Response, NextFunction } from 'express';
import { Foo } from '../../src/models/Foo';
import * as fooController from '../../src/controllers/foo';
import { doesNotReject } from 'assert';

describe('createFoo', async () => {
  it('should add bars to the foo', async () => {
    /**
     * Spy on the model save function and return a completed promise.
     * We are not testing the model here, only the controller so this is ok.
     */
    jest.spyOn(Foo.prototype, 'save').mockImplementationOnce(() => Promise.resolve());

    /**
     * Create a mock request and set type to any to tell typescript to ignore type checking
     */
    const mockRequest: any = {
      user: {
        _id: 1234,
      },
      body: {
        bars: ['baz', 'qux', 'quux'],
      },
    };

    /**
     * Create a mock repsonse with only the methods that are called in the controller and
     * record their output with jest.fn()
     */
    const mockResponse: any = {
      status: jest.fn(),
      json: jest.fn(),
    };

    /**
     * Create a mock next function. It is okay to set its type to Express's NextFunction because
     * we are mocking the entire function.
     */
    const mockNext: NextFunction = jest.fn();

    await fooController.createFoo(mockRequest, mockResponse, mockNext);

    expect(mockResponse.json).toHaveBeenCalledTimes(1);
    expect(mockResponse.json).toHaveBeenCalledWith('bars');

    expect(mockResponse.status).toHaveBeenCalledTimes(1);
    expect(mockResponse.status).toHaveBeenCalledWith(200);
  });
});

I hope this helps!

like image 121
Borduhh Avatar answered Oct 03 '22 08:10

Borduhh