NodeJS 如何在超级测试中模拟中间件?

yqyhoc1h  于 2022-11-22  发布在  Node.js
关注(0)|答案(2)|浏览(124)

我想测试app.js中的中间件是否被调用,虽然我模拟了work.js模块,但它仍然运行原始代码。
app.js

const work = require('./work')
const express = require('require')

const app = express()
  .use(work)
  .use(...)
  .get(...)

module.exports = app

work.js

function work(req, res, next){...}

module.exports = work

app-test.js

const supertest = require('supertest')
const app = require('../app')

test('test middleware in app.js', async(done) => {
  jest.mock('../work', () => jest.fn((req, res, next) => next()))

  const agent = () => supertest(app)
  const work = require('../work')  

  await agent()
    .get('/')
    .set(...)

  expect(work).toHaveBeenCalledTimes(1)  // Error

  done()
})

我希望work.js会被调用一次。有什么问题吗?我应该改变我的测试吗?

vmpqdwk3

vmpqdwk31#

下面的例子对我很有效:
app.js

const work = require('./work');
const express = require('express');

const app = express();

app.use(work).get('/', (req, res) => {
  res.sendStatus(200);
});

module.exports = app;

work.js

function work(req, res, next) {
  next();
}

module.exports = work;

app.spec.js

jest.mock('./work', () => jest.fn((req, res, next) => next()));

const supertest = require('supertest');
const app = require('./app');
const work = require('./work');

let agent;
let server;
beforeEach(done => {
  server = app.listen(4000, err => {
    if (err) return done(err);

    agent = supertest(server);
    done();
  });
});

afterEach(done => {
  server && server.close(done);
});

describe('app', () => {
  test('test middleware in app.js', async () => {
    const response = await agent.get('/');
    expect(response.status).toBe(200);
    expect(work).toHaveBeenCalledTimes(1);
  });
});

100%覆盖的单元测试结果:

PASS  src/stackoverflow/56014527/app.spec.js
  app
    ✓ test middleware in app.js (90ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 app.js   |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.248s

以下是完整的演示:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/56014527

h7appiyu

h7appiyu2#

经过一些修改,我成功地复制了slideshowp2的示例(它工作正常:)),使用了一个不需要服务器的版本。

var express = require("express");
var supertest = require("supertest");
var httpContext = require("express-http-context");

var app = express();

// before uses
app.use(httpContext.middleware);
// before gets etc.
app.use(work());

app.get("/", (req, res) => {
  console.log("get");
  res.sendStatus(200);
});

describe("work", () => {
  it("should call work", async () => {
    var agent = supertest.agent(app);
    const res = await agent.get("/");
    expect(res.status).toBe(200);
    // expect...;
  });
  });

一月一日

export function work() {
  return (req, res, next) => {
    console.log("work");
    next();
  };
}

相关问题