vitest-auto-spy 1.0.0 → 1.1.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.
- package/README.md +86 -21
- package/dist/index.cjs +252 -221
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +68 -149
- package/dist/index.d.ts +68 -149
- package/dist/index.js +252 -221
- package/dist/index.js.map +1 -1
- package/package.json +33 -2
package/README.md
CHANGED
|
@@ -1,10 +1,50 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# vitest-auto-spy
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
**Automatic, fully-typed test spies from a class — powered by Vitest's `vi.fn()`.**
|
|
6
|
+
|
|
7
|
+
A drop-in replacement for [`jest-auto-spies`](https://www.npmjs.com/package/jest-auto-spies):
|
|
8
|
+
same API, but spying only on **Vitest** instead of Jest.
|
|
9
|
+
|
|
10
|
+
[](https://www.npmjs.com/package/vitest-auto-spy)
|
|
11
|
+
[](https://www.npmjs.com/package/vitest-auto-spy)
|
|
12
|
+
[](https://github.com/ASDAlexey/vitest-auto-spy/actions/workflows/ci.yml)
|
|
13
|
+
[](https://bundlephobia.com/package/vitest-auto-spy)
|
|
14
|
+
[](https://www.npmjs.com/package/vitest-auto-spy)
|
|
15
|
+
[](https://github.com/ASDAlexey/vitest-auto-spy/actions/workflows/ci.yml)
|
|
16
|
+
[](./LICENSE)
|
|
4
17
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
📦 [**npm**](https://www.npmjs.com/package/vitest-auto-spy) · 🐙 [**GitHub**](https://github.com/ASDAlexey/vitest-auto-spy) · 🔖 [**Changelog**](./CHANGELOG.md)
|
|
19
|
+
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
- 🧪 Reads a class and generates a typed `vi.fn()` spy for **every** method
|
|
25
|
+
- 🎯 Return-type-aware helpers — sync, `Promise`, and `Observable` all get the right API
|
|
26
|
+
- 🔀 `calledWith` / `mustBeCalledWith` argument dispatch
|
|
27
|
+
- 📡 First-class RxJS `Observable` spying (`nextWith`, `nextWithValues`, `throwWith`, …)
|
|
28
|
+
- ⚙️ Getter / setter spies via `accessorSpies`
|
|
29
|
+
- 🅰️ Angular helpers (`provideAutoSpy`, `injectSpy`) — works with **both zoneless and zone.js**
|
|
30
|
+
- 🟢 100% test coverage, zero runtime dependencies beyond a tiny arg serializer
|
|
31
|
+
|
|
32
|
+
## Table of contents
|
|
33
|
+
|
|
34
|
+
- [Install](#install)
|
|
35
|
+
- [Why](#why)
|
|
36
|
+
- [Migrating from jest-auto-spies](#migrating-from-jest-auto-spies)
|
|
37
|
+
- [Configuration](#configuration)
|
|
38
|
+
- [Synchronous methods](#synchronous-methods)
|
|
39
|
+
- [Promise-returning methods](#promise-returning-methods)
|
|
40
|
+
- [Observable methods & properties](#observable-returning-methods--observable-properties)
|
|
41
|
+
- [Getters & setters](#getters--setters)
|
|
42
|
+
- [Angular helpers](#angular-helpers)
|
|
43
|
+
- [API reference](#api-reference)
|
|
44
|
+
- [Contributing](#contributing)
|
|
45
|
+
- [License](#license)
|
|
46
|
+
|
|
47
|
+
## Install
|
|
8
48
|
|
|
9
49
|
```bash
|
|
10
50
|
npm i -D vitest-auto-spy
|
|
@@ -13,8 +53,6 @@ npm i -D vitest-auto-spy
|
|
|
13
53
|
Peer dependencies (provided by your project): `vitest`, `rxjs`, and — for the Angular
|
|
14
54
|
helpers — `@angular/core`.
|
|
15
55
|
|
|
16
|
-
---
|
|
17
|
-
|
|
18
56
|
## Why
|
|
19
57
|
|
|
20
58
|
Manually mocking a service is tedious and brittle:
|
|
@@ -42,7 +80,29 @@ beforeEach(() => {
|
|
|
42
80
|
`Spy<UserService>` exposes each method as a `vi.fn()` **plus** the right helpers based on
|
|
43
81
|
the method's return type (sync / `Promise` / `Observable`).
|
|
44
82
|
|
|
45
|
-
|
|
83
|
+
## Migrating from jest-auto-spies
|
|
84
|
+
|
|
85
|
+
The public API is intentionally identical. In most projects the migration is a
|
|
86
|
+
**find-and-replace of the import**:
|
|
87
|
+
|
|
88
|
+
```diff
|
|
89
|
+
- import { createSpyFromClass, provideAutoSpy } from 'jest-auto-spies';
|
|
90
|
+
+ import { createSpyFromClass, provideAutoSpy } from 'vitest-auto-spy';
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
| jest-auto-spies | vitest-auto-spy | Status |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| `createSpyFromClass` | `createSpyFromClass` | ✅ identical |
|
|
96
|
+
| `provideAutoSpy` | `provideAutoSpy` | ✅ identical |
|
|
97
|
+
| `calledWith` / `mustBeCalledWith` | same | ✅ identical |
|
|
98
|
+
| `resolveWith` / `rejectWith` / `resolveWithPerCall` | same | ✅ identical |
|
|
99
|
+
| `nextWith` / `nextOneTimeWith` / `nextWithValues` / `nextWithPerCall` | same | ✅ identical |
|
|
100
|
+
| `throwWith` / `complete` / `returnSubject` | same | ✅ identical |
|
|
101
|
+
| `accessorSpies.getters/setters` | same | ✅ identical |
|
|
102
|
+
| `createObservableWithValues` | same | ✅ identical |
|
|
103
|
+
| underlying mock | `jest.fn()` → `vi.fn()` | 🔁 swapped |
|
|
104
|
+
|
|
105
|
+
Just make sure your tests run under Vitest, and (for Angular) that `TestBed` is set up.
|
|
46
106
|
|
|
47
107
|
## Configuration
|
|
48
108
|
|
|
@@ -62,8 +122,6 @@ createSpyFromClass(MyService, {
|
|
|
62
122
|
});
|
|
63
123
|
```
|
|
64
124
|
|
|
65
|
-
---
|
|
66
|
-
|
|
67
125
|
## Synchronous methods
|
|
68
126
|
|
|
69
127
|
```ts
|
|
@@ -80,8 +138,6 @@ myService.getName.mustBeCalledWith(1).mockReturnValue('Fake Name');
|
|
|
80
138
|
expect(() => myService.getName(2)).toThrow();
|
|
81
139
|
```
|
|
82
140
|
|
|
83
|
-
---
|
|
84
|
-
|
|
85
141
|
## Promise-returning methods
|
|
86
142
|
|
|
87
143
|
```ts
|
|
@@ -96,8 +152,6 @@ myService.getProducts.resolveWithPerCall([{ value: ['a'] }, { value: ['b'] }]);
|
|
|
96
152
|
myService.getProducts.calledWith(1).resolveWith(['one']);
|
|
97
153
|
```
|
|
98
154
|
|
|
99
|
-
---
|
|
100
|
-
|
|
101
155
|
## Observable-returning methods & Observable properties
|
|
102
156
|
|
|
103
157
|
Both spied **methods** that return an `Observable` and spied **properties** of type
|
|
@@ -141,8 +195,6 @@ const fake$ = createObservableWithValues([{ value: 1 }, { value: 2 }, { complete
|
|
|
141
195
|
const { values$, subject } = createObservableWithValues([{ value: 1 }], { returnSubject: true });
|
|
142
196
|
```
|
|
143
197
|
|
|
144
|
-
---
|
|
145
|
-
|
|
146
198
|
## Getters & setters
|
|
147
199
|
|
|
148
200
|
```ts
|
|
@@ -160,8 +212,6 @@ spy.userName = 'New Name';
|
|
|
160
212
|
expect(spy.accessorSpies.setters.userName).toHaveBeenCalledWith('New Name');
|
|
161
213
|
```
|
|
162
214
|
|
|
163
|
-
---
|
|
164
|
-
|
|
165
215
|
## Angular helpers
|
|
166
216
|
|
|
167
217
|
`provideAutoSpy` is the shorthand for providing an auto-spy in a `TestBed`:
|
|
@@ -200,8 +250,6 @@ mockReadonlyPropGetter(service, 'label', () => 'A'); // dynamic getter
|
|
|
200
250
|
mockAccessorsProp(service, 'theme'); // spied get + set
|
|
201
251
|
```
|
|
202
252
|
|
|
203
|
-
---
|
|
204
|
-
|
|
205
253
|
## API reference
|
|
206
254
|
|
|
207
255
|
| Export | Description |
|
|
@@ -226,8 +274,25 @@ mockAccessorsProp(service, 'theme'); // spied get + set
|
|
|
226
274
|
|
|
227
275
|
`ValueConfig` (for `nextWithValues`): `{ value, delay? }` | `{ errorValue, delay? }` | `{ complete?, delay? }`.
|
|
228
276
|
|
|
229
|
-
|
|
277
|
+
## Contributing
|
|
278
|
+
|
|
279
|
+
Contributions are welcome! Please read [CONTRIBUTING.md](./CONTRIBUTING.md) and the
|
|
280
|
+
[Code of Conduct](./CODE_OF_CONDUCT.md). In short:
|
|
281
|
+
|
|
282
|
+
```bash
|
|
283
|
+
npm ci
|
|
284
|
+
npm test # run the suite
|
|
285
|
+
npm run test:coverage # 100% thresholds enforced
|
|
286
|
+
npm run build
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Releases are automated: merging a PR into `master` bumps the version from the
|
|
290
|
+
Conventional Commit types and publishes to npm — see
|
|
291
|
+
[CONTRIBUTING.md → Releases](./CONTRIBUTING.md#releases).
|
|
292
|
+
|
|
293
|
+
If this package saved you time, a ⭐ on [GitHub](https://github.com/ASDAlexey/vitest-auto-spy)
|
|
294
|
+
helps others find it.
|
|
230
295
|
|
|
231
296
|
## License
|
|
232
297
|
|
|
233
|
-
MIT © Alexey Popov
|
|
298
|
+
[MIT](./LICENSE) © [Alexey Popov](https://github.com/ASDAlexey)
|