question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Unit test an Express middleware using cls-hooked

See original GitHub issue

Hello,

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:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
krotscheckcommented, Jul 10, 2019

In case anyone runs into this, this works for me:

import { ns } from '../auth/request-context';

describe('a test', () => {
     let context;

     beforeEach(() => {
         context = ns.createContext();
         ns.enter(context);
    });

    it('should store and retrieve a value', () => {
         ns.set('key', 'value');
         const response = ns.get('key');
         response.should.equal('value');
    });

    afterEach(() => {
        ns.exit(context);
    });
});

1reaction
Jeff-Lewiscommented, Dec 5, 2017

It’s hard to say without knowing what correlationIdMiddleware does but it looks like the context only lives during the execution of correlationIdMiddleware.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found