Skip to content

Commit

Permalink
tests: add testcase about passing function to preferBuiltins
Browse files Browse the repository at this point in the history
  • Loading branch information
younggglcy committed Sep 23, 2024
1 parent 3722b44 commit 7642e85
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sep } from 'path';
import events from 'events';

export default { sep, events };
36 changes: 30 additions & 6 deletions packages/node-resolve/test/prefer-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ test('handles importing builtins', async (t) => {
});

test('warning when preferring a builtin module, no explicit configuration', async (t) => {
let warning = null;
let warning = '';
await rollup({
input: 'prefer-builtin.js',
onwarn({ message }) {
// eslint-disable-next-line no-bitwise
if (~message.indexOf('preferring')) {
warning = message;
onwarn({ message, pluginCode }) {
if (pluginCode === 'PREFER_BUILTINS') {
warning += message;
}
},
plugins: [nodeResolve()]
Expand All @@ -47,7 +46,8 @@ test('warning when preferring a builtin module, no explicit configuration', asyn
warning,
`preferring built-in module 'events' over local alternative ` +
`at '${localPath}', pass 'preferBuiltins: false' to disable this behavior ` +
`or 'preferBuiltins: true' to disable this warning`
`or 'preferBuiltins: true' to disable this warning.` +
`or passing a function to 'preferBuiltins' to provide more fine-grained control over which built-in modules to prefer.`
);
});

Expand Down Expand Up @@ -134,3 +134,27 @@ test('detects builtins imported with node: protocol', async (t) => {

t.is(warnings.length, 0);
});

test('accpet passing a function to determine which builtins to prefer', async (t) => {
const warnings = [];
const bundle = await rollup({
input: 'prefer-builtin-local-and-builtin.js',
onwarn({ message }) {
warnings.push(message);
},
plugins: [
nodeResolve({
preferBuiltins: (id) => id !== 'events'
})
]
});

const {
module: { exports }
} = await testBundle(t, bundle);

t.is(warnings.length, 0);
t.is(exports.sep, require('node:path').sep);
t.not(exports.events, require('node:events'));
t.is(exports.events, 'not the built-in events module');
});

0 comments on commit 7642e85

Please sign in to comment.