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 imported modules having problems with babel 7 #7118

Closed
imjeremyhi opened this issue Oct 8, 2018 · 2 comments
Closed

Mocking imported modules having problems with babel 7 #7118

imjeremyhi opened this issue Oct 8, 2018 · 2 comments

Comments

@imjeremyhi
Copy link

imjeremyhi commented Oct 8, 2018

🐛 Bug Report

The below code does not work, with the error.
Cannot set property bar of [object Object] which has only a getter
import * as foo from './foo'; foo.bar = jest.fn();

Edit: After further investigation I've isolated it to the use case - Importing and then re-exporting objects/modules within a single object causes these objects/modules to become immutable which means I cannot stub them. Is this a known issue/is there a known work around?

To Reproduce

My source code looks something like this: https://repl.it/repls/SunnyRuddyTheory, but instead of the require I do: import * as abc from './add. This test passes since its using the babel 6 transpiled version, but with babel 7, it does not work.

Environment:

It works when used with babel 6, but does not work with babel 7.
It looks like babel has made this import * syntax such that the variable cannot be overridden. I have tested overriding the module works with default imports, but does not work with { Foo } object destructured imports nor import * imports.

Expected behavior

This should work with babel 7? Documented approaches such as:
const spy = jest.spyOn(video, 'play'); would no longer work if this isn't resolved.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS Sierra 10.12.6
    CPU: x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 10.9.0 - ~/.nvm/versions/node/v10.9.0/bin/node
    Yarn: 1.9.2 - /usr/local/bin/yarn
    npm: 6.2.0 - ~/.nvm/versions/node/v10.9.0/bin/npm
  npmPackages:
    jest: ^23.4.2 => 23.4.2
@imjeremyhi imjeremyhi changed the title Setting imports not working with babel 7 Mocking imported modules having problems with babel 7 Oct 8, 2018
@thymikee
Copy link
Collaborator

thymikee commented Jan 8, 2019

We've upgraded Jest to use Babel 7, so this shouldn't be an issue anymore. Please test with jest@beta and let us know :)

@thymikee thymikee closed this as completed Jan 8, 2019
@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

No branches or pull requests

3 participants