@@ -8,79 +8,79 @@ import { plugin, findNodeAddonForBindings, type PluginOptions } from "./plugin.j
88import { setupTempDirectory } from "../test-utils.js" ;
99
1010describe ( "plugin" , ( ) => {
11- it ( "transforms require calls, regardless" , ( context ) => {
12- const tempDirectoryPath = setupTempDirectory ( context , {
13- "package.json" : `{ "name": "my-package" }` ,
14- "addon-1.node" :
15- "// This is supposed to be a binary file" ,
16- "addon-2.node" :
17- "// This is supposed to be a binary file" ,
18- "addon-1.js" : `
19- const addon = require('./addon-1.node');
20- console.log(addon);
21- ` ,
22- "addon-2.js" : `
23- const addon = require('./addon-2.node');
24- console.log(addon);
25- ` ,
26- "sub-directory/addon-1.js" : `
27- const addon = require('../addon-1.node');
28- console.log(addon);
29- ` ,
30- "sub-directory-3/package.json" : `{ "name": "sub-package" }` ,
31- "sub-directory-3/addon-outside.js" : `
32- const addon = require('../addon-2.node');
33- console.log(addon);
34- ` ,
35- "addon-1-bindings.js" : `
36- const addon = require('bindings')('addon-1');
37- console.log(addon);
38- ` ,
39- "require-js-file.js" : `
40- const addon = require('./addon-1.js');
41- console.log(addon);
42- ` ,
43- } ) ;
44-
11+ describe ( "transforms require calls, regardless" , ( ) => {
4512 const EXPECTED_PKG_NAME = "my-package" ;
4613
4714 type TestCaseParams = {
4815 resolvedPath ?: string ;
4916 originalPath : string ;
5017 inputFile : string ;
51- options ?: PluginOptions ;
52- } ;
53- const runTestCase = ( {
54- resolvedPath,
55- originalPath,
56- inputFile,
57- options,
58- } : TestCaseParams ) => {
59- const result = transformFileSync (
60- path . join ( tempDirectoryPath , inputFile ) ,
61- { plugins : [ [ plugin , options ] ] }
62- ) ;
63- assert ( result ) ;
64- const { code } = result ;
65- if ( ! resolvedPath ) {
66- assert (
67- code && ! code . includes ( `requireNodeAddon` ) ,
68- `Unexpected code: ${ code } `
69- ) ;
70- } else {
71- assert (
72- code && code . includes ( `requireNodeAddon("${ resolvedPath } ", "${ EXPECTED_PKG_NAME } ", "${ originalPath } ")` ) ,
73- `Unexpected code: ${ code } `
74- ) ;
75- }
7618 } ;
7719
78- runTestCase ( { resolvedPath : "./addon-1.node" , originalPath : "./addon-1.node" , inputFile : "./addon-1.js" } ) ;
79- runTestCase ( { resolvedPath : "./addon-2.node" , originalPath : "./addon-2.node" , inputFile : "./addon-2.js" } ) ;
80- runTestCase ( { resolvedPath : "./addon-1.node" , originalPath : "../addon-1.node" , inputFile : "./sub-directory/addon-1.js" } ) ;
81- runTestCase ( { resolvedPath : "./addon-2.node" , originalPath : "../addon-2.node" , inputFile : "./sub-directory-3/addon-outside.js" } ) ;
82- runTestCase ( { resolvedPath : "./addon-1.node" , originalPath : "addon-1" , inputFile : "./addon-1-bindings.js" } ) ;
83- runTestCase ( { resolvedPath : undefined , originalPath : "./addon-1.js" , inputFile : "./require-js-file.js" } ) ;
20+ ( [
21+ { resolvedPath : "./addon-1.node" , originalPath : "./addon-1.node" , inputFile : "./addon-1.js" } ,
22+ { resolvedPath : "./addon-2.node" , originalPath : "./addon-2.node" , inputFile : "./addon-2.js" } ,
23+ { resolvedPath : "./addon-1.node" , originalPath : "../addon-1.node" , inputFile : "./sub-directory/addon-1.js" } ,
24+ { resolvedPath : "./addon-2.node" , originalPath : "../addon-2.node" , inputFile : "./sub-directory-3/addon-outside.js" } ,
25+ { resolvedPath : "./addon-1.node" , originalPath : "addon-1" , inputFile : "./addon-1-bindings.js" } ,
26+ { resolvedPath : undefined , originalPath : "./addon-1.js" , inputFile : "./require-js-file.js" } ,
27+ ] as TestCaseParams [ ] ) . forEach ( ( { resolvedPath, originalPath, inputFile } ) => {
28+ const expectedMessage = resolvedPath
29+ ? `transform to requireNodeAddon() with "${ resolvedPath } "`
30+ : "NOT transform to requireNodeAddon()" ;
31+
32+ it ( `${ inputFile } should ${ expectedMessage } ` , ( context ) => {
33+ const tempDirectoryPath = setupTempDirectory ( context , {
34+ "package.json" : `{ "name": "${ EXPECTED_PKG_NAME } " }` ,
35+ "addon-1.node" :
36+ "// This is supposed to be a binary file" ,
37+ "addon-2.node" :
38+ "// This is supposed to be a binary file" ,
39+ "addon-1.js" : `
40+ const addon = require('./addon-1.node');
41+ console.log(addon);
42+ ` ,
43+ "addon-2.js" : `
44+ const addon = require('./addon-2.node');
45+ console.log(addon);
46+ ` ,
47+ "sub-directory/addon-1.js" : `
48+ const addon = require('../addon-1.node');
49+ console.log(addon);
50+ ` ,
51+ "sub-directory-3/package.json" : `{ "name": "sub-package" }` ,
52+ "sub-directory-3/addon-outside.js" : `
53+ const addon = require('../addon-2.node');
54+ console.log(addon);
55+ ` ,
56+ "addon-1-bindings.js" : `
57+ const addon = require('bindings')('addon-1');
58+ console.log(addon);
59+ ` ,
60+ "require-js-file.js" : `
61+ const addon = require('./addon-1.js');
62+ console.log(addon);
63+ ` ,
64+ } ) ;
65+ const result = transformFileSync (
66+ path . join ( tempDirectoryPath , inputFile ) ,
67+ { plugins : [ [ plugin , { } ] ] }
68+ ) ;
69+ assert ( result ) ;
70+ const { code } = result ;
71+ if ( ! resolvedPath ) {
72+ assert (
73+ code && ! code . includes ( `requireNodeAddon` ) ,
74+ `Unexpected code: ${ code } `
75+ ) ;
76+ } else {
77+ assert (
78+ code && code . includes ( `requireNodeAddon("${ resolvedPath } ", "${ EXPECTED_PKG_NAME } ", "${ originalPath } ")` ) ,
79+ `Unexpected code: ${ code } `
80+ ) ;
81+ }
82+ } ) ;
83+ } ) ;
8484 } ) ;
8585
8686 it ( "transforms require calls to packages with native entry point" , ( context ) => {
0 commit comments