thespian
TypeScript icon, indicating that this package has built-in type declarations

3.3.5 • Public • Published

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:

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.3.5259latest

Version History

VersionDownloads (Last 7 Days)Published
3.3.5259
3.3.40
3.3.30
3.3.20
3.3.00
3.2.60
3.2.50
3.2.40
3.2.30
3.2.20
3.1.01
3.0.00
2.16.132
2.16.01
2.15.30
2.15.20
2.15.10
2.13.10
2.11.20
2.11.10
2.10.30
2.10.13
2.10.00
2.9.20
2.9.10
2.9.00
2.8.20
2.8.00
2.7.10
2.7.00
2.6.160
2.6.150
2.6.140
2.6.130
2.6.120
2.6.100
2.6.90
2.6.80
2.6.70
2.6.60
2.6.50
2.6.30
2.6.20
2.5.30
2.5.20
2.5.00
2.4.20
2.4.10
2.4.00
2.3.40
2.3.35
2.3.20
2.3.10
2.2.30
2.2.20
2.2.10
2.1.160
2.1.150
2.1.61
2.1.50
2.1.40
2.1.30
2.1.20
2.1.10
2.1.00
1.3.20
1.3.10
1.2.80
1.2.70
1.2.61
1.2.50
1.2.40
1.2.30
1.2.20
1.2.10
1.1.00
1.0.30
1.0.20
1.0.10
1.0.00
0.9.90
0.9.80
0.9.70
0.9.60
0.9.50

Package Sidebar

Install

npm i thespian

Weekly Downloads

303

Version

3.3.5

License

Apache 2.0

Unpacked Size

372 kB

Total Files

144

Last publish

Collaborators

  • rickmugridge