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.

Why manual mock by creating manual mock file in __mocks__ folder not work

See original GitHub issue

I tried mock method in ES6 class, by creating manual mock file in __mock__folder, but mock file not working at all. Here is my project:

├── __mocks__
│   └── MockTestDependency.js
├── MockTestDependency.js
└── MockTest.test.js
// ./MockTestDependency.js
export default class MockTestDependency {
    addAction(value1, value2) {
        return value1 + value2
    }
}

// ./__mocks__/MockTestDependency.js
export default {
    addAction: jest.fn((value1, value2) => {
        return value1 + value2 + 1
    })
}

// ./MockTest.test.js
import MockTestDependency from "./MockTestDependency"

beforeAll(() => {
    jest.mock('MockTestDependency')
})

describe('mock test', () => {
    it('first case', () => {
        let mockTestDependency = new MockTestDependency()
        expect(mockTestDependency.addAction(1, 2)).toBe(4)
    })
})

Any help would be appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
thymikeecommented, Mar 1, 2018

still cannot repro

0reactions
github-actions[bot]commented, May 12, 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

Manual mocking using __mocks__ not working - Stack Overflow
I went through jest documentation for manual mocking and tried creating mocks folder as instructed. Please find the folder structure below.
Read more >
Manual Mocks - Jest
Manual mocks are used to stub out functionality with mock data. For example, instead of accessing a remote resource like a website or...
Read more >
Mock defined in __mocks__ directory not being ... - GitHub
I have a file named app.js. Immediately adjacent to that I have a manual __mocks__/app.js file. I have a test that both requires...
Read more >
Manual Mocks - Jest - w3resource
When a given module has a manual mock, Jest's module system uses that module when explicitly calling jest.mock('moduleName'). However, when you ...
Read more >
How To Do Manual Mock In Jest Unit Testing | by Kris Ma
A much powerful but uncommon way of mocking service in jest unit test ... Jest has two ways to mock functions: Either by...
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