Skip to content

Commit

Permalink
Allow tests to specify an entry point for running the code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 19, 2024
1 parent 7d3e317 commit dc6a4d8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 31 deletions.
7 changes: 7 additions & 0 deletions packages/commonjs/src/helpers.js
Expand Up @@ -2,12 +2,19 @@ export const isWrappedId = (id, suffix) => id.endsWith(suffix);
export const wrapId = (id, suffix) => `\0${id}${suffix}`;
export const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);

// A proxy module when a module is required from non-wrapped CommonJS. Is different for ESM and CommonJS requires.
export const PROXY_SUFFIX = '?commonjs-proxy';
// Indicates that a required module is wrapped commonjs and needs special handling.
export const WRAPPED_SUFFIX = '?commonjs-wrapped';
// Indicates that a required module is external
export const EXTERNAL_SUFFIX = '?commonjs-external';
// A helper module that contains the exports object of a module
export const EXPORTS_SUFFIX = '?commonjs-exports';
// A helper module that contains the module object of a module, e.g. when module.exports is reassigned
export const MODULE_SUFFIX = '?commonjs-module';
// A special proxy for CommonJS entry points
export const ENTRY_SUFFIX = '?commonjs-entry';
// A proxy when wrapped ESM is required from CommonJS
export const ES_IMPORT_SUFFIX = '?commonjs-es-import';

export const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
Expand Down
@@ -1,7 +1,6 @@
module.exports = {
options: {
output: {
file: 'output/bundle.js',
format: 'cjs'
}
}
Expand Down
Expand Up @@ -6,7 +6,7 @@ const { nodeResolve } = require('@rollup/plugin-node-resolve');
module.exports = {
description: 'resolves imports of node_modules from subdirectories',
options: {
input: path.join(__dirname, 'sub/entry.js'),
input: path.join(__dirname, 'sub/main.js'),
plugins: [nodeResolve(), json()]
},
pluginOptions: {
Expand Down
7 changes: 6 additions & 1 deletion packages/commonjs/test/function.js
Expand Up @@ -48,7 +48,12 @@ readdirSync('./fixtures/function').forEach((dir) => {
console.groupEnd();
}
}
const { exports, global, error } = runCodeSplitTest(codeMap, t, config.context);
const { exports, global, error } = runCodeSplitTest(
codeMap,
t,
config.testEntry || 'main.js',
config.context
);

if (config.exports) config.exports(exports, t);
if (config.global) config.global(global, t);
Expand Down
10 changes: 4 additions & 6 deletions packages/commonjs/test/helpers/util.js
Expand Up @@ -27,7 +27,7 @@ function requireWithContext(code, context) {
return contextWithExports.module.exports;
}

function runCodeSplitTest(codeMap, t, configContext = {}) {
function runCodeSplitTest(codeMap, t, entryName = 'main.js', configContext = {}) {
const requireFromOutputVia = (importer) => (importee) => {
const outputId = path.posix.join(path.posix.dirname(importer), importee);
const code = codeMap[outputId];
Expand All @@ -42,8 +42,6 @@ function runCodeSplitTest(codeMap, t, configContext = {}) {
return require(importee);
};

const chunkNames = Object.keys(codeMap);
const entryName = chunkNames.length === 1 ? chunkNames[0] : 'main.js';
if (!codeMap[entryName]) {
throw new Error(
`Could not find entry "${entryName}" in generated output.\nChunks:\n${Object.keys(
Expand All @@ -56,7 +54,7 @@ function runCodeSplitTest(codeMap, t, configContext = {}) {
let exports;
try {
exports = requireWithContext(codeMap[entryName], {
require: requireFromOutputVia('main.js'),
require: requireFromOutputVia(entryName),
...context
});
} catch (error) {
Expand Down Expand Up @@ -84,9 +82,9 @@ async function getCodeFromBundle(bundle, customOptions = {}) {
return (await bundle.generate(options)).output[0].code;
}

async function executeBundle(bundle, t, { context, exports } = {}) {
async function executeBundle(bundle, t, { context, exports, testEntry = 'main.js' } = {}) {
const codeMap = await getCodeMapFromBundle(bundle, exports ? { exports } : {});
return runCodeSplitTest(codeMap, t, context);
return runCodeSplitTest(codeMap, t, testEntry, context);
}

module.exports = {
Expand Down
38 changes: 19 additions & 19 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -490,7 +490,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
{
'bundle.js': `'use strict';␊
'main.js': `'use strict';␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
Expand Down Expand Up @@ -2813,37 +2813,37 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
{
'entry.js': `'use strict';␊
'main.js': `'use strict';␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
var entry$2 = {};␊
var entry$1;␊
var hasRequiredEntry$1;␊
function requireEntry$1 () {␊
if (hasRequiredEntry$1) return entry$1;␊
hasRequiredEntry$1 = 1;␊
entry$1 = 'custom-module';␊
return entry$1;␊
}␊
var main$1 = {};␊
var entry;␊
var hasRequiredEntry;␊
function requireEntry () {␊
if (hasRequiredEntry) return entry$2;␊
if (hasRequiredEntry) return entry;␊
hasRequiredEntry = 1;␊
t.is(requireEntry$1(), 'custom-module');␊
return entry$2;␊
entry = 'custom-module';␊
return entry;␊
}␊
var entryExports = requireEntry();␊
var entry = /*@__PURE__*/getDefaultExportFromCjs(entryExports);␊
var hasRequiredMain;␊
module.exports = entry;␊
function requireMain () {␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
t.is(requireEntry(), 'custom-module');␊
return main$1;␊
}␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
module.exports = main;␊
`,
}

Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
6 changes: 3 additions & 3 deletions packages/commonjs/test/test.js
Expand Up @@ -324,7 +324,7 @@ test('can handle parens around right have node while producing default export',
plugins: [commonjs()]
});

t.is((await executeBundle(bundle, t)).exports, 42);
t.is((await executeBundle(bundle, t, { testEntry: 'index.js' })).exports, 42);
});

test('typeof transforms: correct-scoping', async (t) => {
Expand All @@ -333,7 +333,7 @@ test('typeof transforms: correct-scoping', async (t) => {
plugins: [commonjs()]
});

t.is((await executeBundle(bundle, t)).exports, 'object');
t.is((await executeBundle(bundle, t, { testEntry: 'correct-scoping.js' })).exports, 'object');
});

test('typeof transforms: protobuf', async (t) => {
Expand All @@ -343,7 +343,7 @@ test('typeof transforms: protobuf', async (t) => {
plugins: [commonjs()]
});

t.is((await executeBundle(bundle, t)).exports, true);
t.is((await executeBundle(bundle, t, { testEntry: 'protobuf.js' })).exports, true);
});

test('typeof transforms: sinon', async (t) => {
Expand Down

0 comments on commit dc6a4d8

Please sign in to comment.