test(compartment-mapper): tests for require.resolve to match node.js behavior
MODIFY
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/app/index.js
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/app/index.js
@@ -10,6 +10,7 @@ const afterExec = require('after-exec');
require('default-difficulties');
const nestedFile = require('nested-file/nested');
const nestedExport = require('nested-export/callBound');
+const requireResolvePaths = require('require-resolve');
// Generated by running v16.13.2: node assertion-generators/expected.cjs
// I chose this behavior because:
@@ -31,6 +32,23 @@ function assertInteropNameCollisions({ moduleReference, expect, name }) {
}
module.exports.assertions = {
+ requireResolve() {
+ // process._rawDebug(requireResolvePaths)
+ const assertion = [
+ "Cannot find module '.'",
+ '/skipped/fixtures-cjs-compat/node_modules/require-resolve/package.json',
+ '/skipped/fixtures-cjs-compat/node_modules/require-resolve/nested/file.js',
+ '/skipped/fixtures-cjs-compat/node_modules/require-resolve/nested/file.js.map',
+ '/skipped/fixtures-cjs-compat/node_modules/app/index.js',
+ '/skipped/fixtures-cjs-compat/node_modules/nested-export/callBound.js'
+ ]
+ assertion.forEach((expected, i) => {
+ const actual = requireResolvePaths[i];
+ if (actual !== expected) {
+ throw Error(`Expected require.resolve to return \n"${expected}"\nbut got \n"${actual}"`);
+ }
+ })
+ },
packageNestedFile() {
if(!nestedFile.isOk){
throw Error('Expected nested-file/nested to have been resolved based on path')

MODIFY
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/app/package.json
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/app/package.json
"name": "app",
"version": "1.0.0",
"main": "./index.js",
+ "type": "commonjs",
"dependencies": {
"exports-shenanigans": "^1.0.0",
"defineprop": "^1.0.0",
"default-difficulties": "^1.0.0",
"after-exec": "^1.0.0",
"nested-file": "^1.0.0",
- "nested-export": "^1.0.0"
+ "nested-export": "^1.0.0",
+ "require-resolve": "^1.0.0"
}
}

RENAME
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/nested-file/main.js
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/nested-file/index.js
MODIFY
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/nested-file/package.json
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/nested-file/package.json
{
- "name": "nested-file",
- "main": "./main.js"
+ "name": "nested-file"
}

ADD
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/README.md
+This is a package that serves as a fixture for looking up files with require.resolve
\ No newline at end of file

ADD
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/main.js
+const adaptToAssertions = (f) => {
+ try {
+ return (f()).replace(/^.*compartment-mapper\/test/,'/skipped');
+ } catch (e) {
+ return e.message.split('\n')[0];
+ }
+}
+
+module.exports = [
+ adaptToAssertions(()=>require.resolve(".")),
+ adaptToAssertions(()=>require.resolve("./package.json")),
+ adaptToAssertions(()=>require.resolve("./nested")),
+ adaptToAssertions(()=>require.resolve("./nested/file.js")),
+ adaptToAssertions(()=>require.resolve("./nested/file.js.map")),
+ adaptToAssertions(()=>require.resolve("./nested/file.missing")),
+ adaptToAssertions(()=>require.resolve("app")),
+ adaptToAssertions(()=>require.resolve("fs")),
+ adaptToAssertions(()=>require.resolve("nested-export/callBound")),
+]
+process._rawDebug(module.exports)
\ No newline at end of file

ADD
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/nested/file.js
+'hello';
\ No newline at end of file

ADD
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/nested/file.js.map
+{
+ "hello":"map"
+}
\ No newline at end of file

ADD
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/nested/index.js
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/nested/index.js
ADD
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/package.json
+{
+ "name": "require-resolve",
+ "exports": {
+ ".": "./main.js",
+ "./package.json": "./package.json",
+ "./nested/file.js": "./nested/file.js",
+ "./nested/file.js.map": "./nested/file.js.map"
+ }
+}
\ No newline at end of file

MODIFY
packages/compartment-mapper/test/test-cjs-compat.js
packages/compartment-mapper/test/test-cjs-compat.js
@@ -25,6 +25,7 @@ const assertFixture = (t, { namespace }) => {
assertions.moduleWithCycle();
assertions.defaultChangesAfterExec();
assertions.packageNestedFile();
+ assertions.requireResolve();
t.pass();
};

test(compartment-mapper): tests for require.resolve to match node.js behavior