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

Feature request: better toString() outputs showing expected value #229

Open
frstie opened this issue Aug 20, 2022 · 2 comments
Open

Feature request: better toString() outputs showing expected value #229

frstie opened this issue Aug 20, 2022 · 2 comments

Comments

@frstie
Copy link

frstie commented Aug 20, 2022

When verifying parameters, the various matchers report the value they were expecting, but not the value they received. This sometimes makes it tricky to figure out what went wrong when the test fails.

Simple example:

interface Emailer {
  send(email: string): void
}

describe("Notifier", () => {
  it("sends correct email", () => {
    let notifier = (emailer: Emailer) => {
      emailer.send("[email protected]")
    }

    let emailer = mock<Emailer>()

    notifier(instance(emailer))

    verify(emailer.send("[email protected]")).once()
  })
})

This results in an error message:

1) Notifier
       sends correct email:
     Error: Expected "send(strictEqual([email protected]))" to be called 1 time(s). But has been called 0 time(s).

I've put together a quick custom matcher that improves toString() to report the value received as well as the requested match:

class StringMatcher extends Matcher {
  private received: string = ""

  constructor(private expected: string) {
    super()
  }

  public match(value: any): boolean {
    this.received = value
    return this.expected === value
  }

  public toString(): string {
    return `string(expected: ${this.expected}, got: ${this.received})`
  }
}

export function stringMatcher(expected: string): any {
  return new StringMatcher(expected) as any
}

The new call to verify:

verify(emailer.send(stringMatcher("[email protected]"))).once()

and the new output:

1) Notifier
       sends correct email:
     Error: Expected "send(string(expected: [email protected], got: [email protected]))" to be called 1 time(s). But has been called 0 time(s).

I'm a new user to the library, wondered if there would be any interest in making the default toString() on the existing matchers support something like this? Happy to contribute a PR if this would be of use to anyone?

@frstie
Copy link
Author

frstie commented Aug 20, 2022

Ah, I've just spotted #216 - guess this is handled more elegantly with that PR.

Just found the thread about the various forks - is there any consensus on future directions/merging for the different forks?

@mikeporterdev
Copy link
Contributor

@frstie this feature is now released under the TypeStrong repo https://github.com/TypeStrong/ts-mockito
Another PR for making it handle objects nicely is in progress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants