Replies: 1 comment
-
The challenge here is that "mock" and "import" are both somewhat overloaded terms.
So for example, if you had this: // a.js
import { foo } from './b.js'
export const fooInA = foo t.test('', async t => {
const { fooInA } = await t.mockImport('./a.js', {
// when a.js imports from './b.js', it gets this object instead
// of loading the actual contents of ./b.js
'./b.js': { foo: 'mocked foo' },
})
t.equal(fooInA, 'mocked foo')
}) If you want to import something, and then apply some mocks to it, you can use import a from './a.js'
const fooBaz = t.createMock(a, { foo: 'baz' })
// imports are a null-prototype object with configurable:false properties
// so make a plain old object out of it first.
const aPlainObj = { ...a }
const aIntercepted = t.intercept(aPlainObj, 'foo', { value: 'baz' })
t.equal(aPlainObj.foo, 'baz')
aPlainObj.foo = 'bar'
t.equal(aPlainObj.foo, 'bar')
// now aIntercepted has info about the get and set operations
// and when the test ends, the intercept is removed and the property
// goes back to how it was before the test mucked with it. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The module isn't getting mocked:
Beta Was this translation helpful? Give feedback.
All reactions