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 modules and __mocks__/<module>/index.js #4439

Closed
hongymagic opened this issue Sep 6, 2017 · 4 comments · Fixed by #6232
Closed

Mocking modules and __mocks__/<module>/index.js #4439

hongymagic opened this issue Sep 6, 2017 · 4 comments · Fixed by #6232

Comments

@hongymagic
Copy link

hongymagic commented Sep 6, 2017

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

Potential bug as described behaviour below could be intentional.

What is the current behavior?

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.

Currently when creating a mock for modules, you can add it to root __mocks__ directory. And it also supports sub-modules like so:

# __mocks__/@module/x.js
// add some mocked implementation

# some.test.js
jest.mock('@module/x');
import x from '@module/x';

However it fails to recognise the same LOAD_INDEX (or even perhaps LOAD_AS_DIRECTORY resolver behaviour as documented in Node.js documentation. For example, this won't work:

# __mocks__/module/index.js
// add some mocked implementation

# some.test.js
jest.mock('module');
import x from 'module';

Couldn't or didn't know how to create a global mock on repl.it so here's a contrived example:

# main.js
import Twilio from 'twilio';
const twilio = new Twilio('x', 'y');

twilio.messages.create({}).then(console.log).catch(console.error);

# __mocks__/twilio/index.js <-- this is the issue
export default class Twilio {
  constructor() {}

  get messsages() {
    return {
      create: async function (options) { console.log(options); }
    }
  }
}

# main.test.js
jest.mock('twilio');

import main from './main';
main();

throws

[TypeError: Cannot read property 'messages' of undefined]

However if I do this it works as expected:

$ mv __mocks__/twilio/index.js __mocks__/twilio.js

What is the expected behavior?

Jest recognises __mocks__/twilio/index.js as module twilio. I understand that the current behaviour could be intentional, in which case we should proceed by documenting it.

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

# package.json
{
  ...,
  "jest": {
    "testEnvironment": "node"
  }
}
@samhunta
Copy link

samhunta commented Oct 3, 2017

@hongymagic was it a typo:

  get messsages() {

@jamietre
Copy link
Contributor

Just came across this. Still a bug in 23.0.0-charlie.2 unless I'm doing it wrong. I don't suppose you've found a workaround?

@SimenB
Copy link
Member

SimenB commented May 19, 2018

Definitely a bug. Dug a bit into it, and the issue is that when the resolver queries the module map for mocks here: https://github.com/facebook/jest/blob/f4f51914d577dc53748f599f3f626dfe3154883e/packages/jest-resolve/src/index.js#L230, the mock is saved as twilio/index here: https://github.com/facebook/jest/blob/f4f51914d577dc53748f599f3f626dfe3154883e/packages/jest-haste-map/src/module_map.js#L58-L60 and not twilio.

I'm not sure how to fix it though. Help welcome!

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants