From 64d8d187061a805c3c239c91484c60184261353c Mon Sep 17 00:00:00 2001 From: Krystian Jarmicki Date: Tue, 14 Jul 2020 08:18:52 +0200 Subject: [PATCH] Skip async hooks test when not supported --- test/body-parser.js | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/test/body-parser.js b/test/body-parser.js index 73b59b70..a9433c3b 100644 --- a/test/body-parser.js +++ b/test/body-parser.js @@ -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) { } @@ -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 () {