From 6ef4d9ef116f616ccaf189e651f83184a922379c Mon Sep 17 00:00:00 2001 From: Andrey Melikhov Date: Sun, 26 May 2019 15:51:36 +0300 Subject: [PATCH] module: increase code coverage of cjs loader Add test cases to cover uncovered wrap and wrapper getters. Refs: https://coverage.nodejs.org/coverage-99268b1e996d13a0/lib/internal/modules/cjs/loader.js.html#L153 PR-URL: https://github.com/nodejs/node/pull/27898 Reviewed-By: Anna Henningsen Reviewed-By: Jan Krems Reviewed-By: James M Snell Reviewed-By: Rich Trott --- test/fixtures/cjs-module-wrap.js | 1 + test/fixtures/cjs-module-wrapper.js | 23 +++++++++++++++++++++++ test/parallel/test-module-wrap.js | 2 +- test/parallel/test-module-wrapper.js | 9 +++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/cjs-module-wrapper.js create mode 100644 test/parallel/test-module-wrapper.js diff --git a/test/fixtures/cjs-module-wrap.js b/test/fixtures/cjs-module-wrap.js index 2e11cc1a3b3..4a3114c5f1e 100644 --- a/test/fixtures/cjs-module-wrap.js +++ b/test/fixtures/cjs-module-wrap.js @@ -1,3 +1,4 @@ +'use strict'; const assert = require('assert'); const m = require('module'); diff --git a/test/fixtures/cjs-module-wrapper.js b/test/fixtures/cjs-module-wrapper.js new file mode 100644 index 00000000000..b4e73682af8 --- /dev/null +++ b/test/fixtures/cjs-module-wrapper.js @@ -0,0 +1,23 @@ +'use strict'; +const assert = require('assert'); +const m = require('module'); + +global.mwc = 0; + +const originalWrapper = m.wrapper; +const patchedWrapper = {...m.wrapper}; + +patchedWrapper[0] += 'global.mwc = (global.mwc || 0 ) + 1'; + +// Storing original version of wrapper function +m.wrapper = patchedWrapper; + +require('./not-main-module.js'); + +assert.strictEqual(mwc, 1); + +// Restoring original wrapper function +m.wrapper = originalWrapper; +// Cleaning require cache +delete require.cache[require.resolve('./not-main-module.js')]; +delete global.mwc; diff --git a/test/parallel/test-module-wrap.js b/test/parallel/test-module-wrap.js index 639300cb6af..367307e4a79 100644 --- a/test/parallel/test-module-wrap.js +++ b/test/parallel/test-module-wrap.js @@ -4,6 +4,6 @@ const fixtures = require('../common/fixtures'); const { execFileSync } = require('child_process'); const cjsModuleWrapTest = fixtures.path('cjs-module-wrap.js'); -const node = process.argv[0]; +const node = process.execPath; execFileSync(node, [cjsModuleWrapTest], { stdio: 'pipe' }); diff --git a/test/parallel/test-module-wrapper.js b/test/parallel/test-module-wrapper.js new file mode 100644 index 00000000000..39e6d734f4f --- /dev/null +++ b/test/parallel/test-module-wrapper.js @@ -0,0 +1,9 @@ +'use strict'; +require('../common'); +const fixtures = require('../common/fixtures'); +const { execFileSync } = require('child_process'); + +const cjsModuleWrapTest = fixtures.path('cjs-module-wrapper.js'); +const node = process.execPath; + +execFileSync(node, [cjsModuleWrapTest], { stdio: 'pipe' });