test262-harness
test262-harness copied to clipboard
Use a Node module in the prelude
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).
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...
The problem is that the parent context's require
would look for modules at the wrong path, right?
Possibly. I assume that if the module modified globals it would modify the parent realm too.
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 do you have any thoughts/suggestions for how to address the issues discussed above?
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?
@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.
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.
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
)
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.