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

fix(babel-plugin-jest-hoist): allow using module wrapper arguments in hoisted mock factory function #10903

Merged
merged 3 commits into from Dec 3, 2020

Conversation

theneva
Copy link
Contributor

@theneva theneva commented Dec 2, 2020

Summary

This allows several things. My specific case is using the content of a file as the return value for a mocked function:

jest.mock('some-module', () => {
  const fs = require('fs');
  const path = require('path');

  return {
    someFunction() {
      const pathToHtmlFile = path.resolve(__dirname, 'some-fixture.html');
      const html = fs.readFileSync(pathToHtmlFile);
      return html;
    },
  };
});

The test is located inside a Yarn workspace, and for complicated reasons I have to execute jest from the root of the repository. As far as I can tell, this is the only way I can avoid letting the test know about its own directory path in the repo.

Test plan

To be honest, I'm just doing what @SimenB told me would solve my problem.

I applied this patch to my own repo, and it works. Does it make sense to document this further?

@theneva theneva changed the title fix(babel-plugin-jest-hoist): allow __dirname and __filename in hoisted mock factory function fix(babel-plugin-jest-hoist): allow using module wrapper arguments in hoisted mock factory function Dec 2, 2020
'require',
'module',
'__filename',
'__dirname',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I grouped these with require in the order that the module wrapper passes them

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! just missing a changelog entry 🙂

@SimenB SimenB merged commit 149502d into jestjs:master Dec 3, 2020
@theneva theneva deleted the patch-1 branch December 3, 2020 21:38
@github-actions
Copy link

This pull request 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 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants