swc-project / jest

Super-fast alternative for babel-jest or ts-jest without type checking. Please use main repository for issues
758 stars 37 forks source link

jest.spyOn(React, 'useEffect').mockImplementation(f => f()); does not invoke the useEffect hook #126

Closed jbizkit closed 2 years ago

jbizkit commented 2 years ago

This works when using ts-jest. But once I moved to @swc/jest it does not work.

I'm using TypeScript 4.3.2, jest 29.1.2, @swc/core 1.3.4, @swc/jest 0.2.23, react 16.8.6, enzyme 3.11.0

TestBadge.tsx

import * as React from 'react';

export const TestBadge: React.FC<{}> = (props) => {
    const [value, setValue] = React.useState<boolean>(false);

    React.useEffect(() => {
        if (value === false) {
            setValue(true);
        }
    }, []);

    return value ? <div id="true">True</div> : <div id="false">False</div>;
};

TestBadge.test.tsx

import * as React from 'react';
import { shallow } from 'enzyme';

jest.spyOn(React, 'useEffect').mockImplementation(f => f());

import { TestBadge } from './TestBadge';

describe('TestBadge', () => {
    it('should trigger useEffect', () => {
        const wrapper = shallow(<TestBadge />);
        expect(wrapper.find('#true').exists()).toBeTruthy();
    });
});

This fails.

kdy1 commented 2 years ago

Please search before filing an issue https://github.com/swc-project/swc/issues/5205