-
-
Notifications
You must be signed in to change notification settings - Fork 269
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: clarify that mockImport is "load with a mocked import" and not "mock the inported thing" #1022
Comments
That's not what mock import does. It's for mocking the things that that module imports. Example, if // a.js
import { foo } from './b.js'
export { foo } // b.js
export const foo = 'bar' // a.spec.js
import t from 'tap'
const a = await t.mockImport('./a.js', {
'./b.js': {
foo: 'baz'
}
})
console.log(a.foo) // 'baz' In other words, it's not "a thing that you import and then mock", it's "load this module, but with a mocked If you just want to import something and then swap out some fields, check out For example: import t from 'tap'
const a = await import('./a.js')
t.equal(a.foo, 'bar')
const fooAccesses = t.intercept(a, 'foo', { value: 'baz' })
t.equal(a.foo, 'baz')
console.error(fooAccesses) // [ { type: 'get', value: 'baz', ... } ] |
Ah, roger that. |
Actually, you know, "mock the imported thing" is a reasonable guess as to what a method named Reopening as a doc issue. |
@isaacs it was especially confusing because I was coming from jest's |
It's a matter of taste, so there's really no right or wrong answer per se, and the word "mock" is super overloaded in the testing space so it's a reasonable approach I guess, but personally I find jest's import mocking pretty confusing to reason about. It's sort of global, but not exactly, and feels very magical. Tap's import mocking is just a specific function that loads the actual module code, but with its dependencies injected with known values, so you can do the mocked import behavior in a clearer and more isolated scope. It's also handy when you want to have a single test file that mocks things in a bunch of different ways, so eg you can load a module with |
I'd thought about naming it |
Hmm, using your example gives me: const fooAccesses = t.intercept(a, 'foo', { value: 'baz' }); Error: Cannot intercept property 'foo', defined {configurable:false} import t from 'tap';
const a = await import('./a.js');
t.equal(a.foo, 'bar');
const fooAccesses = t.intercept(a, 'foo', { value: 'baz' });
t.equal(a.foo, 'baz');
console.error(fooAccesses); |
Oh, yeah, if a thing is But you could always put it on a different object, and intercept that. import t from 'tap';
const a = { ...(await import('./a.js')) };
t.equal(a.foo, 'bar');
const fooAccesses = t.intercept(a, 'foo', { value: 'baz' });
t.equal(a.foo, 'baz');
console.error(fooAccesses); |
Is there an existing issue for this?
Have you read the
CONTRIBUTING
guide on posting bugs, andCODE_OF_CONDUCT
?This issue exists in the latest tap version
Description
mockImport
doesn't seem to mock the module as expected.Reproduction
Environment
The text was updated successfully, but these errors were encountered: