question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle primitive values

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
mhjamcommented, Feb 18, 2020

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)) {

2reactions
maddin1502commented, Jun 11, 2020

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Primitive Data Types (The Java™ Tutorials > Learning the ...
Primitive Data Types ... The Java programming language is statically-typed, which means that all variables must first be declared before they can be...
Read more >
Primitive vs Reference Data Types in JavaScript
Now that we've seen how easy it is to handle primitive data types, let's see how similarly reference data types work.
Read more >
Java: Understanding Primitive Types and Wrapper Objects
Java defines eight primitive data types: byte, short, int, long, float, double, boolean and char. All other variables in java are object ...
Read more >
Java Primitives versus Objects - Baeldung
Java has a two-fold type system consisting of primitives such as int, boolean and reference types such as Integer, Boolean. Every primitive ......
Read more >
Data Types in Java | Primitive and Non-Primitive Data Types
Primitive Data Types: A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found