From ca1cc029c6aa014e45709ed9aa17f1400e978328 Mon Sep 17 00:00:00 2001 From: Steve Konves Date: Wed, 7 Jun 2017 19:59:46 -0700 Subject: [PATCH] test: add tests for promises and async/await see issue #4 --- package.json | 2 +- tests/express-test-harness.js | 47 +++++++++++++++++++++++++++++++++++ tests/node8.js | 46 ++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/node8.js diff --git a/package.json b/package.json index 5a12e18..dac68e8 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "cover": "istanbul cover _mocha index.js -R ./tests --coverage", - "test": "mocha ./tests", + "test": "mocha ./tests/express-test-harness.js", "coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls" }, "repository": { diff --git a/tests/express-test-harness.js b/tests/express-test-harness.js index 00ffde7..f337d88 100644 --- a/tests/express-test-harness.js +++ b/tests/express-test-harness.js @@ -83,4 +83,51 @@ describe('express-http-context', function () { done(); }); }); + + it('maintains context when using promises', function (done) { + // ARRANGE + const app = express(); + + app.use(httpContext.middleware); + + const delayWithPromise = delay => new Promise((resolve, reject) => setTimeout(resolve, delay)); + + app.get('/test', (req, res) => { + const delay = new Number(req.query.delay); + const valueFromRequest = req.query.value; + + httpContext.set('value', valueFromRequest); + + delayWithPromise(delay) + .then(() => { + return delayWithPromise(delay); + }).then(() => { + const valueFromContext = httpContext.get('value'); + res.status(200).json({ + value: valueFromContext + }); + }); + }); + + const sut = supertest(app); + + const value1 = 'value1'; + const value2 = 'value2'; + + // ACT + sut.get('/test').query({ delay: 100, value: value1 }).end((err, res) => { + // ASSERT + assert.equal(res.body.value, value1); + done(); + }); + + sut.get('/test').query({ delay: 50, value: value2 }).end((err, res) => { + // ASSERT + assert.equal(res.body.value, value2); + }); + }); + + if (process.versions.node.startsWith('8.')) { + require('./node8'); + } }); \ No newline at end of file diff --git a/tests/node8.js b/tests/node8.js new file mode 100644 index 0000000..30e28ee --- /dev/null +++ b/tests/node8.js @@ -0,0 +1,46 @@ +'use strict' + +const assert = require('chai').assert; +const express = require('express'); +const supertest = require('supertest'); +const httpContext = require('../index'); + +it('maintains context when using async/await', function (done) { + // ARRANGE + const app = express(); + + app.use(httpContext.middleware); + + const delayWithPromise = async delay => new Promise((resolve, reject) => setTimeout(resolve, delay)); + + app.get('/test', async (req, res) => { + const delay = new Number(req.query.delay); + const valueFromRequest = req.query.value; + + httpContext.set('value', valueFromRequest); + + await delayWithPromise(delay) + + const valueFromContext = httpContext.get('value'); + res.status(200).json({ + value: valueFromContext + }); + }); + + const sut = supertest(app); + + const value1 = 'value1'; + const value2 = 'value2'; + + // ACT + sut.get('/test').query({ delay: 100, value: value1 }).end((err, res) => { + // ASSERT + assert.equal(res.body.value, value1); + done(); + }); + + sut.get('/test').query({ delay: 50, value: value2 }).end((err, res) => { + // ASSERT + assert.equal(res.body.value, value2); + }); +}); \ No newline at end of file