3.0.0 • Published 1 month ago

thespian v3.0.0

Weekly downloads
455
License
Apache 2.0
Repository
github
Last release
1 month ago

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:

3.0.0

1 month ago

2.16.1

3 months ago

2.16.0

3 months ago

2.15.3

3 months ago

2.15.2

3 months ago

2.15.1

3 months ago

2.13.1

5 months ago

2.11.2

9 months ago

2.11.1

1 year ago

2.10.3

1 year ago

2.10.1

2 years ago

2.9.2

2 years ago

2.9.1

3 years ago

2.10.0

2 years ago

2.9.0

3 years ago

2.8.2

3 years ago

2.8.0

3 years ago

2.7.0

3 years ago

2.7.1

3 years ago

2.6.15

3 years ago

2.6.16

3 years ago

2.6.14

3 years ago

2.6.12

3 years ago

2.6.13

3 years ago

2.6.10

3 years ago

2.6.9

3 years ago

2.6.7

3 years ago

2.6.8

3 years ago

2.6.6

3 years ago

2.6.5

3 years ago

2.6.3

3 years ago

2.6.2

3 years ago

2.5.3

3 years ago

2.5.0

3 years ago

2.5.2

3 years ago

2.4.2

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.4

3 years ago

2.3.3

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.1.16

4 years ago

2.1.15

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.9.9

4 years ago

0.9.8

5 years ago

0.9.7

5 years ago

0.9.6

5 years ago

0.9.5

5 years ago