expect-matcher-node-mock 1.0.0 → 1.1.1

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 CHANGED
@@ -1,8 +1,16 @@
1
1
  # Extend jest-expect matcher by native node test runner mock matchers
2
2
  Extend for Jest [expect library](https://jestjs.io/docs/expect#expectextendmatchers).
3
3
 
4
+ Native node test runner does have diffrent mock structure from jest.mock therefore the extension of current methods is necessary.yst
5
+
6
+ ## Usage:
7
+
8
+ Since native node test runner doesn't have any global setup, use this extension by import package:
9
+ ```js
10
+ import 'expect-matcher-node-mock';
11
+ ```
12
+ The extend of expect matchers is included in this import.
4
13
 
5
- Native node test runner does have diffrent mock structure from jest.mock therefore the extension of current methods is necessary.
6
14
 
7
15
  ## Content:
8
16
 
@@ -21,6 +29,18 @@ https://jestjs.io/docs/expect#tohavebeenlastcalledwitharg1-arg2-
21
29
  ### toHaveBeenNthCalledWith
22
30
  https://jestjs.io/docs/expect#tohavebeennthcalledwithnthcall-arg1-arg2-
23
31
 
24
- ### TODO
25
- add all toHaveReturnedXX methods
32
+ ### toHaveReturned
33
+ alias `toReturn`
34
+ https://jestjs.io/docs/expect#tohavereturned
35
+
36
+ ### toHaveReturnedTimes
37
+ https://jestjs.io/docs/expect#tohavereturnedtimesnumber
38
+
39
+ ### toHaveReturnedWith
40
+ https://jestjs.io/docs/expect#tohavereturnedwithvalue
41
+
42
+ ### toHaveLastReturnedWith
43
+ https://jestjs.io/docs/expect#tohavelastreturnedwithvalue
26
44
 
45
+ ### toHaveNthReturnedWith
46
+ https://jestjs.io/docs/expect#tohaventhreturnedwithnthcall-value
@@ -1,11 +1,28 @@
1
1
  import { describe, it, mock } from 'node:test';
2
2
 
3
3
  import { expect } from 'expect';
4
+ import stripAnsi from 'strip-ansi';
4
5
 
5
6
  import '../index.mjs';
6
7
 
7
8
  describe('mockMethodMatchers', () => {
8
- it('toHaveBeenCalled', () => {
9
+ it('toHaveBeenCalled - should fail to test when method is not node mock', () => {
10
+ const method = () => {};
11
+
12
+ method();
13
+
14
+ try {
15
+ expect(method).toHaveBeenCalled();
16
+ } catch (error) {
17
+ expect(stripAnsi(error.message)).toEqual(
18
+ expect.stringContaining(
19
+ 'Matcher error: received value must be a node mock function'
20
+ )
21
+ );
22
+ }
23
+ });
24
+
25
+ it('toHaveBeenCalled - should test pass and not pass', () => {
9
26
  const method = mock.fn();
10
27
  const notCalledMethod = mock.fn();
11
28
 
@@ -15,7 +32,23 @@ describe('mockMethodMatchers', () => {
15
32
  expect(notCalledMethod).not.toHaveBeenCalled();
16
33
  });
17
34
 
18
- it('toHaveBeenCalledTimes', () => {
35
+ it('toHaveBeenCalledTimes - should fail to test when method is not node mock', () => {
36
+ const method = () => {};
37
+
38
+ method();
39
+
40
+ try {
41
+ expect(method).toHaveBeenCalledTimes();
42
+ } catch (error) {
43
+ expect(stripAnsi(error.message)).toEqual(
44
+ expect.stringContaining(
45
+ 'Matcher error: received value must be a node mock function'
46
+ )
47
+ );
48
+ }
49
+ });
50
+
51
+ it('toHaveBeenCalledTimes - should test pass and not pass', () => {
19
52
  const method = mock.fn();
20
53
  const notCalledMethod = mock.fn();
21
54
 
@@ -26,7 +59,23 @@ describe('mockMethodMatchers', () => {
26
59
  expect(notCalledMethod).not.toHaveBeenCalledTimes(1);
27
60
  });
28
61
 
29
- it('toHaveBeenCalledWith', () => {
62
+ it('toHaveBeenCalledWith - should fail to test when method is not node mock', () => {
63
+ const method = () => {};
64
+
65
+ method();
66
+
67
+ try {
68
+ expect(method).toHaveBeenCalledWith();
69
+ } catch (error) {
70
+ expect(stripAnsi(error.message)).toEqual(
71
+ expect.stringContaining(
72
+ 'Matcher error: received value must be a node mock function'
73
+ )
74
+ );
75
+ }
76
+ });
77
+
78
+ it('toHaveBeenCalledWith - should test pass and not pass', () => {
30
79
  const method = mock.fn();
31
80
 
32
81
  method('foo', 'bar');
@@ -35,7 +84,23 @@ describe('mockMethodMatchers', () => {
35
84
  expect(method).not.toHaveBeenCalledWith('bar', 'foo');
36
85
  });
37
86
 
38
- it('toHaveBeenLastCalledWith', () => {
87
+ it('toHaveBeenLastCalledWith - should fail to test when method is not node mock', () => {
88
+ const method = () => {};
89
+
90
+ method();
91
+
92
+ try {
93
+ expect(method).toHaveBeenLastCalledWith();
94
+ } catch (error) {
95
+ expect(stripAnsi(error.message)).toEqual(
96
+ expect.stringContaining(
97
+ 'Matcher error: received value must be a node mock function'
98
+ )
99
+ );
100
+ }
101
+ });
102
+
103
+ it('toHaveBeenLastCalledWith - should test pass and not pass', () => {
39
104
  const method = mock.fn();
40
105
 
41
106
  method('foo');
@@ -45,7 +110,23 @@ describe('mockMethodMatchers', () => {
45
110
  expect(method).not.toHaveBeenLastCalledWith('foo');
46
111
  });
47
112
 
48
- it('toHaveBeenNthCalledWith', () => {
113
+ it('toHaveBeenNthCalledWith - should fail to test when method is not node mock', () => {
114
+ const method = () => {};
115
+
116
+ method();
117
+
118
+ try {
119
+ expect(method).toHaveBeenNthCalledWith();
120
+ } catch (error) {
121
+ expect(stripAnsi(error.message)).toEqual(
122
+ expect.stringContaining(
123
+ 'Matcher error: received value must be a node mock function'
124
+ )
125
+ );
126
+ }
127
+ });
128
+
129
+ it('toHaveBeenNthCalledWith - should test pass and not pass', () => {
49
130
  const method = mock.fn();
50
131
 
51
132
  method('foo');
@@ -55,4 +136,195 @@ describe('mockMethodMatchers', () => {
55
136
  expect(method).toHaveBeenNthCalledWith(2, 'bar');
56
137
  expect(method).not.toHaveBeenNthCalledWith(1, 'bar');
57
138
  });
139
+
140
+ it('toHaveReturned - should fail to test when method is not node mock', () => {
141
+ const method = () => {};
142
+
143
+ method();
144
+
145
+ try {
146
+ expect(method).toHaveReturned();
147
+ } catch (error) {
148
+ expect(stripAnsi(error.message)).toEqual(
149
+ expect.stringContaining(
150
+ 'Matcher error: received value must be a node mock function'
151
+ )
152
+ );
153
+ }
154
+ });
155
+
156
+ it('toHaveReturned - should test pass and not pass', () => {
157
+ const method = mock.fn();
158
+ const notCalledMethod = mock.fn();
159
+
160
+ method();
161
+
162
+ expect(method).toHaveReturned();
163
+ expect(notCalledMethod).not.toHaveReturned();
164
+ });
165
+
166
+ it('toHaveReturned - should not pass when mock method thorws an error', () => {
167
+ const method = mock.fn(() => {
168
+ throw new Error();
169
+ });
170
+
171
+ expect(() => {
172
+ method();
173
+ }).toThrow();
174
+
175
+ expect(method).not.toHaveReturned();
176
+ });
177
+
178
+ it('toHaveReturnedTimes - should fail to test when method is not node mock', () => {
179
+ const method = () => {};
180
+
181
+ method();
182
+
183
+ try {
184
+ expect(method).toHaveReturnedTimes();
185
+ } catch (error) {
186
+ expect(stripAnsi(error.message)).toEqual(
187
+ expect.stringContaining(
188
+ 'Matcher error: received value must be a node mock function'
189
+ )
190
+ );
191
+ }
192
+ });
193
+
194
+ it('toHaveReturnedTimes - should test pass and not pass', () => {
195
+ const method = mock.fn();
196
+ const notCalledMethod = mock.fn();
197
+
198
+ method();
199
+ method();
200
+
201
+ expect(method).toHaveReturnedTimes(2);
202
+ expect(notCalledMethod).not.toHaveReturnedTimes(1);
203
+ });
204
+
205
+ it('toHaveReturnedTimes - should not pass when mock method thorws an error', () => {
206
+ const method = mock.fn(() => {
207
+ throw new Error();
208
+ });
209
+
210
+ expect(() => {
211
+ method();
212
+ }).toThrow();
213
+
214
+ expect(method).not.toHaveReturnedTimes(1);
215
+ });
216
+
217
+ it('toHaveReturnedWith - should fail to test when method is not node mock', () => {
218
+ const method = () => {};
219
+
220
+ method();
221
+
222
+ try {
223
+ expect(method).toHaveReturnedWith();
224
+ } catch (error) {
225
+ expect(stripAnsi(error.message)).toEqual(
226
+ expect.stringContaining(
227
+ 'Matcher error: received value must be a node mock function'
228
+ )
229
+ );
230
+ }
231
+ });
232
+
233
+ it('toHaveReturnedWith - should test pass and not pass', () => {
234
+ const method = mock.fn(arg => arg);
235
+
236
+ method('foo', 'bar');
237
+
238
+ expect(method).toHaveReturnedWith('foo', 'bar');
239
+ expect(method).not.toHaveReturnedWith('bar', 'foo');
240
+ });
241
+
242
+ it('toHaveReturnedWith - should not pass when mock method thorws an error', () => {
243
+ const method = mock.fn(() => {
244
+ throw new Error();
245
+ });
246
+
247
+ expect(() => {
248
+ method();
249
+ }).toThrow();
250
+
251
+ expect(method).not.toHaveReturnedWith('foo');
252
+ });
253
+
254
+ it('toHaveLastReturnedWith - should fail to test when method is not node mock', () => {
255
+ const method = () => {};
256
+
257
+ method();
258
+
259
+ try {
260
+ expect(method).toHaveLastReturnedWith();
261
+ } catch (error) {
262
+ expect(stripAnsi(error.message)).toEqual(
263
+ expect.stringContaining(
264
+ 'Matcher error: received value must be a node mock function'
265
+ )
266
+ );
267
+ }
268
+ });
269
+
270
+ it('toHaveLastReturnedWith - should test pass and not pass', () => {
271
+ const method = mock.fn(arg => arg);
272
+
273
+ method('foo');
274
+ method('bar');
275
+
276
+ expect(method).toHaveLastReturnedWith('bar');
277
+ expect(method).not.toHaveLastReturnedWith('foo');
278
+ });
279
+
280
+ it('toHaveLastReturnedWith - should not pass when mock method thorws an error', () => {
281
+ const method = mock.fn(() => {
282
+ throw new Error();
283
+ });
284
+
285
+ expect(() => {
286
+ method();
287
+ }).toThrow();
288
+
289
+ expect(method).not.toHaveLastReturnedWith('foo');
290
+ });
291
+
292
+ it('toHaveNthReturnedWith - should fail to test when method is not node mock', () => {
293
+ const method = () => {};
294
+
295
+ method();
296
+
297
+ try {
298
+ expect(method).toHaveNthReturnedWith();
299
+ } catch (error) {
300
+ expect(stripAnsi(error.message)).toEqual(
301
+ expect.stringContaining(
302
+ 'Matcher error: received value must be a node mock function'
303
+ )
304
+ );
305
+ }
306
+ });
307
+
308
+ it('toHaveNthReturnedWith - should test pass and not pass', () => {
309
+ const method = mock.fn(arg => arg);
310
+
311
+ method('foo');
312
+ method('bar');
313
+
314
+ expect(method).toHaveNthReturnedWith(1, 'foo');
315
+ expect(method).toHaveNthReturnedWith(2, 'bar');
316
+ expect(method).not.toHaveNthReturnedWith(1, 'bar');
317
+ });
318
+
319
+ it('toHaveNthReturnedWith - should not pass when mock method thorws an error', () => {
320
+ const method = mock.fn(() => {
321
+ throw new Error();
322
+ });
323
+
324
+ expect(() => {
325
+ method();
326
+ }).toThrow();
327
+
328
+ expect(method).not.toHaveNthReturnedWith(1, 'foo');
329
+ });
58
330
  });
@@ -0,0 +1,265 @@
1
+ import { describe, it, mock } from 'node:test';
2
+
3
+ import { expect } from 'expect';
4
+ import stripAnsi from 'strip-ansi';
5
+
6
+ import {
7
+ toHaveBeenCalled,
8
+ toHaveBeenCalledTimes,
9
+ toHaveBeenCalledWith,
10
+ toHaveBeenLastCalledWith,
11
+ toHaveBeenNthCalledWith,
12
+ } from '../mockMethodMatchers.mjs';
13
+
14
+ describe('mockMethodMatchers return snapshots', () => {
15
+ describe('toHaveBeenCalled', () => {
16
+ it('fail', () => {
17
+ const options = {
18
+ isNot: false,
19
+ promise: false,
20
+ toHaveBeenCalled,
21
+ };
22
+
23
+ const method = mock.fn();
24
+
25
+ const result = options.toHaveBeenCalled(method);
26
+ const message = stripAnsi(result.message());
27
+
28
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
29
+ expect(message).toStrictEqual(
30
+ expect.stringContaining('expect(mock.fn()).false.toHaveBeenCalled()')
31
+ );
32
+ expect(message).toStrictEqual(
33
+ expect.stringContaining('Expected number of calls: >= 1')
34
+ );
35
+ expect(message).toStrictEqual(
36
+ expect.stringContaining('Received number of calls: 0')
37
+ );
38
+ });
39
+
40
+ it('not pass', () => {
41
+ const options = {
42
+ isNot: true,
43
+ promise: false,
44
+ toHaveBeenCalled,
45
+ };
46
+ const method = mock.fn();
47
+
48
+ const result = options.toHaveBeenCalled(method);
49
+ const message = stripAnsi(result.message());
50
+
51
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
52
+ expect(message).toStrictEqual(
53
+ expect.stringContaining(
54
+ 'expect(mock.fn()).false.not.toHaveBeenCalled()'
55
+ )
56
+ );
57
+ expect(message).toStrictEqual(
58
+ expect.stringContaining('Expected number of calls: >= 1')
59
+ );
60
+ expect(message).toStrictEqual(
61
+ expect.stringContaining('Received number of calls: 0')
62
+ );
63
+ });
64
+ });
65
+
66
+ describe('toHaveBeenCalledTimes', () => {
67
+ it('fail', () => {
68
+ const options = {
69
+ isNot: false,
70
+ promise: false,
71
+ toHaveBeenCalledTimes,
72
+ };
73
+
74
+ const method = mock.fn();
75
+
76
+ const result = options.toHaveBeenCalledTimes(method, 1);
77
+ const message = stripAnsi(result.message());
78
+
79
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
80
+ expect(message).toStrictEqual(
81
+ expect.stringContaining(
82
+ 'expect(mock.fn()).false.toHaveBeenCalledTimes(expected)'
83
+ )
84
+ );
85
+ expect(message).toStrictEqual(
86
+ expect.stringContaining('Expected number of calls: 1')
87
+ );
88
+ expect(message).toStrictEqual(
89
+ expect.stringContaining('Received number of calls: 0')
90
+ );
91
+ });
92
+
93
+ it('not pass', () => {
94
+ const options = {
95
+ isNot: true,
96
+ promise: false,
97
+ toHaveBeenCalledTimes,
98
+ };
99
+ const method = mock.fn();
100
+
101
+ const result = options.toHaveBeenCalledTimes(method, 1);
102
+ const message = stripAnsi(result.message());
103
+
104
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
105
+ expect(message).toStrictEqual(
106
+ expect.stringContaining(
107
+ 'expect(mock.fn()).false.not.toHaveBeenCalledTimes(expected)'
108
+ )
109
+ );
110
+ expect(message).toStrictEqual(
111
+ expect.stringContaining('Expected number of calls: 1')
112
+ );
113
+ expect(message).toStrictEqual(
114
+ expect.stringContaining('Received number of calls: 0')
115
+ );
116
+ });
117
+ });
118
+
119
+ describe('toHaveBeenCalledWith', () => {
120
+ it('fail', () => {
121
+ const options = {
122
+ isNot: false,
123
+ promise: false,
124
+ toHaveBeenCalledWith,
125
+ };
126
+
127
+ const method = mock.fn();
128
+
129
+ const result = options.toHaveBeenCalledWith(method, 1);
130
+ const message = stripAnsi(result.message());
131
+
132
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
133
+ expect(message).toStrictEqual(
134
+ expect.stringContaining(
135
+ 'expect(mock.fn()).false.toHaveBeenCalledWith(...expected)'
136
+ )
137
+ );
138
+ expect(message).toStrictEqual(expect.stringContaining('Expected: 1'));
139
+ expect(message).toStrictEqual(
140
+ expect.stringContaining('Number of calls: 0')
141
+ );
142
+ });
143
+
144
+ it('not pass', () => {
145
+ const options = {
146
+ isNot: true,
147
+ promise: false,
148
+ toHaveBeenCalledWith,
149
+ };
150
+ const method = mock.fn();
151
+
152
+ const result = options.toHaveBeenCalledWith(method, 1);
153
+ const message = stripAnsi(result.message());
154
+
155
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
156
+ expect(message).toStrictEqual(
157
+ expect.stringContaining(
158
+ 'expect(mock.fn()).false.not.toHaveBeenCalledWith(...expected)'
159
+ )
160
+ );
161
+ expect(message).toStrictEqual(expect.stringContaining('Expected: 1'));
162
+ expect(message).toStrictEqual(
163
+ expect.stringContaining('Number of calls: 0')
164
+ );
165
+ });
166
+ });
167
+
168
+ describe('toHaveBeenLastCalledWith', () => {
169
+ it('fail', () => {
170
+ const options = {
171
+ isNot: false,
172
+ promise: false,
173
+ toHaveBeenLastCalledWith,
174
+ };
175
+
176
+ const method = mock.fn();
177
+
178
+ const result = options.toHaveBeenLastCalledWith(method, 1);
179
+ const message = stripAnsi(result.message());
180
+
181
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
182
+ expect(message).toStrictEqual(
183
+ expect.stringContaining(
184
+ 'expect(mock.fn()).false.toHaveBeenLastCalledWith(...expected)'
185
+ )
186
+ );
187
+ expect(message).toStrictEqual(expect.stringContaining('Expected: 1'));
188
+ expect(message).toStrictEqual(
189
+ expect.stringContaining('Number of calls: 0')
190
+ );
191
+ });
192
+
193
+ it('not pass', () => {
194
+ const options = {
195
+ isNot: true,
196
+ promise: false,
197
+ toHaveBeenLastCalledWith,
198
+ };
199
+ const method = mock.fn();
200
+
201
+ const result = options.toHaveBeenLastCalledWith(method, 1);
202
+ const message = stripAnsi(result.message());
203
+
204
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
205
+ expect(message).toStrictEqual(
206
+ expect.stringContaining(
207
+ 'expect(mock.fn()).false.not.toHaveBeenLastCalledWith(...expected)'
208
+ )
209
+ );
210
+ expect(message).toStrictEqual(expect.stringContaining('Expected: 1'));
211
+ expect(message).toStrictEqual(
212
+ expect.stringContaining('Number of calls: 0')
213
+ );
214
+ });
215
+ });
216
+
217
+ describe('toHaveBeenNthCalledWith', () => {
218
+ it('fail', () => {
219
+ const options = {
220
+ isNot: false,
221
+ promise: false,
222
+ toHaveBeenNthCalledWith,
223
+ };
224
+
225
+ const method = mock.fn();
226
+
227
+ const result = options.toHaveBeenNthCalledWith(method, 1, 1);
228
+ const message = stripAnsi(result.message());
229
+
230
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
231
+ expect(message).toStrictEqual(
232
+ expect.stringContaining(
233
+ 'expect(mock.fn()).false.toHaveBeenNthCalledWith(...expected)'
234
+ )
235
+ );
236
+ expect(message).toStrictEqual(expect.stringContaining('Expected: 1'));
237
+ expect(message).toStrictEqual(
238
+ expect.stringContaining('Number of calls: 0')
239
+ );
240
+ });
241
+
242
+ it('not pass', () => {
243
+ const options = {
244
+ isNot: true,
245
+ promise: false,
246
+ toHaveBeenNthCalledWith,
247
+ };
248
+ const method = mock.fn();
249
+
250
+ const result = options.toHaveBeenNthCalledWith(method, 1, 1);
251
+ const message = stripAnsi(result.message());
252
+
253
+ expect(result).toEqual({ message: expect.any(Function), pass: false });
254
+ expect(message).toStrictEqual(
255
+ expect.stringContaining(
256
+ 'expect(mock.fn()).false.not.toHaveBeenNthCalledWith(...expected)'
257
+ )
258
+ );
259
+ expect(message).toStrictEqual(expect.stringContaining('Expected: 1'));
260
+ expect(message).toStrictEqual(
261
+ expect.stringContaining('Number of calls: 0')
262
+ );
263
+ });
264
+ });
265
+ });
package/lib/index.mjs CHANGED
@@ -6,6 +6,12 @@ import {
6
6
  toHaveBeenCalledWith,
7
7
  toHaveBeenLastCalledWith,
8
8
  toHaveBeenNthCalledWith,
9
+ toReturn,
10
+ toHaveReturned,
11
+ toHaveReturnedTimes,
12
+ toHaveReturnedWith,
13
+ toHaveLastReturnedWith,
14
+ toHaveNthReturnedWith,
9
15
  } from './mockMethodMatchers.mjs';
10
16
 
11
17
  expect.extend({
@@ -14,4 +20,10 @@ expect.extend({
14
20
  toHaveBeenCalledWith,
15
21
  toHaveBeenLastCalledWith,
16
22
  toHaveBeenNthCalledWith,
23
+ toReturn,
24
+ toHaveReturned,
25
+ toHaveReturnedTimes,
26
+ toHaveReturnedWith,
27
+ toHaveLastReturnedWith,
28
+ toHaveNthReturnedWith,
17
29
  });