expect-matcher-node-mock 1.1.1 → 1.1.2
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 +202 -8
- package/lib/index.mjs +15 -0
- package/package.json +28 -2
- package/.eslintrc.js +0 -82
- package/.nvmrc +0 -1
package/README.md
CHANGED
|
@@ -1,18 +1,133 @@
|
|
|
1
|
-
# Extend
|
|
2
|
-
Extend for Jest [expect library](https://jestjs.io/docs/expect#expectextendmatchers).
|
|
1
|
+
# Extend Jest expect matchers for native Node.js test runner mock objects
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
[](https://badge.fury.io/js/expect-matcher-node-mock)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://nodejs.org/)
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
Extend the Jest [expect library](https://jestjs.io/docs/expect#expectextendmatchers) with matchers that work with native Node.js test runner mock objects.
|
|
8
|
+
|
|
9
|
+
The native Node.js test runner has a different mock structure from `jest.mock`, therefore extending the current methods is necessary to provide Jest-like testing experience.
|
|
10
|
+
|
|
11
|
+
## Why use this?
|
|
12
|
+
|
|
13
|
+
The native Node.js test runner (introduced in Node.js 18) provides built-in testing capabilities without external dependencies. However, its mock objects have a different structure than Jest mocks, making Jest's expect matchers incompatible.
|
|
14
|
+
|
|
15
|
+
This package bridges that gap by:
|
|
16
|
+
- ✅ Providing familiar Jest-like assertions for Node.js mocks
|
|
17
|
+
- ✅ Zero configuration setup
|
|
18
|
+
- ✅ Lightweight with minimal dependencies
|
|
19
|
+
- ✅ Full compatibility with existing Jest expect patterns
|
|
20
|
+
- ✅ Perfect for migrating from Jest to native Node.js testing
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install expect-matcher-node-mock
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
yarn add expect-matcher-node-mock
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pnpm add expect-matcher-node-mock
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
Since the native Node.js test runner doesn't have any global setup, you need to import this extension in every test file:
|
|
7
39
|
|
|
8
|
-
Since native node test runner doesn't have any global setup, use this extension by import package:
|
|
9
40
|
```js
|
|
10
41
|
import 'expect-matcher-node-mock';
|
|
11
42
|
```
|
|
12
|
-
The extend of expect matchers is included in this import.
|
|
13
43
|
|
|
44
|
+
The extension of expect matchers is included in this import.
|
|
45
|
+
|
|
46
|
+
### Alternative: Import expect directly
|
|
47
|
+
|
|
48
|
+
You can also import `expect` directly from this package, which automatically includes all the extended matchers:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import { expect } from 'expect-matcher-node-mock';
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### JavaScript Example
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
import { test, mock } from 'node:test';
|
|
58
|
+
import { expect } from 'expect-matcher-node-mock'; // Direct import with matchers included
|
|
59
|
+
|
|
60
|
+
test('mock function testing', () => {
|
|
61
|
+
const mockFn = mock.fn();
|
|
62
|
+
|
|
63
|
+
mockFn('arg1', 'arg2');
|
|
64
|
+
mockFn('arg3');
|
|
65
|
+
|
|
66
|
+
expect(mockFn).toHaveBeenCalled();
|
|
67
|
+
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
68
|
+
expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2');
|
|
69
|
+
expect(mockFn).toHaveBeenLastCalledWith('arg3');
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Alternative Import Method
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
import { test, mock } from 'node:test';
|
|
77
|
+
import { expect } from 'expect';
|
|
78
|
+
import 'expect-matcher-node-mock'; // Extension import
|
|
79
|
+
|
|
80
|
+
test('same functionality as above', () => {
|
|
81
|
+
const mockFn = mock.fn();
|
|
82
|
+
// ... same test code
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### TypeScript Example
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { test, mock } from 'node:test';
|
|
90
|
+
import { expect } from 'expect-matcher-node-mock'; // Direct import recommended
|
|
91
|
+
|
|
92
|
+
test('mock function with return values', () => {
|
|
93
|
+
const mockFn = mock.fn<(x: number) => number>();
|
|
94
|
+
|
|
95
|
+
mockFn.mock.mockImplementation((x: number) => x * 2);
|
|
96
|
+
|
|
97
|
+
const result1 = mockFn(5);
|
|
98
|
+
const result2 = mockFn(10);
|
|
99
|
+
|
|
100
|
+
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
101
|
+
expect(mockFn).toHaveReturnedWith(10);
|
|
102
|
+
expect(mockFn).toHaveLastReturnedWith(20);
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Advanced Example - Method Mocking
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
import { test, mock } from 'node:test';
|
|
110
|
+
import { expect } from 'expect-matcher-node-mock';
|
|
111
|
+
|
|
112
|
+
test('object method mocking', () => {
|
|
113
|
+
const obj = {
|
|
114
|
+
method: mock.fn(() => 'original'),
|
|
115
|
+
calculate: mock.fn((a, b) => a + b)
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
obj.method();
|
|
119
|
+
obj.calculate(2, 3);
|
|
120
|
+
obj.calculate(5, 7);
|
|
121
|
+
|
|
122
|
+
expect(obj.method).toHaveBeenCalled();
|
|
123
|
+
expect(obj.calculate).toHaveBeenCalledTimes(2);
|
|
124
|
+
expect(obj.calculate).toHaveBeenNthCalledWith(1, 2, 3);
|
|
125
|
+
expect(obj.calculate).toHaveBeenNthCalledWith(2, 5, 7);
|
|
126
|
+
expect(obj.calculate).toHaveNthReturnedWith(2, 12);
|
|
127
|
+
});
|
|
128
|
+
```
|
|
14
129
|
|
|
15
|
-
##
|
|
130
|
+
## Available Matchers
|
|
16
131
|
|
|
17
132
|
### toHaveBeenCalled
|
|
18
133
|
https://jestjs.io/docs/expect#tohavebeencalled
|
|
@@ -30,7 +145,7 @@ https://jestjs.io/docs/expect#tohavebeenlastcalledwitharg1-arg2-
|
|
|
30
145
|
https://jestjs.io/docs/expect#tohavebeennthcalledwithnthcall-arg1-arg2-
|
|
31
146
|
|
|
32
147
|
### toHaveReturned
|
|
33
|
-
|
|
148
|
+
*Alias: `toReturn`*
|
|
34
149
|
https://jestjs.io/docs/expect#tohavereturned
|
|
35
150
|
|
|
36
151
|
### toHaveReturnedTimes
|
|
@@ -44,3 +159,82 @@ https://jestjs.io/docs/expect#tohavelastreturnedwithvalue
|
|
|
44
159
|
|
|
45
160
|
### toHaveNthReturnedWith
|
|
46
161
|
https://jestjs.io/docs/expect#tohaventhreturnedwithnthcall-value
|
|
162
|
+
|
|
163
|
+
## Requirements
|
|
164
|
+
|
|
165
|
+
- **Node.js 18.0.0 or higher** (for native test runner support)
|
|
166
|
+
- **expect package** (peer dependency) - Install with `npm install expect`
|
|
167
|
+
|
|
168
|
+
## Peer Dependencies
|
|
169
|
+
|
|
170
|
+
This package requires the `expect` library to be installed in your project:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npm install expect
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Troubleshooting
|
|
177
|
+
|
|
178
|
+
### "expect is not defined" error
|
|
179
|
+
You have two options to import `expect`:
|
|
180
|
+
|
|
181
|
+
**Option 1: Direct import (recommended)**
|
|
182
|
+
```js
|
|
183
|
+
import { expect } from 'expect-matcher-node-mock';
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
**Option 2: Separate imports**
|
|
187
|
+
```js
|
|
188
|
+
import { expect } from 'expect';
|
|
189
|
+
import 'expect-matcher-node-mock';
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Matchers not working
|
|
193
|
+
Ensure you either:
|
|
194
|
+
- Import expect directly from this package: `import { expect } from 'expect-matcher-node-mock';`
|
|
195
|
+
- Or import the extension before using expect: `import 'expect-matcher-node-mock';`
|
|
196
|
+
|
|
197
|
+
### TypeScript support
|
|
198
|
+
While this package is written in JavaScript, it works seamlessly with TypeScript projects. For better type support, you may want to add type declarations for the extended matchers.
|
|
199
|
+
|
|
200
|
+
## Compatibility
|
|
201
|
+
|
|
202
|
+
- ✅ Node.js native test runner (18.0.0+)
|
|
203
|
+
- ✅ Works with ES modules (`.mjs`, `type: "module"`)
|
|
204
|
+
- ✅ TypeScript projects
|
|
205
|
+
- ✅ CommonJS projects (with appropriate imports)
|
|
206
|
+
|
|
207
|
+
## License
|
|
208
|
+
|
|
209
|
+
MIT - see [LICENSE](LICENSE) file for details.
|
|
210
|
+
|
|
211
|
+
## Contributing
|
|
212
|
+
|
|
213
|
+
Issues and pull requests are welcome on [GitHub](https://github.com/crysadrak/expect-matcher-node-mock).
|
|
214
|
+
|
|
215
|
+
### Development
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
# Clone the repository
|
|
219
|
+
git clone https://github.com/crysadrak/expect-matcher-node-mock.git
|
|
220
|
+
cd expect-matcher-node-mock
|
|
221
|
+
|
|
222
|
+
# Install dependencies
|
|
223
|
+
npm install
|
|
224
|
+
|
|
225
|
+
# Run tests
|
|
226
|
+
npm test
|
|
227
|
+
|
|
228
|
+
# Run linting
|
|
229
|
+
npm run lint
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Changelog
|
|
233
|
+
|
|
234
|
+
See [GitHub Releases](https://github.com/crysadrak/expect-matcher-node-mock/releases) for version history.
|
|
235
|
+
|
|
236
|
+
## Related Projects
|
|
237
|
+
|
|
238
|
+
- [expect](https://github.com/jestjs/jest/tree/main/packages/expect) - The core expect library
|
|
239
|
+
- [Node.js Test Runner](https://nodejs.org/api/test.html) - Native Node.js testing
|
|
240
|
+
- [Jest](https://jestjs.io/) - JavaScript testing framework
|
package/lib/index.mjs
CHANGED
|
@@ -27,3 +27,18 @@ expect.extend({
|
|
|
27
27
|
toHaveLastReturnedWith,
|
|
28
28
|
toHaveNthReturnedWith,
|
|
29
29
|
});
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
expect,
|
|
33
|
+
toHaveBeenCalled,
|
|
34
|
+
toHaveBeenCalledTimes,
|
|
35
|
+
toHaveBeenCalledWith,
|
|
36
|
+
toHaveBeenLastCalledWith,
|
|
37
|
+
toHaveBeenNthCalledWith,
|
|
38
|
+
toReturn,
|
|
39
|
+
toHaveReturned,
|
|
40
|
+
toHaveReturnedTimes,
|
|
41
|
+
toHaveReturnedWith,
|
|
42
|
+
toHaveLastReturnedWith,
|
|
43
|
+
toHaveNthReturnedWith,
|
|
44
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expect-matcher-node-mock",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "Jest
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"description": "Jest-like expect matchers for native Node.js test runner mock objects. Provides toHaveBeenCalled, toHaveBeenCalledWith, toHaveReturned and more.",
|
|
5
5
|
"main": "lib/index.mjs",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"homepage": "https://github.com/crysadrak/expect-matcher-node-mock#readme",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=18.0.0"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"lib/",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"keywords": [
|
|
17
|
+
"jest",
|
|
18
|
+
"expect",
|
|
19
|
+
"matcher",
|
|
20
|
+
"mock",
|
|
21
|
+
"node",
|
|
22
|
+
"test",
|
|
23
|
+
"testing",
|
|
24
|
+
"node-test",
|
|
25
|
+
"nodejs",
|
|
26
|
+
"test-runner",
|
|
27
|
+
"unit-test",
|
|
28
|
+
"assertion",
|
|
29
|
+
"spy",
|
|
30
|
+
"stub"
|
|
31
|
+
],
|
|
6
32
|
"bugs": {
|
|
7
33
|
"url": "https://github.com/crysadrak/expect-matcher-node-mock/issues"
|
|
8
34
|
},
|
package/.eslintrc.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
root: true,
|
|
3
|
-
ignorePatterns: [
|
|
4
|
-
'**/node_modules/**',
|
|
5
|
-
'**/dist/**',
|
|
6
|
-
'**/build/**',
|
|
7
|
-
'**/docs/**',
|
|
8
|
-
'**/.docusaurus/**',
|
|
9
|
-
'**/coverage/**',
|
|
10
|
-
],
|
|
11
|
-
extends: [
|
|
12
|
-
'eslint:recommended',
|
|
13
|
-
'plugin:prettier/recommended',
|
|
14
|
-
'plugin:import/recommended',
|
|
15
|
-
],
|
|
16
|
-
rules: {
|
|
17
|
-
'no-console': [
|
|
18
|
-
'error',
|
|
19
|
-
{
|
|
20
|
-
allow: ['warn', 'error'],
|
|
21
|
-
},
|
|
22
|
-
],
|
|
23
|
-
'no-unused-vars': [
|
|
24
|
-
'error',
|
|
25
|
-
{
|
|
26
|
-
argsIgnorePattern: '^_',
|
|
27
|
-
varsIgnorePattern: '^_',
|
|
28
|
-
destructuredArrayIgnorePattern: '^_',
|
|
29
|
-
ignoreRestSiblings: true,
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
|
|
33
|
-
// Prettier
|
|
34
|
-
'prettier/prettier': [
|
|
35
|
-
'error',
|
|
36
|
-
{
|
|
37
|
-
singleQuote: true,
|
|
38
|
-
semi: true,
|
|
39
|
-
trailingComma: 'es5',
|
|
40
|
-
jsxSingleQuote: true,
|
|
41
|
-
bracketSameLine: false,
|
|
42
|
-
arrowParens: 'avoid',
|
|
43
|
-
},
|
|
44
|
-
],
|
|
45
|
-
|
|
46
|
-
// Import plugin
|
|
47
|
-
'import/no-unresolved': [
|
|
48
|
-
'warn',
|
|
49
|
-
{
|
|
50
|
-
ignore: [
|
|
51
|
-
'^@\\/', // ignore @/* aliases
|
|
52
|
-
'@(docusaurus|theme)',
|
|
53
|
-
],
|
|
54
|
-
},
|
|
55
|
-
],
|
|
56
|
-
'import/order': [
|
|
57
|
-
'error',
|
|
58
|
-
{
|
|
59
|
-
groups: ['builtin', 'external', 'internal'],
|
|
60
|
-
'newlines-between': 'always',
|
|
61
|
-
alphabetize: {
|
|
62
|
-
order: 'asc',
|
|
63
|
-
caseInsensitive: true,
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
],
|
|
67
|
-
},
|
|
68
|
-
settings: {
|
|
69
|
-
'import/resolver': {
|
|
70
|
-
node: {
|
|
71
|
-
extensions: ['.js', '.jsx', '.mjs', '.json'],
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
parserOptions: {
|
|
76
|
-
sourceType: 'module',
|
|
77
|
-
requireConfigFile: false,
|
|
78
|
-
},
|
|
79
|
-
env: {
|
|
80
|
-
node: true,
|
|
81
|
-
},
|
|
82
|
-
};
|
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
22
|