Skip to content

Commit

Permalink
add test for async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
amiram committed Feb 15, 2018
1 parent a01d81d commit 16b6231
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cls-hooked": "^4.2.2"
},
"devDependencies": {
"bluebird": "^3.5.1",
"chai": "^3.5.0",
"coveralls": "^2.13.0",
"express": "^4.15.2",
Expand Down
44 changes: 43 additions & 1 deletion tests/express-test-harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assert = require('chai').assert;
const express = require('express');
const supertest = require('supertest');
const bluebird = require('bluebird');

const httpContext = require('../index');

Expand Down Expand Up @@ -57,7 +58,48 @@ describe('express-cls-hooked', function () {
});
});

it('returns undefined when key is not found', function (done) {
it('maintains unique value across concurrent requests with async/await', function (done) {
// ARRANGE
const app = express();

app.use(httpContext.middleware);

app.get('/test', (req, res) => {
const delay = new Number(req.query.delay);
const valueFromRequest = req.query.value;

httpContext.set('value', valueFromRequest);

const doJob = async () => {
await bluebird.delay(delay);
const valueFromContext = httpContext.get('value');
res.status(200).json({
value: valueFromContext
});
};

doJob();
});

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);
});
});

it('returns undefined when key is not found', function (done) {
// ARRANGE
const app = express();

Expand Down

0 comments on commit 16b6231

Please sign in to comment.