prostub 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +115 -2
  2. package/index.d.ts +465 -94
  3. package/index.js +1 -582
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -2,7 +2,107 @@
2
2
 
3
3
  ## Description
4
4
 
5
- A mocking/stubbing library for JavaScript, inspired by Mockito. It is evironment-agnostic, `@types/node` is included as a dev-dependency to provide `console` during testing. The production code at run-time only relies on the JavaScript standard library
5
+ A mocking/stubbing library for JavaScript, inspired by Mockito. It is evironment-agnostic. The production code at run-time only relies on the JavaScript standard library and tslib. The test code relies on Node.js for running the tests, but the library itself can be used in any JavaScript environment (Node.js, browsers, Deno, etc.).
6
+
7
+ ## Features
8
+ - Create mock and spy objects for classes and interfaces
9
+ - Stub methods and properties with various behaviors
10
+ - Verify interactions with mock objects
11
+ - Type-safe API with TypeScript support
12
+ - Lightweight and easy to use
13
+ - Low bundle size (~2.2KB minified + gzipped)
14
+ - Small API surface (only 15 API symbols)
15
+
16
+ It consists of 3 separate DSLs:
17
+
18
+ ### The Mocking DSL
19
+
20
+ The `mock()` and `spy()` functions create mock objects that can be used in place of real objects during testing. Mocks record interactions and can be verified later. By default, all interactions throw errors unless stubbed.
21
+
22
+ They are created as follows:
23
+
24
+ ```typescript
25
+ import { mock, spy } from "prostub";
26
+
27
+ class Example {
28
+ }
29
+
30
+ const mockedExample = mock(Example);
31
+
32
+ const realExample = new Example();
33
+ const spiedExample = spy(realExample);
34
+ ```
35
+
36
+ ### The Stubbing DSL
37
+
38
+ The `stub()` function allows you to define custom behavior for methods and properties on mock objects. You can specify return values, thrown errors, or custom implementations for methods, as well as get/set behavior for properties.
39
+
40
+ A stub always follows the logic `stub(<mock>).<method/property> = <behavior>`, for example:
41
+
42
+ ```typescript
43
+ import { fixedValue, mock, noop, stub } from "prostub";
44
+
45
+ class Example {
46
+ method(): number {
47
+ this.property = "changed";
48
+ }
49
+ property: string = "initial";
50
+ }
51
+
52
+ const mockedExample = mock(Example);
53
+
54
+ // Stubbing a method to return a fixed value
55
+ stub(mockedExample).method = noop();
56
+ stub(mockedExample).property = fixedValue("stubbed value");
57
+
58
+ // mockedExample.method() now no longer changes the property
59
+ // instead, it does nothing and returns undefined
60
+ // mockedExample.property now returns "stubbed value"
61
+ ```
62
+
63
+ Note: The `stub()` function only works with mock and spy objects created by `mock()` and `spy()`. Attempting to use it on real objects will result in a runtime error.
64
+
65
+ Note 2: stubbed behavior only affects interactions with the mock/spy object itself. This is especially important for spies, as calls to methods on the real object from within other methods will not be affected by stubbing.
66
+
67
+ #### Function behaviors
68
+
69
+ - `answer(fn: (this: OriginalObjectType, ...args: OriginalFunctionArgs) => ReturnType)`: Defines a custom implementation for a method.
70
+ - `callThrough()`: Calls the original method implementation on a spy. Throws an error if used on a pure mock.
71
+ - `delegateTo(fn: (this: OriginalObjectType, ...args: OriginalFunctionArgs) => ReturnType)`: Delegates the method call to the provided function.
72
+ - `noop()`: A no-operation function that does nothing and returns `undefined`.
73
+ - `returnFixed(value: any)`: Returns a fixed value when the method is called.
74
+ - `returnSerial(...values: any[])`: Returns a series of values on successive calls.
75
+ - `throwOnCall(errorFactory: () => Error)`: Throws the specified error when the method is called.
76
+ - `when(condition: (args: OriginalFunctionArgs) => boolean)`: Specifies a condition for when the stubbed behavior should apply.
77
+
78
+ #### Property behaviors
79
+
80
+ - `fixedValue(value: any)`: Always returns the specified fixed value when the property is accessed.
81
+ - `defaultValue(value: any)`: Returns the specified default value when the property is accessed, but allows it to be changed.
82
+ - `trackValue()`: Tracks the value of the property, allowing it to be read and written like a normal property.
83
+
84
+ ### The Verification DSL
85
+
86
+ The `verify()` function allows you to assert that certain interactions occurred on mock objects. You can verify method calls, property accesses, and the number of times they were called.
87
+
88
+ For example:
89
+
90
+ ```typescript
91
+ import { mock, verify, returnFixed } from "prostub";
92
+ class Example {
93
+ method(): string {
94
+ return this.property;
95
+ }
96
+ property: string = "initial";
97
+ }
98
+
99
+ const mockedExample = mock(Example);
100
+
101
+ stub(mockedExample).method = returnFixed("stubbed result");
102
+
103
+ verify(mockedExample).method.firstInvocation().returned(it => it === "stubbed result"); // Passes
104
+ verify(mockedExample).property.wasNotRead(); // Passes
105
+ ```
6
106
 
7
107
  ## Building
8
108
 
@@ -10,5 +110,18 @@ The library is automatically built when running `pnpm install`, but it can be re
10
110
 
11
111
  ## Testing
12
112
 
13
- Right now there are no unit tests. However, there is a `test.ts`, which examplifies how the library is meant to be used. It can be executed by running `npm test`.
113
+ The library is fully tested using Node.js. Tests are executed as part of the building process, run inside the prepare hook, so when install the dependencies:
114
+
115
+ ```bash
116
+ npm install
117
+ ```
118
+
119
+ The tests will be run automatically. To run the tests manually, simply run:
120
+
121
+ ```bash
122
+ npm test
123
+ ```
124
+
125
+ ## License
14
126
 
127
+ This project is licensed under the MIT License. See the [LICENSE](./LICENSE.txt) file for details.