Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose parts of runtime for dynamic module loading #10069

Closed
skeggse opened this issue May 21, 2020 · 8 comments
Closed

Expose parts of runtime for dynamic module loading #10069

skeggse opened this issue May 21, 2020 · 8 comments

Comments

@skeggse
Copy link

skeggse commented May 21, 2020

🚀 Feature Proposal

Expose enough functionality from the runtime to allow dynamically constructing a module (and optionally loading it into the module registry) from generated source.

In particular, this would involve a new function (e.g. jest.loadModuleFromSource) that hooks into _execModule with a little additional glue to create the InitialModule.

Motivation

When testing bundler-related functionality, it can be useful to evaluate the generated source and check its validity against a well-defined module implementation. It's possible to do this currently by instantiating a Module from the builtin module module (ahem) and using _compile, but this doesn't get access to the current testEnvironment (i.e. there's no document available in the dynamically loaded module under jsdom).

Example

Please provide an example for how this feature would be used.

import { rollup } from 'rollup';
import customPluginBeingTested from '..';

it('should parse and execute', async () => {
  const code = await rollup({
    // e.g. makes use of browser globals and thus needs jsdom present to work.
    input: 'fixtures/example.js',
    plugins: [customPluginBeingTested],
  })
    .then((bundle) => bundle.generate({ format: 'cjs' }))
    .then(({ output }) => output[0].code);

  const module = jest.loadModuleFromSource(`${__dirname}/fixtures/example.out.js`, code);
  expect(module.exports.template()).toEqual(expect.objectContaining({ nodeName: 'DIV' }));
});

Pitch

There doesn't appear to be much public API surface that allows more complex interactions with the runtime or the module registry. It'd be nice to support additional use-cases in testing dynamically generated code that don't seem to currently be supported.


Happy to take a stab at the implementation if y'all think this is a feature worth of the jest platform.

@jeysal
Copy link
Contributor

jeysal commented May 21, 2020

We usually write to temp dirs when testing things like these. Admittedly though, this may be a significant slowdown on Win/Mac.
I'm not particularly excited about adding a new API for this, but what would be great IMO would be exposing a modified 'module' API that fixes the testEnvironment problem, if at all possible.

@skeggse
Copy link
Author

skeggse commented May 21, 2020

A modified module would totally work for me; would we be overriding the _compile method, or adding a new one?

@jeysal
Copy link
Contributor

jeysal commented May 21, 2020

Override - I view it similarly to how we have a custom require (which is also normally part of the Node module system) with some Jest-specific behavior included.

skeggse pushed a commit to skeggse/jest that referenced this issue May 21, 2020
Hook into the jest module loading mechanisms for the `Module#_compile`
functionality provided by Node. Fixes jestjs#10069.
skeggse pushed a commit to skeggse/jest that referenced this issue May 21, 2020
Hook into the jest module loading mechanisms for the `Module#_compile`
functionality provided by Node. Fixes jestjs#10069.
@skeggse
Copy link
Author

skeggse commented May 21, 2020

@jeysal I'd love your feedback on a PoC PR for this: #10072

@jeysal
Copy link
Contributor

jeysal commented May 21, 2020

Thanks a lot for offering to implement this ❤️ I've left a comment over there

skeggse pushed a commit to skeggse/jest that referenced this issue May 22, 2020
Hook into the jest module loading mechanisms for the `Module#_compile`
functionality provided by Node. Fixes jestjs#10069.
skeggse pushed a commit to skeggse/jest that referenced this issue May 22, 2020
Hook into the jest module loading mechanisms for the `Module#_compile`
functionality provided by Node. Fixes jestjs#10069.
skeggse pushed a commit to skeggse/jest that referenced this issue May 22, 2020
Hook into the jest module loading mechanisms for the `Module#_compile`
functionality provided by Node. Fixes jestjs#10069.
skeggse pushed a commit to skeggse/jest that referenced this issue May 22, 2020
Hook into the jest module loading mechanisms for the `Module#_compile`
functionality provided by Node. Fixes jestjs#10069.
@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

github-actions bot commented May 1, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants