jest-mock-extended icon indicating copy to clipboard operation
jest-mock-extended copied to clipboard

Handle primitive values

Open jgornick opened this issue 5 years ago • 5 comments
trafficstars

I see some benefit to being able for mock<...>() to also mock primitive values.

For example:

interface Baz {
  quux: boolean
}

interface Foo {
  bar: string
  baz: Baz
}

const foo = mock<Foo>()

// modifidy mock return value for quux
foo.baz.quux.mockReturnValueOnce(false)

Refer to #3 and https://github.com/reergymerej/super-mockable-non-functions/blob/master/src/index.js

Thoughts?

jgornick avatar Dec 11 '19 21:12 jgornick

I'll take a look at this, but suspect that it will not work as you have specified there given how jest works and how I've implemented deep mocks.

It may require something like :

  mock<Foo>({
    baz: {
        quux: primitiveMock().mockReturnValueOnce(false)
   }
)

or

const foo = mock<Foo>()

// modifidy mock return value for quux
primitiveMock(foo.baz.quux).mockReturnValueOnce(false)

marchaos avatar Dec 16 '19 20:12 marchaos

Is there any update on this? It would really be useful to the current project I'm testing.

perfectmak avatar Jan 28 '20 22:01 perfectmak

unfortunately since Jest does not support this, the only way to add this is to do as above. PRs welcome

marchaos avatar Jan 29 '20 16:01 marchaos

My current workaround is this:

	it('should mock a primitive', () => {
		const quuxGetter = jest.fn();
		const foo = mock<Foo>(); // mockDeep does not work!
		Object.defineProperty(foo.baz, 'quux', {
			get: quuxGetter
		});
		quuxGetter.mockReturnValue(false);

		expect(foo.baz.quux).toBe(false);
		expect(quuxGetter).toHaveBeenCalledTimes(1);
	});

If you do not need spying capabilities and just want quux to have a fixed value, you can do

	it('should mock a primitive', () => {
		const foo = mock<Foo>(); // mockDeep does not work!
		Object.defineProperty(foo.baz, 'quux', {
			get: () => false
		});

		expect(foo.baz.quux).toBe(false);
	});

But this seems to be more complicated then necessary. It should be possible to write

	it('should mock a primitive', () => {
		const foo = mock<Foo>();
		foo.baz.quux = false;

		expect(foo.baz.quux).toBe(false);
	});

And actually, this does work (even with mockDeep), but not for falsy values like in the example. I believe this is due to line 86 in Mock.ts, which reads if (!obj[property]) { but should rather be if (!(property in obj)) {

mhjam avatar Feb 18 '20 12:02 mhjam

this is due to line 86 in Mock.ts, which reads if (!obj[property]) { but should rather be if (!(property in obj)) {

I tested this solution and it works great. Why is this fix not committed yet?

maddin1502 avatar May 19 '20 08:05 maddin1502