Comments (2)
Although it would be better to integrate support for getters into the library, it is possible to mock a getter whilst using jest-mock-extended
. Suppose you have an interface like this.
interface Strings {
readonly size: number;
item(index: number): string;
}
Just creating a mock for this interface using mock<Strings>
will allow you to mock the item
method. But since the type of size
is not a function, an expression like mockObject.size.mockReturnValue(3)
is a type error since you cannot call a method on an integer value.
However it is possible to define a property that uses a getter function directly on the implementation object that jest-mock-extended
uses to record the state for its mock object. We need to pass an empty object into the mock<>()
function so that we get a reference to this object. Consider the following code:
// Construct the mockers
const baseMock = {};
const mockStrings = mock<Strings>(baseMock);
const mockSizeGetter = jest.fn<number, []>();
Object.defineProperty(baseMock, 'size', { get: mockSizeGetter });
This code creates an object mockStrings
that mocks the Strings
interface. The jest mock function mockSizeGetter
is set to be the getter function for the size
property. You have to call defineProperty
on the baseMock
object after creating the mocker. Otherwise the mock<>()
function will attempt to alter it, which does not work since there is no setter function.
Now we can prepare the mockers to pretend to be a collection of three strings.
mockSizeGetter.mockReturnValue(3);
mockStrings.item
.mockReturnValueOnce('zero')
.mockReturnValueOnce('one')
.mockReturnValueOnce('two');
The following code demonstrates that the getter have been mocked.
const concatenate = (strings: Strings): string => {
let result = '';
for (let i = 0; i < strings.size; ++i) {
if (i > 0) {
result += ',';
}
result += strings.item(i);
}
return result;
}
expect(concatenate(mockStrings)).toEqual('zero,one,two');
from jest-mock-extended.
Thanks for this example. While I am not working with any projects that are using jest framework at the moment, I will give it a try in the future. =)
from jest-mock-extended.
Related Issues (20)
- 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 1
- 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 2
- 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
- Capture arguments passed to method on mocked object
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.