Skip to content

Commit

Permalink
Skip async hooks test when not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarmicki committed Jul 14, 2020
1 parent a12b947 commit 64d8d18
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ var http = require('http')
var methods = require('methods')
var request = require('supertest')

var testAsyncHooks = false
var whenAsyncHooksAreSupportedIt = it.skip
try {
var asyncHooks = require('async_hooks')
testAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'
whenAsyncHooksAreSupportedIt = typeof asyncHooks.AsyncLocalStorage === 'function' ? it : it.skip
} catch (ignored) {
}

Expand Down Expand Up @@ -58,30 +58,28 @@ describe('bodyParser()', function () {
.expect(200, '{"user":"tobi"}', done)
})

if (testAsyncHooks) {
it('should work with async hooks', function (done) {
var _bodyParser = bodyParser()
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()
whenAsyncHooksAreSupportedIt('should work with async hooks', function (done) {
var _bodyParser = bodyParser()
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()

var server = http.createServer(function (req, res) {
const store = {
contextMaintained: true
}
asyncLocalStorage.run(store, function () {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(asyncLocalStorage.getStore()))
})
var server = http.createServer(function (req, res) {
const store = {
contextMaintained: true
}
asyncLocalStorage.run(store, function () {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(asyncLocalStorage.getStore()))
})
})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"contextMaintained":true}', done)
})
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"contextMaintained":true}', done)
})

describe('http methods', function () {
before(function () {
Expand Down

0 comments on commit 64d8d18

Please sign in to comment.