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

Mocking dependency module class, constructor and it's methods (ESM/Typescript) #485

Closed
3 tasks
ukuhnhardt opened this issue May 5, 2022 · 1 comment
Closed
3 tasks

Comments

@ukuhnhardt
Copy link

ukuhnhardt commented May 5, 2022

Description

When moduleUnderTest.ts has a dependency to DependencyClass.ts with constructor and class-scope function classDoSomething() I am failing to mock DependencyClass for testing moduleUnderTest.ts.

Issue

moduleUnderTest.ts invokes new DependencyClass(options).classDoSomething(args)

const mockDependencyClass = td.replace('path/to/DependencyClass') is fine,

  1. How do I mock the constructor?
  2. How do I mock class-scope functions?
  3. How do I use the td.when() pattern on class scope methods?

Environment

  • node -v output: v16.15.0
  • npm -v (or yarn --version) output: 8.5.5
  • npm ls testdouble (or yarn list testdouble) version: └── testdouble@3.16.3

Thanks for maintaining td - it's an awesome tool. The documentation is outstanding

@searls
Copy link
Member

searls commented May 6, 2022

It's been a few years since I've really used classes in JS in earnest. I'd take a look at this test in our suite as an example of how the API works: https://github.com/testdouble/testdouble.js/blob/main/test/safe/constructor.test.js

If you have any questions or hit a bug, please provide a reproducible example

@searls searls closed this as completed May 6, 2022
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