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

TS & Jest mocks #1

Closed
embiem opened this issue Mar 5, 2020 · 2 comments
Closed

TS & Jest mocks #1

embiem opened this issue Mar 5, 2020 · 2 comments

Comments

@embiem
Copy link

embiem commented Mar 5, 2020

Nice Blog!

I recently came across your blog as it was top google search result for jest mockimplementation typescript. Specifically this page: https://klzns.github.io/how-to-use-type-script-and-jest-mocks

This didn't work for me in current TS version, but this did work: kulshekhar/ts-jest#576 (comment)

Maybe you'd consider updating the post as it is first result in Google searches?

Cheers!

@klzns
Copy link
Owner

klzns commented Mar 22, 2020

Hi @embiem, thanks for the feedback!

I've tested the solution proposed in my blog post and it seemed to work fine.

I tested in two ways:

Using create-react-app with TS

I created a fresh install of create-react-app with TS running:

  1. yarn create react-app my-app --template typescript
  2. yarn add @testing-library/react @testing-library/jest-dom

And I created two examples:

  1. Example mocking a node module
  2. Example mocking a local module

That's the output of the tests:

image

In this test I used TypeScript v3.7.5.

Using react-boilerplate-typescript

Since the first test worked fine, I thought the issue might be related to the usage of ts-jest.

So I also created a branch using the react-boilerplate-typescript that someone mentioned in the thread.

With the same two examples:

  1. Example mocking a node module
  2. Example mocking a local module

And got the same results:

image

Things to check

Make sure you have @types/jest in your project devDependencies or dependencies.

Let me know if you find what I'm missing here! Thanks

@embiem
Copy link
Author

embiem commented Mar 22, 2020

You're right, it does work. I'm honestly not 100% sure why it didn't work back when I posted this issue.

I remember that I tried your approach, which didn't work at the time, then came across the solution using ts-jest/utils (kulshekhar/ts-jest#576 (comment)), which worked perfectly.

Thanks for taking the time to double-check this!

@embiem embiem closed this as completed Mar 22, 2020
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