Skip to content

Commit

Permalink
test: add tests for promises and async/await
Browse files Browse the repository at this point in the history
see issue #4
  • Loading branch information
skonves committed Jun 12, 2017
1 parent fdfac93 commit ca1cc02
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
47 changes: 47 additions & 0 deletions tests/express-test-harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
46 changes: 46 additions & 0 deletions tests/node8.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit ca1cc02

Please sign in to comment.