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

docs: runnable example of the for Each test #13428

Merged
merged 35 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
65cfe82
Runnable example of the for Each test
Oct 11, 2022
6f43205
Merge branch 'facebook:main' into patch-1
Dec 14, 2022
3a88b69
Update website/versioned_docs/version-29.1/MockFunctions.md
Dec 15, 2022
38afc48
Update website/versioned_docs/version-29.1/MockFunctions.md
Dec 15, 2022
8ef4867
Changed docs around all the versions for mockFunc
Dec 15, 2022
9398746
single quotes
Dec 15, 2022
4753f97
basic working example
Dec 15, 2022
01bf339
.toHaveLength()
Dec 15, 2022
5ae1f95
Update website/versioned_docs/version-25.x/MockFunctions.md
Dec 16, 2022
ebc6112
also update docs main dir + prettier lint
Dec 16, 2022
544a9c7
Calls.length -> toHaveLength()
Dec 16, 2022
05cca48
ESM Syntax
Dec 16, 2022
5bd950f
change title to forEach
Dec 16, 2022
c98ed64
undo unmeant doc changes to v25
Dec 16, 2022
1222c5d
remove mymock1
Dec 16, 2022
988db29
space
Dec 16, 2022
2414a55
Empty lines undov25
Dec 19, 2022
2f7efae
/docs adjustment
Dec 19, 2022
f467915
V28 mockFunctions
Dec 19, 2022
9aaa5dd
mockfunction v26
Dec 19, 2022
6cf4e4a
mockFunction v29
Dec 19, 2022
f1cae77
mockfunction v29.1
Dec 19, 2022
b1eac2c
mockFunctions 29.2
Dec 19, 2022
817d3cd
mockFunctions v29.3
Dec 19, 2022
11aed76
docs type foEach
Dec 19, 2022
9b893b2
context mock functions
Dec 19, 2022
408dc62
seperate line filter test
Dec 19, 2022
f75b8a4
mockcontext 29.0
Dec 19, 2022
e01bb92
mockcontext v29.1
Dec 19, 2022
25dbb66
mock.bind revert from v28 onwards
Dec 21, 2022
3658f8c
.mock prop
Dec 21, 2022
77be83a
remove space
Dec 21, 2022
b0d31c1
enter and close function block
Dec 21, 2022
737fb4d
close function block
Dec 21, 2022
0d91ee2
v28 cloase block
Dec 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions website/versioned_docs/version-25.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,49 @@ function forEach(items, callback) {
callback(items[index]);
}
}
module.exports = forEach;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thought. What about using ESM syntax here? The examples below written in ESM: https://jestjs.io/docs/next/mock-functions#mocking-modules

Copy link
Contributor Author

@Jeroendevr Jeroendevr Dec 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESM syntax is then export forEach; ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is:

export function forEach(items, callback) {
  // etc ...

And instead of const forEach = require('./foreach');:

import {forEach} from './forEach';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I thought there were two ways to export a module.
One as you describe it. Add export before the function declaration and one where you can group all the functions, classes you want to export together (for example at the bottom)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok if v25 is acceptable I'll change the others as well. Thanks for learning on the job.

```

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="foreach.test.js"
Jeroendevr marked this conversation as resolved.
Show resolved Hide resolved
Jeroendevr marked this conversation as resolved.
Show resolved Hide resolved
const forEach = require('./foreach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
Jeroendevr marked this conversation as resolved.
Show resolved Hide resolved
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);
Jeroendevr marked this conversation as resolved.
Show resolved Hide resolved

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
```
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
}
)
```
## `.mock` property

All mock functions have this special `.mock` property, which is where data about how the function has been called and what the function returned is kept. The `.mock` property also tracks the value of `this` for each call, so it is possible to inspect this as well:

```javascript
const myMock = jest.fn();
const myMock1 = jest.fn();
const a = new myMock1();
console.log(myMock1.mock.instances);
// > [ <a> ]

const a = new myMock();
const myMock2 = jest.fn();
const b = {};
const bound = myMock.bind(b);
const bound = myMock2.bind(b);
bound();

console.log(myMock.mock.instances);
// > [ <a>, <b> ]
console.log(myMock2.mock.contexts);
// > [ <b> ]
```

These mock members are very useful in tests to assert how these functions get called, instantiated, or what they returned:
Expand All @@ -69,12 +77,18 @@ expect(someMockFunction.mock.calls[0][1]).toBe('second arg');
// The return value of the first call to the function was 'return value'
expect(someMockFunction.mock.results[0].value).toBe('return value');

// The function was called with a certain `this` context: the `element` object.
expect(someMockFunction.mock.contexts[0]).toBe(element);

Copy link
Contributor

@mrazauskas mrazauskas Dec 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. I guess these are accidental changes. Jest v25 does not have mock.contexts it was introduced recently. That is why it appears only in the docs of later versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah my bad. Glad you paid good attention to it.

// This function was instantiated exactly twice
expect(someMockFunction.mock.instances.length).toBe(2);

// The object returned by the first instantiation of this function
// had a `name` property whose value was set to 'test'
expect(someMockFunction.mock.instances[0].name).toBe('test');

// The first argument of the last call to the function was 'test'
expect(someMockFunction.mock.lastCall[0]).toBe('test');
```

## Mock Return Values
Expand Down Expand Up @@ -105,8 +119,8 @@ const result = [11, 12].filter(num => filterTestFn(num));

