expect-matcher-node-mock 1.0.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/.eslintrc.js ADDED
@@ -0,0 +1,82 @@
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 ADDED
@@ -0,0 +1 @@
1
+ 22
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Seznam.cz a.s.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Extend jest-expect matcher by native node test runner mock matchers
2
+ Extend for Jest [expect library](https://jestjs.io/docs/expect#expectextendmatchers).
3
+
4
+
5
+ Native node test runner does have diffrent mock structure from jest.mock therefore the extension of current methods is necessary.
6
+
7
+ ## Content:
8
+
9
+ ### toHaveBeenCalled
10
+ https://jestjs.io/docs/expect#tohavebeencalled
11
+
12
+ ### toHaveBeenCalledTimes
13
+ https://jestjs.io/docs/expect#tohavebeencalledtimesnumber
14
+
15
+ ### toHaveBeenCalledWith
16
+ https://jestjs.io/docs/expect#tohavebeencalledwitharg1-arg2-
17
+
18
+ ### toHaveBeenLastCalledWith
19
+ https://jestjs.io/docs/expect#tohavebeenlastcalledwitharg1-arg2-
20
+
21
+ ### toHaveBeenNthCalledWith
22
+ https://jestjs.io/docs/expect#tohavebeennthcalledwithnthcall-arg1-arg2-
23
+
24
+ ### TODO
25
+ add all toHaveReturnedXX methods
26
+
@@ -0,0 +1,58 @@
1
+ import { describe, it, mock } from 'node:test';
2
+
3
+ import { expect } from 'expect';
4
+
5
+ import '../index.mjs';
6
+
7
+ describe('mockMethodMatchers', () => {
8
+ it('toHaveBeenCalled', () => {
9
+ const method = mock.fn();
10
+ const notCalledMethod = mock.fn();
11
+
12
+ method();
13
+
14
+ expect(method).toHaveBeenCalled();
15
+ expect(notCalledMethod).not.toHaveBeenCalled();
16
+ });
17
+
18
+ it('toHaveBeenCalledTimes', () => {
19
+ const method = mock.fn();
20
+ const notCalledMethod = mock.fn();
21
+
22
+ method();
23
+ method();
24
+
25
+ expect(method).toHaveBeenCalledTimes(2);
26
+ expect(notCalledMethod).not.toHaveBeenCalledTimes(1);
27
+ });
28
+
29
+ it('toHaveBeenCalledWith', () => {
30
+ const method = mock.fn();
31
+
32
+ method('foo', 'bar');
33
+
34
+ expect(method).toHaveBeenCalledWith('foo', 'bar');
35
+ expect(method).not.toHaveBeenCalledWith('bar', 'foo');
36
+ });
37
+
38
+ it('toHaveBeenLastCalledWith', () => {
39
+ const method = mock.fn();
40
+
41
+ method('foo');
42
+ method('bar');
43
+
44
+ expect(method).toHaveBeenLastCalledWith('bar');
45
+ expect(method).not.toHaveBeenLastCalledWith('foo');
46
+ });
47
+
48
+ it('toHaveBeenNthCalledWith', () => {
49
+ const method = mock.fn();
50
+
51
+ method('foo');
52
+ method('bar');
53
+
54
+ expect(method).toHaveBeenNthCalledWith(1, 'foo');
55
+ expect(method).toHaveBeenNthCalledWith(2, 'bar');
56
+ expect(method).not.toHaveBeenNthCalledWith(1, 'bar');
57
+ });
58
+ });
package/lib/index.mjs ADDED
@@ -0,0 +1,17 @@
1
+ import { expect } from 'expect';
2
+
3
+ import {
4
+ toHaveBeenCalled,
5
+ toHaveBeenCalledTimes,
6
+ toHaveBeenCalledWith,
7
+ toHaveBeenLastCalledWith,
8
+ toHaveBeenNthCalledWith,
9
+ } from './mockMethodMatchers.mjs';
10
+
11
+ expect.extend({
12
+ toHaveBeenCalled,
13
+ toHaveBeenCalledTimes,
14
+ toHaveBeenCalledWith,
15
+ toHaveBeenLastCalledWith,
16
+ toHaveBeenNthCalledWith,
17
+ });
@@ -0,0 +1,256 @@
1
+ import { expect } from 'expect';
2
+ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
3
+
4
+ /**
5
+ * toHaveBeenCalled
6
+ */
7
+ function toHaveBeenCalled(method) {
8
+ expect(typeof method).toBe('function');
9
+
10
+ // detect native node test runner mock
11
+ if (typeof method.mock === 'object') {
12
+ const matcherName = 'toHaveBeenCalled';
13
+ const options = {
14
+ isNot: this.isNot,
15
+ promise: this.promise,
16
+ };
17
+
18
+ const pass = method.mock.calls.length > 0;
19
+
20
+ if (pass) {
21
+ return {
22
+ pass,
23
+ message: () =>
24
+ `${matcherHint(
25
+ matcherName,
26
+ undefined,
27
+ undefined,
28
+ options
29
+ )}\nexpected ${method.name} not to be called`,
30
+ };
31
+ }
32
+
33
+ return {
34
+ pass,
35
+ message: () =>
36
+ `${matcherHint(matcherName, undefined, undefined, options)}\nexpected ${
37
+ method.name
38
+ } to be called`,
39
+ };
40
+ }
41
+
42
+ return expect(method).toHaveBeenCalled();
43
+ }
44
+
45
+ /**
46
+ * toHaveBeenCalledTimes
47
+ */
48
+ function toHaveBeenCalledTimes(method, expected) {
49
+ expect(typeof method).toBe('function');
50
+
51
+ // detect native node test runßßner mock
52
+ if (typeof method.mock === 'object') {
53
+ const matcherName = 'toHaveBeenCalledTimes';
54
+ const options = {
55
+ isNot: this.isNot,
56
+ promise: this.promise,
57
+ };
58
+ const received = method.mock.calls.length;
59
+
60
+ const pass = received === expected;
61
+
62
+ if (pass) {
63
+ return {
64
+ pass,
65
+ message: () =>
66
+ `${matcherHint(
67
+ matcherName,
68
+ undefined,
69
+ undefined,
70
+ options
71
+ )}\nExpected: ${printExpected(expected)}\nReceived: ${printReceived(
72
+ received
73
+ )}`,
74
+ };
75
+ }
76
+
77
+ return {
78
+ pass,
79
+ message: () =>
80
+ `${matcherHint(
81
+ matcherName,
82
+ undefined,
83
+ undefined,
84
+ options
85
+ )}\nExpected: ${printExpected(expected)}\nReceived: ${printReceived(
86
+ received
87
+ )}`,
88
+ };
89
+ }
90
+
91
+ return expect(method).toHaveBeenCalledTimes(expected);
92
+ }
93
+
94
+ /**
95
+ * toHaveBeenCalledWith
96
+ */
97
+ function toHaveBeenCalledWith(method, ...args) {
98
+ expect(typeof method).toBe('function');
99
+
100
+ // detect native node test runner mock
101
+ if (typeof method.mock === 'object') {
102
+ expect(method.mock.calls.length).toBeGreaterThan(0);
103
+
104
+ const matcherName = 'toHaveBeenCalledWith';
105
+ const options = {
106
+ isNot: this.isNot,
107
+ promise: this.promise,
108
+ };
109
+ const receivedArgs = [];
110
+
111
+ const pass = method.mock.calls.some(call => {
112
+ receivedArgs.push(call.arguments);
113
+ return args.every((arg, index) => call.arguments[index] === arg);
114
+ });
115
+
116
+ if (pass) {
117
+ return {
118
+ pass,
119
+ message: () =>
120
+ `${matcherHint(
121
+ matcherName,
122
+ undefined,
123
+ undefined,
124
+ options
125
+ )}\nExpected: ${printExpected(...args)}\nReceived: ${receivedArgs.map(
126
+ args => args.map(arg => printReceived(arg)).join(', ')
127
+ )}`,
128
+ };
129
+ }
130
+
131
+ return {
132
+ pass,
133
+ message: () =>
134
+ `${matcherHint(
135
+ matcherName,
136
+ undefined,
137
+ undefined,
138
+ options
139
+ )}\nExpected: ${printExpected(...args)}\nReceived: ${receivedArgs.map(
140
+ args => args.map(arg => printReceived(arg)).join(', ')
141
+ )}`,
142
+ };
143
+ }
144
+
145
+ return expect(method).toHaveBeenCalledWith(...args);
146
+ }
147
+
148
+ /**
149
+ * toHaveBeenLastCalledWith
150
+ */
151
+ function toHaveBeenLastCalledWith(method, ...args) {
152
+ expect(typeof method).toBe('function');
153
+
154
+ // detect native node test runner mock
155
+ if (typeof method.mock === 'object') {
156
+ expect(method.mock.calls.length).toBeGreaterThan(0);
157
+
158
+ const matcherName = 'toHaveBeenLastCalledWith';
159
+ const options = {
160
+ isNot: this.isNot,
161
+ promise: this.promise,
162
+ };
163
+ const lastCall = method.mock.calls[method.mock.calls.length - 1];
164
+
165
+ const pass = args.every((arg, index) => lastCall.arguments[index] === arg);
166
+
167
+ if (pass) {
168
+ return {
169
+ pass,
170
+ message: () =>
171
+ `${matcherHint(
172
+ matcherName,
173
+ undefined,
174
+ undefined,
175
+ options
176
+ )}\nExpected: ${printExpected(
177
+ ...args
178
+ )}\nReceived: ${lastCall.arguments.map(arg => printReceived(arg))}`,
179
+ };
180
+ }
181
+
182
+ return {
183
+ pass,
184
+ message: () =>
185
+ `${matcherHint(
186
+ matcherName,
187
+ undefined,
188
+ undefined,
189
+ options
190
+ )}\nExpected: ${printExpected(
191
+ ...args
192
+ )}\nReceived: ${lastCall.arguments.map(arg => printReceived(arg))}`,
193
+ };
194
+ }
195
+
196
+ return expect(method).toHaveBeenLastCalledWith(...args);
197
+ }
198
+
199
+ /**
200
+ * toHaveBeenNthCalledWith
201
+ */
202
+ function toHaveBeenNthCalledWith(method, nthCall, ...args) {
203
+ expect(typeof method).toBe('function');
204
+
205
+ // detect native node test runner mock
206
+ if (typeof method.mock === 'object') {
207
+ expect(method.mock.calls.length).toBeGreaterThan(nthCall - 1);
208
+
209
+ const matcherName = 'toHaveBeenNthCalledWith';
210
+ const options = {
211
+ isNot: this.isNot,
212
+ promise: this.promise,
213
+ };
214
+ const lastCall = method.mock.calls[nthCall - 1];
215
+
216
+ const pass = args.every((arg, index) => lastCall.arguments[index] === arg);
217
+
218
+ if (pass) {
219
+ return {
220
+ pass,
221
+ message: () =>
222
+ `${matcherHint(
223
+ matcherName,
224
+ undefined,
225
+ undefined,
226
+ options
227
+ )}\nExpected: ${printExpected(
228
+ ...args
229
+ )}\nReceived: ${lastCall.arguments.map(arg => printReceived(arg))}`,
230
+ };
231
+ }
232
+
233
+ return {
234
+ pass,
235
+ message: () =>
236
+ `${matcherHint(
237
+ matcherName,
238
+ undefined,
239
+ undefined,
240
+ options
241
+ )}\nExpected: ${printExpected(
242
+ ...args
243
+ )}\nReceived: ${lastCall.arguments.map(arg => printReceived(arg))}`,
244
+ };
245
+ }
246
+
247
+ return expect(method).toHaveBeenNthCalledWith(nthCall, ...args);
248
+ }
249
+
250
+ export {
251
+ toHaveBeenCalled,
252
+ toHaveBeenCalledTimes,
253
+ toHaveBeenCalledWith,
254
+ toHaveBeenLastCalledWith,
255
+ toHaveBeenNthCalledWith,
256
+ };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "expect-matcher-node-mock",
3
+ "version": "1.0.0",
4
+ "description": "Jest matcher for Node.js mock objects",
5
+ "main": "lib/index.mjs",
6
+ "bugs": {
7
+ "url": "https://github.com/crysadrak/expect-matcher-node-mock/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/crysadrak/expect-matcher-node-mock"
12
+ },
13
+ "license": "MIT",
14
+ "author": "Crysa Drak <cd@l33t.cz>",
15
+ "scripts": {
16
+ "lint": "eslint --cache --cache-location=node_modules/.cache/ './**/*.{js,jsx,ts,tsx,mjs}'",
17
+ "test": "node --test --test-reporter=spec"
18
+ },
19
+ "devDependencies": {
20
+ "eslint": "^8.27.0",
21
+ "eslint-config-prettier": "^8.5.0",
22
+ "eslint-plugin-import": "^2.27.5",
23
+ "eslint-plugin-prettier": "^4.2.1",
24
+ "prettier": "^2.8.4"
25
+ },
26
+ "dependencies": {
27
+ "expect": "29.7.0",
28
+ "jest-matcher-utils": "29.7.0"
29
+ }
30
+ }