在Express中测试中间件而无需创建重新创建服务器的简单方法? [英] Simple way to test middleware in Express without creating recreating server?

查看:112
本文介绍了在Express中测试中间件而无需创建重新创建服务器的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在每次测试的基础上存根我的中间件功能. 在此明确指出,问题在于我不能仅存根中间件功能,因为节点已缓存了中间件功能,因此自从我一开始就创建我的应用程序以来,我就无法进行任何操作.

I'd like to be able to stub my middleware functions on a per-test basis. The problem, as articulated here, is that I can't just stub my middleware functions since node has cached the middleware function so I can't stub it since I create my app at the very beginning.

const request = require("supertest");
const { expect } = require("chai");
const sinon = require('sinon');
const auth = require ("../utils/auth-middleware")
const adminStub = sinon.stub(auth, "isAdmin").callsFake((req, res, next) => next());
const app = require("../app.js"); // As soon as I create this, the middleware isAdmin function becomes cached and no longer mutable

以上方法可作为链接的SO答案中描述的解决方案,但是我不希望为了还原存根或修改伪造品,我必须完全重新创建服务器.

The above works as the solution described in the linked SO answer, but I don't like that in order to restore the stub or modify the fake, I have to completely recreate the server.

我想知道是否有更好,更优雅的方法来解决Node在第一个require上缓存这些功能的事实.我一直在研究也许使用proxyquiredecache,但是两者似乎都提供了解决方法,而不是可持续的解决方案(尽管我很可能在这里错了).

I'm wondering if there's a better, elegant way to work around the fact that Node caches these functions upon first require. I was looking into maybe using proxyquire or decache but both seem to provide workarounds rather than sustainable solutions (although I may very well be wrong here).

推荐答案

问题与Node缓存模块并没有真正的关系-最初创建服务器时,谁存储对中间件功能的引用. 在对require d模块的isAdmin方法进行存根后,将对存入缓存的版本进行存根,因此使用proxyquire之类的工具仅允许您在需要时需要模块的新版本(无存根方法).出于某种原因.

The problem is not really related to Node caching modules - it's express who stores a reference to a middleware function when a server is initially created. After isAdmin method of required module is stubbed, it's the cached version who gets stubbed, so using a tool like proxyquire would only allow you to require a fresh version of the module (without stubbed method) if you need it for some reason.

如果您要查找的是为已创建的Express服务器调整特定中间件的行为,则需要一种方法来通过其引用来更改中间件功能的行为. 希望sinon存根(还有其他,例如jest提供的存根)能够做到这一点.但是,在创建Express Server之前,仍然需要对模块进行存根,以便它存储对存根函数的引用.

If what you're looking for is adjusting behavior of a particular middleware for already created express server, you'd need a way to alter middleware function's behavior by its reference. Hopefully, sinon stubs (and others too, e.g. ones jest provides) are capable of that. However, you still need to stub the module before creating an express server so it stores a reference to the stubbed function.

示例实现可能如下所示:

Sample implementation might look as follows:

const request = require("supertest");
const { expect } = require("chai");
const sinon = require('sinon');
const auth = require ("../utils/auth-middleware");

// store reference to original function in case you need it:
const originalIsAdmin = auth.isAdmin

// replace isAdmin method with a stubbed, but don't specify implementation yet
const adminStub = sinon.stub(auth, "isAdmin");

// init express server that relies on stubbed `auth.isAdmin` reference
const app = require("../app.js");

it('this test is using auth.isAdmin that just calls .next()', () => {
  // make middleware just pass
  auth.isAdmin.callsFake((req, res, next) => next());

  // ...
});

it('this test is using real auth.isAdmin implementation', () => {
  // make middleware call real implementation
  auth.isAdmin.callsFake(originalIsAdmin);

  // ...
});

这篇关于在Express中测试中间件而无需创建重新创建服务器的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