I have a very simple auth middleware with an export default ()
anonymous function:
export default () => async (req, res, next) => {
try {
// some auth implementation... does not matter
} else {
throw new Error('No "Bearer" token passed as header')
}
next()
} catch (err) {
next(err)
}
}
I want to stub this in my unit tests to just continue with next()
.
import express from 'express';
import supertest from 'supertest';
import { router } from "express-file-routing"
import auth from '../../middlewares/auth.js';
import sinon from 'sinon'
describe("/foo", () => {
let app, request;
before(async () => {
app = express();
app.use("/", await router())
request = supertest(app);
});
beforeEach(() => {
sinon.stub(auth()).callsFake((req, res, next) => next());
// tried also ...
// sinon.replace(auth, 'default', () => (req, res, next) => next());
});
//... test continue
But these are not working. I am getting TypeError: sinon.stub(...).callsFake is not a function
or
TypeError: Cannot replace non-existent property default. Perhaps you meant sandbox.define()?
when I try with sinon.replace