Comments (5)
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)) {
from jest-mock-extended.
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?
from jest-mock-extended.
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)
from jest-mock-extended.
Is there any update on this? It would really be useful to the current project I'm testing.
from jest-mock-extended.
unfortunately since Jest does not support this, the only way to add this is to do as above. PRs welcome
from jest-mock-extended.
Related Issues (20)
- Observable.of mock interface implementation HOT 2
- Option to have mock methods throw by default HOT 2
- description field for Custom Matcher should be optional HOT 2
- Mocks evaluate to `undefined` when used with expect.anything() or expect.any()
- how to define overridable default mock implementation HOT 3
- Doesn't mock special fn in prisma package
- `.calledWith` ignores plain objects as its condition HOT 2
- Support for TypeScript 5.0 HOT 1
- Required to use calledWith when using TypeScript HOT 2
- Typescript error when using @jest/globals
- Doesn't work if injectGlobals is false HOT 2
- The captor function causes type errors when used with `calledWith`.
- How to deep-compare objects with `calledWith`? HOT 1
- Cannot set mock return value on `toString`
- TypeError: Jest: a transform must export a `process` function.
- How to mock an external package with deepMock HOT 1
- How to compare mock to an object with jest HOT 3
- Extreme slow processing of mocks that are created via the mock() function
- Add support for Jest v30
- Type 'DeepNullable<K>' does not satisfy the constraint 'WeakKey' HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-mock-extended.