npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

thespian

v3.3.5

Published

A mocking framework with a sophisticated approach to argument matching and helpful error messages when arguments fail to match. Written in Typescript and respects types in mocks.

Downloads

1,165

Readme

thespian

thespian is a mocking framework with a sophisticated approach to argument matching and providing useful error messages when arguments fail to match.

It is written in Typescript and respects types in mocks. It uses mismatched, a sophisticated composable matcher for matching arguments of method and function calls.

Thespians are like mocks - they play a role.

See Thespian By Example

See What is New

Short Docs:

  • To create a Thespian in order to create mocks:
    • const thespian = new Thespian();
  • To create a mock for a class or interface (with a given name, used in error messages):
    • const mockObject = thespian.mock<Check>("check");
    • To specify an expected method call:
      • mockObject.setup(c => c.match()).returns(() => 4);
    • To specify an expected method call to be called a specific number times:
      • mockObject.setup(c => c.match2("target")).returns(() => "ok").times(2);
    • To specify an expected method call throws an exception:
      • mockObject.setup(c => c.match2("target")).throws(new Error("failed"));
  • To create a mock for an object property (with a given name, used in error messages):
    • const mockObject = thespian.mock<Check>("check");
    • To specify an expected property access:
      • mockObject.setup(c => c.prop).returns(() => 4);
    • To specify an expected property access to be called a specific number times:
      • mockObject.setup(c => c.prop).returns(() => 5).times(2);
    • To specify an expected property access results in an exception:
      • mockObject.setup(c => c.prop).throws("error");
  • To create a mock for a function:
    • const mockFn = thespian.mock<(n: number)=>number>("fun");
    • To specify an expected function call:
      • mockFn.setup(f => f(5)).returns(() => 2);
    • To specify an expected function call to be called a specific number times:
      • mockFn.setup(f => f(100)).returns(() => 20).timesGreater(0);
    • To specify an expected function call results in an exception:
      • mockFn.setup(f => f(5)).throws("failed");
  • To access the underlying mock for use in tests:
    • const check = mockCheck.object;
  • To verify that all expected mock calls and property accesses have happened (usually in an afterEach()):
    • thespian.verify();

Mocked methods and function with the same arguments can return a series of results:

- To specify a mocked method call with the same arguments but different results (4 is returned in the first call, and 5 on the second):
  - `mockCheck.setup(c => c.match()).returns(() => 4);`
  - `mockCheck.setup(c => c.match()).returns(() => 5);`
- To specify a mocked property access with different results (4 is returned in the first call, and 5 on the second):
  - `mockCheck.setup(c => c.prop).returns(() => 4);`
  - `mockCheck.setup(c => c.prop).returns(() => 5);`
 - To specify a mocked method function with the same arguments but different results (4 is returned in the first call, and 5 on the second):
   - `mockCheck.setup(c => f(5)).returns(() => 4);`
   - `mockCheck.setup(c => f(5)).returns(() => 5);`

Possible returns:

  • .returns(()=>45), a function that provides the result. The result can depend on the actual arguments. Eg, .returns((a,b) => a).
  • .returnsVoid() for when the mocked method/function does not return a result.

Possible times checks:

  • .times(), a specific number
  • .timesAtLeast(), the minimum number of times
  • .timesAtMost(), the maximum number of times

Possible throws:

  • .throws(new Errow("failed")), a function that specifies that exception to be thrown when called. The result cannot depend on the actual arguments.

Example Error Message

When a call to a mocked method or function fails to match, it's useful to know whether there were any near misses. Here's an example, where there are two near misses:

message

Also see: