[Question] Unit test an Express middleware using cls-hooked
See original GitHub issueHello,
I’m trying to write a unit test to check if the value I set is well setted in my middleware.
A simplified version (the namespace ‘mynamespace’ is already created) of my middleware is:
module.exports = (req, res, next) => {
ns.bindEmitter(req);
ns.bindEmitter(res);
ns.run(() => {
const correlationId = uuidV4();
getNamespace('mynamespace').set('correlationId', correlationId);
next();
});
};
My failing test is:
it.only('should set a correlationId in the uuid format in the request and the response', done => {
const req = new Emitter();
const res = new Emitter();
const next = sinon.stub();
correlationIdMiddleware(ns)(req, res, next);
ns.run(() => {
const correlationId = ns.get('correlationId');
correlationId.should.satisfy(isUUID.v4);
done();
});
});
It says that correlationId
is undefined
. My guess is that correlationId
doesn’t exist anymore when I check it in my unit test.
When I run my application I can see my middleware working.
Any idea? Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to Unit Test Express Middleware - In Plain English
A middleware checks if the request contains authorization header or not. If it is, you just let the flow runs through the middleware...
Read more >Correct way to unit test Express Middleware - node.js
I am using mocha , chai and node-mocks-http for my TDD and my question surrounds the tests when next() will not be called...
Read more >cls-hooked - Bountysource
This is locally in unit tests and deployed. I've written a test that fails: const createNamespace = require('cls-hooked').createNamespace const getNamespace ...
Read more >Give your logs more context - Part 1 - DEV Community
How to make sense out of your Node.js web app logs ... running unit/integration tests, manually triggering some request to see if everything ......
Read more >What is the use of express-http-context in Node.js (JavaScript ...
And software design. Unit testing. Continuous integration. SQL databases. Java 11. Mongo, Docker, Cassandra, Cucumber, React, Typescript. Jest. Junit. AssertJ.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In case anyone runs into this, this works for me:
It’s hard to say without knowing what
correlationIdMiddleware
does but it looks like the context only lives during the execution ofcorrelationIdMiddleware
.Take a look at cls-middleware and cls-hooked-sample to get an idea of the context’s lifecycle in middleware and ways to test it.