Usage inquiry ES6
ukuhnhardt opened this issue · 1 comments
Hi Anton,
I don't quite understand from documentation what approach to take for mocking transient dependencies.
Testing module A.ts in test A.spec.ts
A.ts depends on rest-api.ts (which is auto-generated from a specification).
I need to mock rest-api.ts
- In
A.spec.tsif I just userewiremock(() => import('rest-api'), {getData: () => Promise.resolve(mockdata)})calling from A > rest.api during tests does not invoke the mocked rest-api function. - However if I mock
Awith
const aMock = await rewiremock(() => import('A'), {
"rest-api": {
getData: () => Promise.resolve(mockdata)
}
})
the mock function is invoked during the test!
Am I using the correct pattern here? I was under the impression the rewiremock would mock any module for the test via (1.) and mocking the module under test was not necessary?
Thanks for your insights.
Best, Ulrich
Sources:
https://itnext.io/unit-tests-for-skynet-written-in-js-6704265858a4
The problem here is with import - by the time you run test it might not yet be configured and thus does nothing.
Consider using ‘require’ (will be not typesafe) if you need sync behaviour.