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

Usage inquiry ES6 #133

Open
ukuhnhardt opened this issue May 2, 2022 · 1 comment
Open

Usage inquiry ES6 #133

ukuhnhardt opened this issue May 2, 2022 · 1 comment

Comments

@ukuhnhardt
Copy link

ukuhnhardt commented May 2, 2022

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

  1. In A.spec.ts if I just use rewiremock(() => import('rest-api'), {getData: () => Promise.resolve(mockdata)}) calling from A > rest.api during tests does not invoke the mocked rest-api function.
  2. However if I mock A with
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

https://github.com/theKashey/rewiremock

@theKashey
Copy link
Owner

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.

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

No branches or pull requests

2 participants