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

docs(test-runner-module-mocking): ✏️ test-moduleMocking details on plugin import order #2642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

movrack
Copy link

@movrack movrack commented Feb 19, 2024

docs(test-runner-module-mocking): ✏️ test-moduleMocking details on plugin import order

Adding details on the order of plugin import in test config. Module mocking must be in first position to avoid conflict with modified path due to other plugins. + refactoring

What I did

  1. Adding doc in readme
  2. Small refactoring for easiness code view
  3. Adding Sinon.js as plugin dep dependency for new test on relative path and try stubbing mocked dependency

…ugin import order

Adding details on the order of plugin import in test config. Module
mocking must be in first position to avoid conflict with modified path
due to other plugins. + refactoring
Copy link

changeset-bot bot commented Feb 19, 2024

🦋 Changeset detected

Latest commit: 1cac4c3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@web/test-runner-module-mocking Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@bashmish bashmish requested a review from tmsns February 20, 2024 10:27
@@ -37,15 +37,37 @@ export function moduleMockingPlugin(): Plugin {
const namedExports = exports.map(e => e.n).filter(n => n !== 'default');
const hasDefaultExport = exports.some(e => e.n === 'default');

body = `
body = generateInterceptedModuleBody(body, url, namedExports, hasDefaultExport);
Copy link
Contributor

@tmsns tmsns Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no real reason to extract this in a separate method, right? Can you revert this change?

@@ -20,6 +20,14 @@ describe('moduleMockingPlugin', function test() {
});
});

it('can intercept server relative modules with stub', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tests are using stubs. :-)
Example:

We don't want to test all different stubbing libraries.

So, you can remove this extra test.

@@ -38,6 +38,8 @@ export default {
};
```

Take care of the plugins order, some plugins can modify path of imported modules and moduleMockingPlugin may be not able to retreive them correctly, so it's advise to use it first in the list.
Copy link
Contributor

@tmsns tmsns Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Take care of the plugins order, some plugins can modify path of imported modules and moduleMockingPlugin may be not able to retreive them correctly, so it's advise to use it first in the list.
When using multiple plugins, make sure `moduleMockingPlugin()` rune before any other plugin that changes import resolutions using the `resolveImport()` hook (eg. `rollupAdapter()`). The interception mechanism of `moduleMockingPlugin()` needs to run before those transformations.
Therefor, it is advised to have `moduleMockingPlugin()` as the first plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants