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

Bug with classes this keyword #97

Open
a7medm7med opened this issue Jul 31, 2022 · 1 comment
Open

Bug with classes this keyword #97

a7medm7med opened this issue Jul 31, 2022 · 1 comment

Comments

@a7medm7med
Copy link

a7medm7med commented Jul 31, 2022

Take a look at this code:

import { when } from 'jest-when';

class MyName {
	reGetName( name: string ) {
		return name;
	}

	getName( name: string ) {
		return this.reGetName( name );
	}
}

it( 'test my name', () => {
	const component = new MyName();
	jest.spyOn( component, 'getName' );

	when( component.getName )
		.calledWith( 'mark' )
		.mockReturnValue( 'mark' );

	expect( component.getName( 'mark' ) ).toBe( 'mark' ); // Ok
	expect( component.getName( 'john' ) ).toBe( 'john' ); // Error
} );

Once run this test it gives this error:
TypeError: this.reGetName is not a function

The problem is jest-when change the default implementation of the function.

My temporary solution is reset the jest-when implementation by doing something like this:

it( 'test my name 2', () => {
	const component = new MyName();
	jest.spyOn( component, 'getName' );

	when( component.getName )
		.calledWith( 'mark' )
		.mockReturnValue( 'mark' )
		.defaultImplementation( MyName.prototype.getName.bind( component ) );

	expect( component.getName( 'john' ) ).toBe( 'john' ); // Ok
	expect( component.getName( 'mark' ) ).toBe( 'mark' ); // Ok
} );

To solve this issue jest-when should not change the function's implementation if it's not called with the added arguments.

@3y3
Copy link

3y3 commented Oct 22, 2022

Should be fixed by #98

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants