test: minor fixes to test-module-loading.js
Use block scope for local variables only used in a small code block. Fixed inverse arguments in some usages of Assert.strictEqual. PR-URL: https://github.com/nodejs/node/pull/12728 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
c20e87a10e
commit
133fb0c3b7
@ -35,59 +35,68 @@ assert.strictEqual(process.mainModule, module,
|
|||||||
// assert that it's *not* the main module in the required module.
|
// assert that it's *not* the main module in the required module.
|
||||||
require('../fixtures/not-main-module.js');
|
require('../fixtures/not-main-module.js');
|
||||||
|
|
||||||
// require a file with a request that includes the extension
|
{
|
||||||
const a_js = require('../fixtures/a.js');
|
// require a file with a request that includes the extension
|
||||||
assert.strictEqual(42, a_js.number);
|
const a_js = require('../fixtures/a.js');
|
||||||
|
assert.strictEqual(a_js.number, 42);
|
||||||
|
}
|
||||||
|
|
||||||
// require a file without any extensions
|
{
|
||||||
const foo_no_ext = require('../fixtures/foo');
|
// require a file without any extensions
|
||||||
assert.strictEqual('ok', foo_no_ext.foo);
|
const foo_no_ext = require('../fixtures/foo');
|
||||||
|
assert.strictEqual(foo_no_ext.foo, 'ok');
|
||||||
|
}
|
||||||
|
|
||||||
const a = require('../fixtures/a');
|
const a = require('../fixtures/a');
|
||||||
const c = require('../fixtures/b/c');
|
|
||||||
const d = require('../fixtures/b/d');
|
const d = require('../fixtures/b/d');
|
||||||
const d2 = require('../fixtures/b/d');
|
const d2 = require('../fixtures/b/d');
|
||||||
// Absolute
|
|
||||||
const d3 = require(path.join(__dirname, '../fixtures/b/d'));
|
|
||||||
// Relative
|
|
||||||
const d4 = require('../fixtures/b/d');
|
|
||||||
|
|
||||||
assert.strictEqual(false, false, 'testing the test program.');
|
{
|
||||||
|
const c = require('../fixtures/b/c');
|
||||||
|
// Absolute
|
||||||
|
const d3 = require(path.join(__dirname, '../fixtures/b/d'));
|
||||||
|
// Relative
|
||||||
|
const d4 = require('../fixtures/b/d');
|
||||||
|
|
||||||
assert.ok(a.A instanceof Function);
|
assert.ok(a.A instanceof Function);
|
||||||
assert.strictEqual('A', a.A());
|
assert.strictEqual(a.A(), 'A');
|
||||||
|
|
||||||
assert.ok(a.C instanceof Function);
|
assert.ok(a.C instanceof Function);
|
||||||
assert.strictEqual('C', a.C());
|
assert.strictEqual(a.C(), 'C');
|
||||||
|
|
||||||
assert.ok(a.D instanceof Function);
|
assert.ok(a.D instanceof Function);
|
||||||
assert.strictEqual('D', a.D());
|
assert.strictEqual(a.D(), 'D');
|
||||||
|
|
||||||
assert.ok(d.D instanceof Function);
|
assert.ok(d.D instanceof Function);
|
||||||
assert.strictEqual('D', d.D());
|
assert.strictEqual(d.D(), 'D');
|
||||||
|
|
||||||
assert.ok(d2.D instanceof Function);
|
assert.ok(d2.D instanceof Function);
|
||||||
assert.strictEqual('D', d2.D());
|
assert.strictEqual(d2.D(), 'D');
|
||||||
|
|
||||||
assert.ok(d3.D instanceof Function);
|
assert.ok(d3.D instanceof Function);
|
||||||
assert.strictEqual('D', d3.D());
|
assert.strictEqual(d3.D(), 'D');
|
||||||
|
|
||||||
assert.ok(d4.D instanceof Function);
|
assert.ok(d4.D instanceof Function);
|
||||||
assert.strictEqual('D', d4.D());
|
assert.strictEqual(d4.D(), 'D');
|
||||||
|
|
||||||
assert.ok((new a.SomeClass()) instanceof c.SomeClass);
|
assert.ok((new a.SomeClass()) instanceof c.SomeClass);
|
||||||
|
}
|
||||||
|
|
||||||
console.error('test index.js modules ids and relative loading');
|
{
|
||||||
const one = require('../fixtures/nested-index/one');
|
console.error('test index.js modules ids and relative loading');
|
||||||
const two = require('../fixtures/nested-index/two');
|
const one = require('../fixtures/nested-index/one');
|
||||||
assert.notStrictEqual(one.hello, two.hello);
|
const two = require('../fixtures/nested-index/two');
|
||||||
|
assert.notStrictEqual(one.hello, two.hello);
|
||||||
|
}
|
||||||
|
|
||||||
console.error('test index.js in a folder with a trailing slash');
|
{
|
||||||
const three = require('../fixtures/nested-index/three');
|
console.error('test index.js in a folder with a trailing slash');
|
||||||
const threeFolder = require('../fixtures/nested-index/three/');
|
const three = require('../fixtures/nested-index/three');
|
||||||
const threeIndex = require('../fixtures/nested-index/three/index.js');
|
const threeFolder = require('../fixtures/nested-index/three/');
|
||||||
assert.strictEqual(threeFolder, threeIndex);
|
const threeIndex = require('../fixtures/nested-index/three/index.js');
|
||||||
assert.notStrictEqual(threeFolder, three);
|
assert.strictEqual(threeFolder, threeIndex);
|
||||||
|
assert.notStrictEqual(threeFolder, three);
|
||||||
|
}
|
||||||
|
|
||||||
console.error('test package.json require() loading');
|
console.error('test package.json require() loading');
|
||||||
assert.throws(
|
assert.throws(
|
||||||
@ -104,31 +113,35 @@ assert.strictEqual(require('../fixtures/packages/main').ok, 'ok',
|
|||||||
assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok',
|
assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok',
|
||||||
'Failed loading package with index.js in main subdir');
|
'Failed loading package with index.js in main subdir');
|
||||||
|
|
||||||
console.error('test cycles containing a .. path');
|
{
|
||||||
const root = require('../fixtures/cycles/root');
|
console.error('test cycles containing a .. path');
|
||||||
const foo = require('../fixtures/cycles/folder/foo');
|
const root = require('../fixtures/cycles/root');
|
||||||
assert.strictEqual(root.foo, foo);
|
const foo = require('../fixtures/cycles/folder/foo');
|
||||||
assert.strictEqual(root.sayHello(), root.hello);
|
assert.strictEqual(root.foo, foo);
|
||||||
|
assert.strictEqual(root.sayHello(), root.hello);
|
||||||
|
}
|
||||||
|
|
||||||
console.error('test node_modules folders');
|
console.error('test node_modules folders');
|
||||||
// asserts are in the fixtures files themselves,
|
// asserts are in the fixtures files themselves,
|
||||||
// since they depend on the folder structure.
|
// since they depend on the folder structure.
|
||||||
require('../fixtures/node_modules/foo');
|
require('../fixtures/node_modules/foo');
|
||||||
|
|
||||||
console.error('test name clashes');
|
{
|
||||||
// this one exists and should import the local module
|
console.error('test name clashes');
|
||||||
const my_path = require('../fixtures/path');
|
// this one exists and should import the local module
|
||||||
assert.ok(my_path.path_func instanceof Function);
|
const my_path = require('../fixtures/path');
|
||||||
// this one does not exist and should throw
|
assert.ok(my_path.path_func instanceof Function);
|
||||||
assert.throws(function() { require('./utils'); },
|
// this one does not exist and should throw
|
||||||
|
assert.throws(function() { require('./utils'); },
|
||||||
/^Error: Cannot find module '\.\/utils'$/);
|
/^Error: Cannot find module '\.\/utils'$/);
|
||||||
|
}
|
||||||
|
|
||||||
let errorThrown = false;
|
let errorThrown = false;
|
||||||
try {
|
try {
|
||||||
require('../fixtures/throws_error');
|
require('../fixtures/throws_error');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
errorThrown = true;
|
errorThrown = true;
|
||||||
assert.strictEqual('blah', e.message);
|
assert.strictEqual(e.message, 'blah');
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.strictEqual(require('path').dirname(__filename), __dirname);
|
assert.strictEqual(require('path').dirname(__filename), __dirname);
|
||||||
@ -136,7 +149,7 @@ assert.strictEqual(require('path').dirname(__filename), __dirname);
|
|||||||
console.error('load custom file types with extensions');
|
console.error('load custom file types with extensions');
|
||||||
require.extensions['.test'] = function(module, filename) {
|
require.extensions['.test'] = function(module, filename) {
|
||||||
let content = fs.readFileSync(filename).toString();
|
let content = fs.readFileSync(filename).toString();
|
||||||
assert.strictEqual('this is custom source\n', content);
|
assert.strictEqual(content, 'this is custom source\n');
|
||||||
content = content.replace('this is custom source',
|
content = content.replace('this is custom source',
|
||||||
'exports.test = \'passed\'');
|
'exports.test = \'passed\'');
|
||||||
module._compile(content, filename);
|
module._compile(content, filename);
|
||||||
@ -166,63 +179,70 @@ try {
|
|||||||
assert.strictEqual(err.message, 'Cannot find module \'../fixtures/empty\'');
|
assert.strictEqual(err.message, 'Cannot find module \'../fixtures/empty\'');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check load order is as expected
|
{
|
||||||
console.error('load order');
|
// Check load order is as expected
|
||||||
|
console.error('load order');
|
||||||
|
|
||||||
const loadOrder = '../fixtures/module-load-order/';
|
const loadOrder = '../fixtures/module-load-order/';
|
||||||
const msg = 'Load order incorrect.';
|
const msg = 'Load order incorrect.';
|
||||||
|
|
||||||
require.extensions['.reg'] = require.extensions['.js'];
|
require.extensions['.reg'] = require.extensions['.js'];
|
||||||
require.extensions['.reg2'] = require.extensions['.js'];
|
require.extensions['.reg2'] = require.extensions['.js'];
|
||||||
|
|
||||||
assert.strictEqual(require(loadOrder + 'file1').file1, 'file1', msg);
|
assert.strictEqual(require(loadOrder + 'file1').file1, 'file1', msg);
|
||||||
assert.strictEqual(require(loadOrder + 'file2').file2, 'file2.js', msg);
|
assert.strictEqual(require(loadOrder + 'file2').file2, 'file2.js', msg);
|
||||||
try {
|
try {
|
||||||
require(loadOrder + 'file3');
|
require(loadOrder + 'file3');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Not a real .node module, but we know we require'd the right thing.
|
// Not a real .node module, but we know we require'd the right thing.
|
||||||
assert.ok(e.message.replace(/\\/g, '/').match(/file3\.node/));
|
assert.ok(e.message.replace(/\\/g, '/').match(/file3\.node/));
|
||||||
}
|
}
|
||||||
assert.strictEqual(require(loadOrder + 'file4').file4, 'file4.reg', msg);
|
assert.strictEqual(require(loadOrder + 'file4').file4, 'file4.reg', msg);
|
||||||
assert.strictEqual(require(loadOrder + 'file5').file5, 'file5.reg2', msg);
|
assert.strictEqual(require(loadOrder + 'file5').file5, 'file5.reg2', msg);
|
||||||
assert.strictEqual(require(loadOrder + 'file6').file6, 'file6/index.js', msg);
|
assert.strictEqual(require(loadOrder + 'file6').file6, 'file6/index.js', msg);
|
||||||
try {
|
try {
|
||||||
require(loadOrder + 'file7');
|
require(loadOrder + 'file7');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
assert.ok(e.message.replace(/\\/g, '/').match(/file7\/index\.node/));
|
assert.ok(e.message.replace(/\\/g, '/').match(/file7\/index\.node/));
|
||||||
|
}
|
||||||
|
assert.strictEqual(require(loadOrder + 'file8').file8, 'file8/index.reg',
|
||||||
|
msg);
|
||||||
|
assert.strictEqual(require(loadOrder + 'file9').file9, 'file9/index.reg2',
|
||||||
|
msg);
|
||||||
}
|
}
|
||||||
assert.strictEqual(require(loadOrder + 'file8').file8, 'file8/index.reg', msg);
|
|
||||||
assert.strictEqual(require(loadOrder + 'file9').file9, 'file9/index.reg2', msg);
|
|
||||||
|
|
||||||
|
{
|
||||||
|
// make sure that module.require() is the same as
|
||||||
|
// doing require() inside of that module.
|
||||||
|
const parent = require('../fixtures/module-require/parent/');
|
||||||
|
const child = require('../fixtures/module-require/child/');
|
||||||
|
assert.strictEqual(child.loaded, parent.loaded);
|
||||||
|
}
|
||||||
|
|
||||||
// make sure that module.require() is the same as
|
{
|
||||||
// doing require() inside of that module.
|
// #1357 Loading JSON files with require()
|
||||||
const parent = require('../fixtures/module-require/parent/');
|
const json = require('../fixtures/packages/main/package.json');
|
||||||
const child = require('../fixtures/module-require/child/');
|
assert.deepStrictEqual(json, {
|
||||||
assert.strictEqual(child.loaded, parent.loaded);
|
|
||||||
|
|
||||||
|
|
||||||
// #1357 Loading JSON files with require()
|
|
||||||
const json = require('../fixtures/packages/main/package.json');
|
|
||||||
assert.deepStrictEqual(json, {
|
|
||||||
name: 'package-name',
|
name: 'package-name',
|
||||||
version: '1.2.3',
|
version: '1.2.3',
|
||||||
main: 'package-main-module'
|
main: 'package-main-module'
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// now verify that module.children contains all the different
|
{
|
||||||
// modules that we've required, and that all of them contain
|
// now verify that module.children contains all the different
|
||||||
// the appropriate children, and so on.
|
// modules that we've required, and that all of them contain
|
||||||
|
// the appropriate children, and so on.
|
||||||
|
|
||||||
const children = module.children.reduce(function red(set, child) {
|
const children = module.children.reduce(function red(set, child) {
|
||||||
let id = path.relative(path.dirname(__dirname), child.id);
|
let id = path.relative(path.dirname(__dirname), child.id);
|
||||||
id = id.replace(/\\/g, '/');
|
id = id.replace(/\\/g, '/');
|
||||||
set[id] = child.children.reduce(red, {});
|
set[id] = child.children.reduce(red, {});
|
||||||
return set;
|
return set;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
assert.deepStrictEqual(children, {
|
assert.deepStrictEqual(children, {
|
||||||
'common.js': {},
|
'common.js': {},
|
||||||
'fixtures/not-main-module.js': {},
|
'fixtures/not-main-module.js': {},
|
||||||
'fixtures/a.js': {
|
'fixtures/a.js': {
|
||||||
@ -273,7 +293,8 @@ assert.deepStrictEqual(children, {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
'fixtures/packages/main/package.json': {}
|
'fixtures/packages/main/package.json': {}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// require() must take string, and must be truthy
|
// require() must take string, and must be truthy
|
||||||
@ -289,29 +310,29 @@ assert.throws(function() {
|
|||||||
|
|
||||||
process.on('exit', function() {
|
process.on('exit', function() {
|
||||||
assert.ok(a.A instanceof Function);
|
assert.ok(a.A instanceof Function);
|
||||||
assert.strictEqual('A done', a.A());
|
assert.strictEqual(a.A(), 'A done');
|
||||||
|
|
||||||
assert.ok(a.C instanceof Function);
|
assert.ok(a.C instanceof Function);
|
||||||
assert.strictEqual('C done', a.C());
|
assert.strictEqual(a.C(), 'C done');
|
||||||
|
|
||||||
assert.ok(a.D instanceof Function);
|
assert.ok(a.D instanceof Function);
|
||||||
assert.strictEqual('D done', a.D());
|
assert.strictEqual(a.D(), 'D done');
|
||||||
|
|
||||||
assert.ok(d.D instanceof Function);
|
assert.ok(d.D instanceof Function);
|
||||||
assert.strictEqual('D done', d.D());
|
assert.strictEqual(d.D(), 'D done');
|
||||||
|
|
||||||
assert.ok(d2.D instanceof Function);
|
assert.ok(d2.D instanceof Function);
|
||||||
assert.strictEqual('D done', d2.D());
|
assert.strictEqual(d2.D(), 'D done');
|
||||||
|
|
||||||
assert.strictEqual(true, errorThrown);
|
assert.strictEqual(errorThrown, true);
|
||||||
|
|
||||||
console.log('exit');
|
console.log('exit');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// #1440 Loading files with a byte order marker.
|
// #1440 Loading files with a byte order marker.
|
||||||
assert.strictEqual(42, require('../fixtures/utf8-bom.js'));
|
assert.strictEqual(require('../fixtures/utf8-bom.js'), 42);
|
||||||
assert.strictEqual(42, require('../fixtures/utf8-bom.json'));
|
assert.strictEqual(require('../fixtures/utf8-bom.json'), 42);
|
||||||
|
|
||||||
// Error on the first line of a module should
|
// Error on the first line of a module should
|
||||||
// have the correct line number
|
// have the correct line number
|
||||||
|
Loading…
x
Reference in New Issue
Block a user