Skip to content

Commit

Permalink
test: add testcase about passing function to preferBuiltins
Browse files Browse the repository at this point in the history
  • Loading branch information
younggglcy committed Jun 6, 2024
1 parent 9d627a0 commit 7e12e8c
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);

Check warning on line 157 in packages/node-resolve/test/prefer-builtins.js

View workflow job for this annotation

GitHub Actions / Node v20

Unexpected require()

Check warning on line 157 in packages/node-resolve/test/prefer-builtins.js

View workflow job for this annotation

GitHub Actions / Node v18

Unexpected require()
t.not(exports.events, require('node:events'));

Check warning on line 158 in packages/node-resolve/test/prefer-builtins.js

View workflow job for this annotation

GitHub Actions / Node v20

Unexpected require()

Check warning on line 158 in packages/node-resolve/test/prefer-builtins.js

View workflow job for this annotation

GitHub Actions / Node v18

Unexpected require()
t.is(exports.events, 'not the built-in events module');
});

0 comments on commit 7e12e8c

Please sign in to comment.