Skip to content

Commit

Permalink
Fix error message when serializing require created by `Module.creat…
Browse files Browse the repository at this point in the history
…eRequire` [fix]
  • Loading branch information
overlookmotel committed Dec 9, 2023
1 parent 15be7f9 commit 51daff3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/init/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function createWrappedRequire(require, filename) {
Object.defineProperties(wrappedRequire, Object.getOwnPropertyDescriptors(require));

// Record `require`
specialFunctions.set(require, {type: 'require', path: filename});
specialFunctions.set(wrappedRequire, {type: 'require', path: filename});

// Return wrapped `require`
return wrappedRequire;
Expand Down
19 changes: 19 additions & 0 deletions test/commonjs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

'use strict';

// Modules
const Module = require('module');

// Imports
const {serialize} = require('livepack'),
{itSerializes, itSerializesEqual, withFixtures, stripLineBreaks} = require('./support/index.js');
Expand Down Expand Up @@ -166,4 +169,20 @@ describe('`require`', () => {
);
});
});

describe('created by `Module.createRequire()`', () => {
it('cannot be serialized directly', () => {
const otherRequire = Module.createRequire(__filename);
expect(() => serialize(otherRequire)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});

it('cannot be serialized in function scope', () => {
const otherRequire = Module.createRequire(__filename);
expect(() => serialize(() => otherRequire)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});
});
});

0 comments on commit 51daff3

Please sign in to comment.