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

Overriding globalThis per mock #198

Closed
tommy-mitchell opened this issue Apr 15, 2023 · 6 comments · Fixed by #205
Closed

Overriding globalThis per mock #198

tommy-mitchell opened this issue Apr 15, 2023 · 6 comments · Fixed by #205

Comments

@tommy-mitchell
Copy link
Collaborator

Is it within scope for esmock to mock globalThis per mocked module? For example, to suppress/capture console.log messages:

const logs = [];
await esmock('source', {
	globalThis: {
		console: {
			log: (...args) => logs.push(...args),
		},
	},
}, { /* other mocks */ });
@koshic
Copy link
Collaborator

koshic commented Apr 15, 2023

At the first sight - no, because globalThis (or Array.prototype.map) mocking is not related to ESM / modules / loaders.

If you want to suppress/capture console.log, just use 'spy' on globalThis from your test framework.

@iambumblehead
Copy link
Owner

iambumblehead commented Apr 15, 2023

esmock doesn't do it now but it is possible to do it. I experimented a few months back and did get a proof of concept working, but changes were very ugly and complicated so they weren't pushed up anywhere.

If it is ever implemented it would be too "expensive" for the test process to apply globally imo so maybe best restricted to local mock. The local esmock could be nested inside a global esmock to achieve deep mocking if that makes sense.

My own bandwidth has been limited these days.

Also it would be cool if esmock interface changed to something like this,

esmock(
  ...localmocks,
  ...packagemocks, // a new addition, similar to global mock but would not affect imports from node_modules
  ...globalmocks
)

Maybe global this could be mocked on a "packagemocks" definition without being prohibitively slow, if the interface did support such a definition.

@tommy-mitchell
Copy link
Collaborator Author

I think it would be useful locally so that each test could have isolated changes to globalThis while still being able to be run concurrently.

By packageMocks, do you mean that the mocks would only apply to relative imports?

@iambumblehead
Copy link
Owner

By packageMocks, do you mean that the mocks would only apply to relative imports?

basically. for example, if one mocked path.basename, path.basename would only be mocked for files in the app sharing package.json with the test, but would not be mocked for modules imported from node_modules

@tommy-mitchell
Copy link
Collaborator Author

esmock doesn't do it now but it is possible to do it. I experimented a few months back and did get a proof of concept working, but changes were very ugly and complicated so they weren't pushed up anywhere.

My own bandwidth has been limited these days.

I could try taking a look at implementing it locally, where's a good place to start?

@iambumblehead
Copy link
Owner

esmock operates in a way that is absurd (necessarily) and because of that there's not a great place to start. Maybe the best start would be to add a failing test.

It would probably be less effort for me to wrap up changes begun here on my machine for this feature and push them up for review. I will at take a look at the changes and maybe push up a minimal starting point this weekend.

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 a pull request may close this issue.

3 participants