test262-harness icon indicating copy to clipboard operation
test262-harness copied to clipboard

Use a Node module in the prelude

Open thorn0 opened this issue 9 years ago • 10 comments

I'd like to load a Node module in the prelude, but things like require, global, etc. don't exist there. Would be great to have this possibility. (Currently, I'm concatenating files to create a working prelude).

thorn0 avatar Sep 20 '15 17:09 thorn0

The difficulty here is having require do the right thing inside a new VM context since you can't just pass in the parent context's require function...

bterlson avatar Sep 20 '15 23:09 bterlson

The problem is that the parent context's require would look for modules at the wrong path, right?

thorn0 avatar Sep 20 '15 23:09 thorn0

Possibly. I assume that if the module modified globals it would modify the parent realm too.

bterlson avatar Sep 21 '15 02:09 bterlson

ping @bterlson - this is the major barrier to being able to use test262-harness inside a polyfill/shim module as part of normal tests.

ljharb avatar Sep 25 '17 19:09 ljharb

@ljharb do you have any thoughts/suggestions for how to address the issues discussed above?

rwaldron avatar Sep 25 '17 20:09 rwaldron

No, I'm not sure; but i believe you could use something like https://www.npmjs.com/package/require-for to pass a require into runInNewContext that was scoped specifically to the dirname of the prelude file?

ljharb avatar Sep 25 '17 20:09 ljharb

@thorn0 @ljharb Would it be possible for you folks to use a tool like Webpack to bundle your polyfills into a string that could be injected via the prelude option? And in your experience, do you think this approach would generalize for any polyfill?

I'm asking this not (just) because I'm lazy but because that approach is host-independent. Polyfill authors could use it to vet their libraries across hosts--not just Node.js.

jugglinmike avatar Oct 07 '17 20:10 jugglinmike

It might be possible but it would be inordinately complex, and the goal is to make using the harness easy.

If the hostType is node, I would assume I could access node features properly in the prelude.

ljharb avatar Oct 07 '17 23:10 ljharb

I was attempting to run a test against the promise.allsettled shim and ran into this issue. require was defined, but it could not resolve the module

I went with @jugglinmike's proposed solution of using Webpack to bundle what I needed into a prelude file. The webpack config and bundled file is here: https://github.com/astlaurent1/Promise.allSettled/commit/631acd603fd556ab8005165c785a64faf71f0cbb. I suppose I could have done that in my fork of test262

After doing all that, in hindsight, it would seem relatively straightforward to support something like --webpack-prelude <prelude-entry.js>, where <prelude-entry.js> imports whatever modules it needs to and runs necessary bootstrapping code. In this case, the <prelude-entry.js> imports the shim, then calls the .shim() method to make Promise.allSettled available in the context of the test drivers

Pre-packaging the prelude like this also serves the added benefit of not "polluting" test cases with module/import semantics. The bundling provides a nice boundary between what you're preparing and what you're testing

(P.S. See https://github.com/tc39/test262/issues/2112 for discussions on testing Promise.allSettled)

astlaurent1 avatar Apr 05 '19 13:04 astlaurent1

Would it be possible for you folks to use a tool like Webpack to bundle your polyfills into a string that could be injected via the prelude option? And in your experience, do you think this approach would generalize for any polyfill?

This is what we're doing for the polyfill in proposal-temporal. It works, although it makes test runs painfully slow (I suspect) because the prelude has to be parsed and executed for every test file.

ptomato avatar Apr 22 '20 17:04 ptomato