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 16 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
26 changes: 16 additions & 10 deletions docs/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@ 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
Expand Down
33 changes: 18 additions & 15 deletions website/versioned_docs/version-25.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

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"
import {forEach} from './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 function', () => {
forEach([0, 1], mockCallback);

// 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 first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// 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 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);
// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -44,12 +49,10 @@ All mock functions have this special `.mock` property, which is where data about

```javascript
const myMock = jest.fn();

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

Jeroendevr marked this conversation as resolved.
Show resolved Hide resolved
console.log(myMock.mock.instances);
// > [ <a>, <b> ]
```
Expand All @@ -58,7 +61,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
46 changes: 27 additions & 19 deletions website/versioned_docs/version-26.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,56 @@ 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 mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

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

// 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 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);
// 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:

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down Expand Up @@ -105,8 +113,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
42 changes: 25 additions & 17 deletions website/versioned_docs/version-27.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,56 @@ 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 mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

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

// 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 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);
// 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:

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
31 changes: 17 additions & 14 deletions website/versioned_docs/version-28.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@ 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 mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

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

// 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 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);
// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -60,7 +66,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand All @@ -71,9 +77,6 @@ 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
31 changes: 17 additions & 14 deletions website/versioned_docs/version-29.0/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@ 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 mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

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

// 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 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);
// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -60,7 +66,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand All @@ -71,9 +77,6 @@ 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
Loading