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

Manual user mocks not working with custom resolver #4427

Closed
Xiphe opened this issue Sep 5, 2017 · 7 comments
Closed

Manual user mocks not working with custom resolver #4427

Xiphe opened this issue Sep 5, 2017 · 7 comments

Comments

@Xiphe
Copy link

Xiphe commented Sep 5, 2017

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

Bug 🐞

What is the current behavior?

Given I have this jest config:

{
  "resolver": "<rootDir>/customResolver"
}

And a module <rootDir>/index.js
And a manual mock available in <rootDir>/__mocks__/index.js
And a spec file mocking the <rootDir>/index.js module
Then I expect the mocked module to be loaded in other modules and my spec
BUT the original module is still being loaded

When I remove the resolver option in my jest config
Then everything works as expected

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.

https://github.com/Xiphe/jest-custom-resolver-with-mocks

What is the expected behavior?

When I use jest.mock to mock a user module file
Then I expect the mocked module to be loaded
Even If I use a custom resolver

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

Minimal jest config to reproduce this error:

{
  "resolver": "<rootDir>/customResolver"
}

customResolver.js

module.exports = require('jest-resolve/build/default_resolver');

Versions

  • jest: 21.0.1
  • node: 8.4.0
  • npm: 5.4.0
  • OS: macOS 10.12.6
@Xiphe
Copy link
Author

Xiphe commented Sep 5, 2017

This has been introduced with #4174 (de74b38)

When I revert that change and link jest-resolve in my project, everything works as expected.

@cpojer
Copy link
Member

cpojer commented Sep 5, 2017

cc @midzelis

@midzelis
Copy link
Contributor

midzelis commented Sep 5, 2017

I'll take a look. Could be a few days before I have something.

@midzelis
Copy link
Contributor

I have a fix. Just adding a test, and then will create PR

@Xiphe
Copy link
Author

Xiphe commented Sep 18, 2017

Thanks for caring @midzelis!

@peter-mouland
Copy link

Sorry to comment on an old issue, but i'm having this problem.

Is there an example of a simple resolver that does not have this problem?

custom resolver:

const { NodeJsInputFileSystem, CachedInputFileSystem, ResolverFactory } = require("enhanced-resolve");

const resolver = ResolverFactory.createResolver({
        fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
        useSyncFileSystemCalls: true,
// add src to mainFields so we don't need to build local packages before testing
        mainFields: ['src', 'main']
});

module.exports = function(value, options) {
  return resolver.resolveSync({}, options.basedir, value);
};

test snippet

jest.mock('../../src/interpolateValues');
import interpolateValues from '../../src/interpolateValues';
...
interpolateValues.mockImplementation(() => ({ foo: 'hello' }));

error
TypeError: _interpolateValues2.default.mockImplementation is not a function

@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants