Skip to content

nextResult/nextError return the internal state, not the spy, so chaining throws #60

Open

Description

@shaurya703

Spy declares both as returning this:

nextError(error: any): this
nextResult(result: R): this

but populateSpy returns state from each:

https://github.com/tinylibs/tinyspy/blob/main/src/internal.ts

defineValue(spy, 'nextError', (error: any) => {
  state.next.push(['error', error])
  return state
})
defineValue(spy, 'nextResult', (result: R) => {
  state.next.push(['ok', result])
  return state
})

The internal state has no nextResult/nextError of its own, so the chain the types promise compiles and then throws:

const s = spy(() => 'real')

s.nextResult('a').nextResult('b')
// TypeError: s.nextResult(...).nextResult is not a function
console.log(s.nextResult('a') === s) // false

Calling them as separate statements works fine, so this only bites chaining.

willCall in internalSpyOn does return spy, which is presumably the intent here too — happy to send a PR making these return the spy if that's the direction you'd want.

Found while writing the API reference in #59; I left chaining out of the docs because of this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions