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: add section about resolves and removal of Promise unwrapping #44

Merged
merged 6 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
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
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# tinyspy

> minimal fork of nanospy, with more features 🕵🏻‍♂️
Expand Down Expand Up @@ -60,19 +59,39 @@ console.log(spied.calls) // []
console.log(spied.returns) // []
```

If you have async implementation, you need to `await` the method to get awaited results (if you don't, you will get a `Promise` inside `results`):
Since 3.0, tinyspy doesn't unwrap the Promise in `returns` and `results` anymore, so you need to await it manually:

```js
const spied = spy(async (n) => n + '!')

const promise = spied('a')

console.log(spied.called) // true
console.log(spied.returns) // [Promise<'a!'>]
console.log(spied.results) // ['ok', Promise<'a!'>]

await promise

console.log(spied.returns) // ['a!']
console.log(spied.results) // ['ok', Promise<'a!'>]

console.log(await spied.returns[0]) // 'a!'
```

> [!WARNING]
> This also means the function that returned a Promise will always have result type `'ok'` even if the Promise rejected

Tinyspy 3.0 still exposes resolved values on `resolves` property:

```js
const spied = spy(async (n) => n + '!')

const promise = spied('a')

console.log(spied.called) // true
console.log(spied.resolves) // [] <- not resolved yet

await promise

console.log(spied.resolves) // ['ok', 'a!']
```

### spyOn
Expand Down
Loading