Typescript 用打字脚本中的笑话嘲弄express和mongoose

Typescript 用打字脚本中的笑话嘲弄express和mongoose,typescript,express,mongoose,jestjs,Typescript,Express,Mongoose,Jestjs,关于如何使用jest.fn()模拟类上的类型脚本类和方法(例如express'Request,Response和NextFunction,以及mongoose模型上的save()方法),我一直很难找到好的资源 例如,假设我有以下模型和控制器: 型号/Foo.ts: import * as mongoose from "mongoose" export type FooModel = mongoose.Document & { owner: mongoose.Schema.Types

关于如何使用
jest.fn()
模拟类上的类型脚本类和方法(例如express'
Request
Response
NextFunction
,以及mongoose模型上的
save()
方法),我一直很难找到好的资源

例如,假设我有以下模型和控制器:

型号/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)
我想添加一些单元测试:

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);
  }
});

我遇到的主要问题是注释掉的部分:我还没有弄清楚如何实际模拟
req
res
next
,或者如何剔除
Foo.save()
或使其抛出错误。如何实现这一点?

要模拟请求和响应对象,只需将它们与所使用的值一起传递即可。在您的情况下,如下所示:

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);
  });
});

我希望这有帮助

你找到解决办法了吗?有解决办法吗,凯蒂?
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);
  });
});