Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Aug 27, 2022
1 parent 7852afe commit e77f38c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 6 deletions.
1 change: 0 additions & 1 deletion cli/run/loadConfigFile.ts
Expand Up @@ -55,7 +55,6 @@ async function getConfigFileExport(fileName: string, commandOptions: Record<stri
}
let cannotLoadEsm = false;
const handleWarning = (warning: Error): void => {
console.error(warning);
if (warning.message.includes('To load an ES module')) {
cannotLoadEsm = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/error.ts
Expand Up @@ -71,7 +71,7 @@ const ADDON_ERROR = 'ADDON_ERROR',
ILLEGAL_REASSIGNMENT = 'ILLEGAL_REASSIGNMENT',
INPUT_HOOK_IN_OUTPUT_PLUGIN = 'INPUT_HOOK_IN_OUTPUT_PLUGIN',
INVALID_CHUNK = 'INVALID_CHUNK',
INVALID_CONFIG_MODULE_FORMAT = 'MISSING_CONFIGINVALID_CONFIG_MODULE_FORMAT',
INVALID_CONFIG_MODULE_FORMAT = 'INVALID_CONFIG_MODULE_FORMAT',
INVALID_EXPORT_OPTION = 'INVALID_EXPORT_OPTION',
INVALID_EXTERNAL_ID = 'INVALID_EXTERNAL_ID',
INVALID_OPTION = 'INVALID_OPTION',
Expand Down
9 changes: 9 additions & 0 deletions test/cli/node_modules/rollup-plugin-esm-test/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/cli/node_modules/rollup-plugin-esm-test/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/cli/samples/import-esm-package/_config.js
@@ -1,4 +1,5 @@
module.exports = {
description: 'allows to import ESM dependencies from transpiled config files',
command: "rollup --config --configPlugin '{transform: c => c}'"
skipIfWindows: true,
command: "rollup --config --configPlugin '{transform:c => c}'"
};
7 changes: 6 additions & 1 deletion test/cli/samples/plugin/advanced-esm/_config.js
@@ -1,4 +1,9 @@
module.exports = {
description: 'load an ESM-only rollup plugin from node_modules as well as CJS plugins',
command: `rollup -c -p node-resolve,commonjs,esm-test -p "terser={mangle: false, output: {beautify: true, indent_level: 2}}"`
skipIfWindows: true,

// The NodeJS resolution rules for ESM modules are more restrictive than CJS.
// Must copy the ESM plugin into the main node_modules in order to use and test it.

command: `rm -rf ../../../../../node_modules/rollup-plugin-esm-test && cp -rp ../../../node_modules_rename_me/rollup-plugin-esm-test ../../../../../node_modules/ && rollup -c -p node-resolve,commonjs,esm-test -p "terser={mangle: false, output: {beautify: true, indent_level: 2}}"`
};
34 changes: 32 additions & 2 deletions test/load-config-file/index.js
Expand Up @@ -70,13 +70,29 @@ describe('loadConfigFile', () => {
cause: {
message: "Unexpected token 'export'"
},
code: 'MISSING_CONFIGINVALID_CONFIG_MODULE_FORMAT',
code: 'INVALID_CONFIG_MODULE_FORMAT',
message:
'Node tried to load your configuration file as CommonJS even though it is likely an ES module. To resolve this, change the extension of your configuration to ".mjs", set "type": "module" in your package.json file or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: Unexpected token \'export\'',
url: 'https://rollupjs.org/guide/en/#--bundleconfigascjs'
});
});

it('just throws the error if it is not accompanied by the proper warning', async () => {
let caughtError;
try {
const promise = loadConfigFile(
path.resolve(__dirname, 'samples/esm-with-error/rollup.config.js')
);
process.emit('warning', { message: 'Another warning.' });
await promise;
} catch (err) {
caughtError = err;
}
compareError(caughtError, {
message: 'Config broken.'
});
});

it('throws a helpful error when loading a CommonJS module that should actually be ES', async () => {
let caughtError;
try {
Expand All @@ -103,10 +119,24 @@ describe('loadConfigFile', () => {
cause: {
message: 'module is not defined in ES module scope'
},
code: 'MISSING_CONFIGINVALID_CONFIG_MODULE_FORMAT',
code: 'INVALID_CONFIG_MODULE_FORMAT',
message:
'Rollup transpiled your configuration to an ES module even though it appears to contain CommonJS elements. To resolve this, you can pass the "--bundleConfigAsCjs" flag to Rollup or change your configuration to only contain valid ESM code.\n\nOriginal error: module is not defined in ES module scope',
url: 'https://rollupjs.org/guide/en/#--bundleconfigascjs'
});
});

it('just throws other errors while bundling', async () => {
let caughtError;
try {
await loadConfigFile(path.resolve(__dirname, 'samples/esm-with-error/rollup.config.js'), {
configPlugin: '{transform: c => c}'
});
} catch (err) {
caughtError = err;
}
compareError(caughtError, {
message: 'Config broken.'
});
});
});
3 changes: 3 additions & 0 deletions test/load-config-file/samples/esm-with-error/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
@@ -0,0 +1 @@
throw new Error('Config broken.');

0 comments on commit e77f38c

Please sign in to comment.