question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jest.mock() does not work with using backticks e.g. jest.mock(`fs`)

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior?

// Works
jest.mock('fs');

// Doesn't work (backticks instead of single or double quotes)
jest.mock(`fs`);

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

It is not possible to reproduce this on repl.it, but it is pretty simple to test.

  1. Create a __mocks__ directory with a mock for fs
  2. Create a test which jest.mock(`fs`)s using backticks
  3. fs is not mocked

What is the expected behavior?

It should mock fs no matter if single or double quotes or backticks are used.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest: 22.1.4 Node: v8.7.0 npm: 5.6.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Feb 2, 2018
0reactions
github-actions[bot]commented, May 13, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock fs function with jest - node.js - Stack Overflow
The error there is because it is looking for a method called fs.mkdirSync on your logger object, which doesn't exist.
Read more >
jest-mock | Yarn - Package Manager
Creates a new module mocker that generates mocks as if they were created in an environment with the given global object.
Read more >
Mock Functions - Jest
Accepts a string to use in test result output in place of 'jest.fn()' to indicate which mock function is being referenced. For example:...
Read more >
Unit Testing in Typescript/NodeJS - PhalanxHead
Jest is a test and mocking framework built in and for NodeJS. Typescript compatibility has been built over the top of it, but...
Read more >
How to Write Unit Tests for Asynchronous Redux Thunks in ...
So I'm using the older require() syntax, which confers an any type and then we coerce to type jest.Mock . And then the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found