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

@swc/jest does not support jest mocking of exported functions within same module #4455

Closed
fdc-viktor-luft opened this issue Apr 27, 2022 · 7 comments

Comments

@fdc-viktor-luft
Copy link

fdc-viktor-luft commented Apr 27, 2022

Describe the feature

Running this:

const swc = require('@swc/core');

const code = `
export const Func1 = () => 'test';
export const Func2 = () => Func1();
`;

swc.transform(code, {
    isModule: true,
    jsc: {
        target: 'es2019',
        parser: {
            syntax: 'typescript',
        },
        transform: {},
    },
    module: {
        type: 'commonjs',
    },
}).then((output) => {
    console.log(output.code);
});

Produces the following output:

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.Func2 = exports.Func1 = void 0;
const Func1 = ()=>'test'
;
exports.Func1 = Func1;
const Func2 = ()=>Func1()
;
exports.Func2 = Func2;

But then the following test code would not work:

import { Func2 } from './example';

jest.mock('./example', () => ({
  ...jest.requireActual('./example'),
  Func1: () => 'mocked',
});

it('should work', () => {
  expect(Func2()).toBe('mocked'); // but actually receiving here 'test'
});

When comparing the produced output to other Transformers, e.g. the following:

const sucrase = require('sucrase');

const code = `
export const Func1 = () => 'test';
export const Func2 = () => Func1();
`;

console.log(sucrase.transform(code, { transforms: ['jsx', 'typescript', 'imports'] }).code);

We get this output:

"use strict";Object.defineProperty(exports, "__esModule", {value: true});
 const Func1 = () => 'test'; exports.Func1 = Func1;
 const Func2 = () => exports.Func1.call(void 0, ); exports.Func2 = Func2;

Therefore I would suggest to change the implementation to use the exports object also when using exported functions inside other functions. E.g. the output fixing the above test should become:

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.Func2 = exports.Func1 = void 0;
const Func1 = ()=>'test'
;
exports.Func1 = Func1;
const Func2 = ()=>exports.Func1()
;
exports.Func2 = Func2;

Babel plugin or link to the feature description

No response

Additional context

No response

@kdy1
Copy link
Member

kdy1 commented Apr 27, 2022

Closing as duplicate. Please search first

@kdy1 kdy1 closed this as completed Apr 27, 2022
@fdc-viktor-luft
Copy link
Author

fdc-viktor-luft commented Apr 27, 2022

I guess you are referring to #4104? It is not always easy to find a matching issue within roughly 300 issues.

And actually I don't agree that this is the same issue.

@kdy1
Copy link
Member

kdy1 commented Apr 27, 2022

I meant #3843

@fdc-viktor-luft
Copy link
Author

fdc-viktor-luft commented Apr 27, 2022

Thanks for sharing. Wasn't looking into the resolved issues.

To summarize here again:

This is expected behavior of swc and some other code transformers are wrong.

In order to fix it, stuff that needs to be mocked should be moved into separate modules.

@kdy1
Copy link
Member

kdy1 commented Apr 27, 2022

I don't think all other are wrong. I didn't say such sentence

@fdc-viktor-luft
Copy link
Author

fdc-viktor-luft commented Apr 27, 2022

Ok. Sorry. I updated my comment to "some". But maybe I didn't fully get this one:

#3843 (comment)

And thanks for being so responsive. Made me consider to migrate our code base to swc instead of surcrase.

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 16, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants