如何使用 sinon 和 mocha 在 node js 中模拟可配置中间件

how to mock configurable middleware in node js with sinon and mocha

我有可配置的中间件,我可以在其中传递参数并基于它调用下一个函数。

中间件代码:

文件:我的-middleware.js

exports.authUser = function (options) {
  return function (req, res, next) {
    // Implement the middleware function based on the options object
    next()
  }
}

var mw = require('./my-middleware.js')

app.use(mw.authUser({ option1: '1', option2: '2' }))

如何使用sinon js模拟中间件?

我已经这样做了,但是,它让我感到震惊 "TypeError: next is not a function"。

这是我的单元测试代码:

  it("Should return data by id", (done: any) => {

        sandbox.stub(mw, 'authUser')
            .callsFake((req: any, res: any, next: any) => { return next(); });

        server = require('../../index');

        let req = {
            "id": '123'
        }

        chai.request(server)
            .post("/user")
            .send(req)
            .end((request, res) => {

                expect(res.status).to.equal(200);
                expect(res.body.success).to.equal(true);

                done();
            });
    });

你能帮我模拟一下可配置的中间件吗?提前致谢!!

通过在您的标签中指定 jestjs...我猜您可能想使用 Jest 免费提供的附加功能。 在这种情况下你真的不需要 Sinon,Jest 有它自己的 mock/stub 方式(整个模块也是),比如 jest.mock('moduleName').

您可能需要查看以下 link 以获得有用的示例和可能的用法: https://jestjs.io/docs/en/manual-mocks

authUser是一个高阶函数,所以你需要为此存根。

例如

mws.js:

exports.authUser = function(options) {
  return function(req, res, next) {
    // Implement the middleware function based on the options object
    next();
  };
};

app.js:

const express = require('express');
const mws = require('./mws');
const app = express();

app.use(mws.authUser({ option1: '1', option2: '2' }));

app.post('/user', (req, res, next) => {
  res.json({ success: true });
});

module.exports = app;

app.integration.test.js:

const chai = require('chai');
const chaiHttp = require('chai-http');
const sandbox = require('sinon').createSandbox();
const mws = require('./mws');

chai.use(chaiHttp);
const expect = chai.expect;

describe('61818474', () => {
  afterEach(() => {
    sandbox.restore();
  });
  it('Should return data by id', (done) => {
    sandbox.stub(mws, 'authUser').callsFake((options) => (req, res, next) => {
      return next();
    });
    const server = require('./app');
    const req = { id: '123' };
    chai
      .request(server)
      .post('/user')
      .send(req)
      .end((request, res) => {
        expect(res.status).to.equal(200);
        expect(res.body.success).to.equal(true);
        sandbox.assert.calledWith(mws.authUser, { option1: '1', option2: '2' });
        done();
      });
  });
});

集成测试结果与覆盖率报告:

  61818474
    ✓ Should return data by id (257ms)


  1 passing (265ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |      80 |      100 |   33.33 |      80 |                   
 app.js   |     100 |      100 |     100 |     100 |                   
 mws.js   |   33.33 |      100 |       0 |   33.33 | 2-4               
----------|---------|----------|---------|---------|-------------------