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

Constant can not be mock correcly #87

Closed
mathieu-suen-sonarsource opened this issue Mar 17, 2022 · 1 comment
Closed

Constant can not be mock correcly #87

mathieu-suen-sonarsource opened this issue Mar 17, 2022 · 1 comment

Comments

@mathieu-suen-sonarsource

When a module export a constant:

export const FOO = 3;

export function getFoo() {
  return FOO;
}

In Jest you can mock using:

jest.mock('./foo', () => {
   const { getFoo  } = jest.requireActual('./foo');
   return {
     FOO: 5,
     getFoo
   }
})

This will not work correctl because SWC is not using the export constant but an internal constant in the module

test('should be mock',  () => {
   expect(getFoo()).toBe(5); // Fail
})

The transpile code looks like:

const FOO = 3
exports.FOO = FOO;

exports.getFoo = function() {
    return FOO; // It should be exports.FOO in order to work.
};
@kdy1
Copy link
Member

kdy1 commented Mar 17, 2022

This is not an issue of swc.
See swc-project/swc#3843

@kdy1 kdy1 closed this as completed Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants