Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Feb 10, 2024
1 parent b117fad commit b8e9f02
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions docs/JestObjectAPI.md
Expand Up @@ -719,32 +719,36 @@ test('logs a warning', () => {
using spy = jest.spyOn(console.warn);
doSomeThingWarnWorthy();
expect(spy).toHaveBeenCalled();
})
});
```

That code is semantically equal to

```js
test('logs a warning', () => {
try {
using spy = jest.spyOn(console.warn);
doSomeThingWarnWorthy();
expect(spy).toHaveBeenCalled();
} finally {
spy.mockRestore()
spy.mockRestore();
}
})
});
```

That way, your spy will automatically be restored to the original value once the current code block is left.

You can even go a step further and use a code block to restrict your mock to only a part of your test without hurting readability.

```js
test('testing something', () => {
{
using spy = jest.spyOn(console.warn);
setupStepThatWillLogAWarning()
setupStepThatWillLogAWarning();
}
// here, console.warn is already restored to the original value
// your test can now continue normally
})
});
```

:::note
Expand Down

0 comments on commit b8e9f02

Please sign in to comment.