6. Mocking Functions and Modules Flashcards

1
Q

What are the advantages of mocking?

A
  1. Reduce dependencies required by tests, allowing faster execution.
  2. Prevent “side effects” during testing.
  3. Build custom mocks to facilitate desired testing procedures.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a mock?

A

A convincing duplicate of an object with no internal workings — it has the same API as the original, but no side effects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can a mock be created?

A

Automatically or manually.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the process of mocking?

A
  1. Scan the original object for methods, give the new object spy methods with the same names.
  2. Ensure that any methods which returned a promise still return a promise in the mock.
  3. Create mocks for any complex values that are returned from methods that are required for tests.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are mock functions also known as?

A

Spies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are spies?

A

Mock functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Do spies have side effects?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the six characteristics of mock functions?

A
  1. Also known as “spies”.
  2. No side-effects.
  3. Count function calls.
  4. Records arguments passed when called.
  5. Can be “loaded” with return values.
  6. Return value must approximate original.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In what way do spies track the mocked function?

A

They count function calls and record the arguments passed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Where must mocks reside?

A

In a __mocks__ folder next to the mocked module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What modules can be mocked?

A

Bot NPM modules and the local modules can be mocked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If you mock an npm package using its original name inside the [root]/__mocks__ folder, what happens when you try to load that npm module in your tests?

A

Your mock is loaded instead of the npm package’s function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you define a spy with a specific return value?

A

By using jest.fn() and passing it an anonymous function that returns the value.

eg. `const mySpy = jest.fn(() => “foo”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you assert that a spy function was called with a specific parameter?

A

By using the .toHaveBeenCalledWith and passing it the parameter you expect it to have received.

eg. `expect(mySpy).toHaveBeenCalledWith(‘foo’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which one takes precedence, manual or automatic mocks?

A

Manual mocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the four main properties of manual mocking?

A
  1. Exists as a separate file alongside the file being mocked.
  2. Will be used automatically for NPM modules.
  3. Requires more work than automatic mocks.
  4. Needs to be updated when mocked file changes.
17
Q

What are the four main properties of automatic mocking?

A
  1. Most modules can be automatically replaced with mocks.
  2. Usually generated correctly, but sometimes not!
  3. Greatly reduced the likelihood of side effects during testing.
  4. Developer must use discretion.
18
Q

What are the N automatic mocking challenges?

A
  1. Methods returning a specific and complex value often can’t be mocked automatically.
  2. Methods that are not part of your module at compile-time won’t be mocked.
  3. Modules that you did not expect to be mocked may be mocked.
19
Q

What can be said about manual vs automatic mocking when it comes to simple vs advanced modules?

A

Simple modules can be automatically mocked whereas advanced ones may need manual mocking.

20
Q

How are the manual mocks of modules created?

A

By placing a correctly named file in the \_\_mocks\_\_ directory.