console.log(result);
// > [11]
console.log(filterTestFn.mock.calls);
// > [ [11], [12] ]
console.log(filterTestFn.mock.calls[0][0]); // 11
console.log(filterTestFn.mock.calls[1][0]); // 12
```

Most real-world examples actually involve getting ahold of a mock function on a dependent component and configuring that, but the technique is the same. In these cases, try to avoid the temptation to implement logic inside of any function that's not directly being tested.
Expand Down
52 changes: 33 additions & 19 deletions website/versioned_docs/version-26.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,49 @@ function forEach(items, callback) {
callback(items[index]);
}
}
module.exports = forEach;
```

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="foreach.test.js"
const forEach = require('./foreach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
```
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
}
)
```
## `.mock` property

All mock functions have this special `.mock` property, which is where data about how the function has been called and what the function returned is kept. The `.mock` property also tracks the value of `this` for each call, so it is possible to inspect this as well:

```javascript
const myMock = jest.fn();
const myMock1 = jest.fn();
const a = new myMock1();
console.log(myMock1.mock.instances);
// > [ <a> ]

const a = new myMock();
const myMock2 = jest.fn();
const b = {};
const bound = myMock.bind(b);
const bound = myMock2.bind(b);
bound();

console.log(myMock.mock.instances);
// > [ <a>, <b> ]
console.log(myMock2.mock.contexts);
// > [ <b> ]
```

These mock members are very useful in tests to assert how these functions get called, instantiated, or what they returned:
Expand All @@ -69,12 +77,18 @@ expect(someMockFunction.mock.calls[0][1]).toBe('second arg');
// The return value of the first call to the function was 'return value'
expect(someMockFunction.mock.results[0].value).toBe('return value');

// The function was called with a certain `this` context: the `element` object.
expect(someMockFunction.mock.contexts[0]).toBe(element);

// This function was instantiated exactly twice
expect(someMockFunction.mock.instances.length).toBe(2);

// The object returned by the first instantiation of this function
// had a `name` property whose value was set to 'test'
expect(someMockFunction.mock.instances[0].name).toBe('test');

// The first argument of the last call to the function was 'test'
expect(someMockFunction.mock.lastCall[0]).toBe('test');
Jeroendevr marked this conversation as resolved.
Show resolved Hide resolved
```

## Mock Return Values
Expand Down Expand Up @@ -105,8 +119,8 @@ const result = [11, 12].filter(num => filterTestFn(num));

console.log(result);
// > [11]
console.log(filterTestFn.mock.calls);
// > [ [11], [12] ]
console.log(filterTestFn.mock.calls[0][0]); // 11
console.log(filterTestFn.mock.calls[1][0]); // 12
```

Most real-world examples actually involve getting ahold of a mock function on a dependent component and configuring that, but the technique is the same. In these cases, try to avoid the temptation to implement logic inside of any function that's not directly being tested.
Expand Down
45 changes: 28 additions & 17 deletions website/versioned_docs/version-27.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,49 @@ function forEach(items, callback) {
callback(items[index]);
}
}
module.exports = forEach;
```

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="foreach.test.js"
const forEach = require('./foreach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
```
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
}
)
```
## `.mock` property

All mock functions have this special `.mock` property, which is where data about how the function has been called and what the function returned is kept. The `.mock` property also tracks the value of `this` for each call, so it is possible to inspect this as well:

```javascript
const myMock = jest.fn();
const myMock1 = jest.fn();
const a = new myMock1();
console.log(myMock1.mock.instances);
// > [ <a> ]

const a = new myMock();
const myMock2 = jest.fn();
const b = {};
const bound = myMock.bind(b);
const bound = myMock2.bind(b);
bound();

console.log(myMock.mock.instances);
// > [ <a>, <b> ]
console.log(myMock2.mock.contexts);
// > [ <b> ]
```

These mock members are very useful in tests to assert how these functions get called, instantiated, or what they returned:
Expand All @@ -69,6 +77,9 @@ expect(someMockFunction.mock.calls[0][1]).toBe('second arg');
// The return value of the first call to the function was 'return value'
expect(someMockFunction.mock.results[0].value).toBe('return value');

// The function was called with a certain `this` context: the `element` object.
expect(someMockFunction.mock.contexts[0]).toBe(element);

// This function was instantiated exactly twice
expect(someMockFunction.mock.instances.length).toBe(2);

Expand Down
28 changes: 17 additions & 11 deletions website/versioned_docs/version-28.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@ function forEach(items, callback) {
callback(items[index]);
}
}
module.exports = forEach;
```

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="foreach.test.js"
const forEach = require('./foreach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
```
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
}
)
```
## `.mock` property

All mock functions have this special `.mock` property, which is where data about how the function has been called and what the function returned is kept. The `.mock` property also tracks the value of `this` for each call, so it is possible to inspect this as well:
Expand Down
28 changes: 17 additions & 11 deletions website/versioned_docs/version-29.0/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@ function forEach(items, callback) {
callback(items[index]);
}
}
module.exports = forEach;
```

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="foreach.test.js"
const forEach = require('./foreach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
```
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
}
)
```
## `.mock` property

All mock functions have this special `.mock` property, which is where data about how the function has been called and what the function returned is kept. The `.mock` property also tracks the value of `this` for each call, so it is possible to inspect this as well:
Expand Down
Loading