a_mock 2.0.2 → 2.0.3
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/.github/workflows/ci.yml +19 -0
- package/README.md +615 -1
- package/and/newBinaryAndSpec/test.js +1 -7
- package/and/newMonadicAndSpec/test.js +2 -5
- package/andSpec/test.js +1 -4
- package/andSpec/testIntegration.js +1 -2
- package/eventEmitter/addSpec/test.js +2 -4
- package/eventEmitter/emitterContextSpec/test.js +2 -3
- package/eventEmitter/removeSpec/test.js +2 -4
- package/eventEmitter/tryAddSpec/test.js +3 -6
- package/eventEmitter/tryRemoveSpec/test.js +2 -5
- package/eventEmitterSpec/test.js +1 -7
- package/expectRequireSpec/test.js +1 -5
- package/index.d.ts +134 -105
- package/mock/mockContextSpec/test.js +2 -5
- package/mock/mockFuncProperties.js +37 -37
- package/mock/mockFuncPropertiesSpec/test.js +2 -5
- package/mock/objectMock.js +59 -46
- package/mock/objectMockSpec/test.js +10 -11
- package/mock/propertyMockSpec/test.js +2 -4
- package/mock/throwUnexpectedArgumentsSpec/test.js +2 -3
- package/mock.js +8 -8
- package/mockSpec/test.js +1 -5
- package/mockSpec/testIntegration.js +1 -5
- package/newMutableAndSpec/test.js +1 -5
- package/newThenSpec/testExecute.js +1 -5
- package/newThenSpec/testNoCallback.js +1 -3
- package/newThenSpec/testPreReject.js +2 -2
- package/newThenSpec/testPreRejectAndThrow.js +2 -2
- package/newThenSpec/testPreResolve.js +1 -2
- package/newThenSpec/testPreResolveAndThrow.js +2 -2
- package/newThenSpec/testReject.js +2 -2
- package/newThenSpec/testRejectAndThrow.js +2 -2
- package/newThenSpec/testResolve.js +1 -2
- package/newThenSpec/testResolveAndThrow.js +2 -2
- package/newThenSpec/testResolveWithFailingPromise.js +1 -2
- package/newThenSpec/testResolveWithPromise.js +1 -2
- package/newTrueNTimesThenFalseSpec/test.js +1 -6
- package/newTrueOnceThenFalseSpec/test.js +1 -4
- package/package.json +5 -1
- package/partialMock/execute.js +10 -10
- package/partialMock/executeSpec/test.js +1 -4
- package/partialMock/expectAnything.js +3 -1
- package/partialMock/expectAnythingSpec/test.js +2 -8
- package/partialMock/expectArraySpec/test.js +2 -3
- package/partialMock/expectCore.js +3 -1
- package/partialMock/expectCoreSpec/test.js +2 -14
- package/partialMock/expectEmptySpec/test.js +2 -10
- package/partialMock/expectSpec/test.js +2 -3
- package/partialMock/hasSameArgument/isEqualArgSpec/test.js +7 -31
- package/partialMock/ignoreSpec/test.js +2 -3
- package/partialMock/mockContext/reset.js +15 -15
- package/partialMock/mockContext/resetSpec/test.js +4 -4
- package/partialMock/negotiateDecrementExpectCountSpec/test.js +1 -4
- package/partialMock/negotiateEndSpec/test.js +1 -6
- package/partialMock/negotiateIncrementExpectCountSpec/test.js +1 -4
- package/partialMock/newExecute.js +9 -9
- package/partialMock/newExecuteSpec/test.js +2 -6
- package/partialMock/newFallbackWrapper.js +29 -29
- package/partialMock/newFallbackWrapperSpec/test.js +2 -6
- package/partialMock/newFallbackWrapperSpec/testWithMockError.js +2 -4
- package/partialMock/newFallbackWrapperSpec/testWithOtherError.js +2 -5
- package/partialMock/newHasArgumentSpec/test.js +2 -5
- package/partialMock/newHasEqualArgumentArraySpec/test.js +2 -7
- package/partialMock/newHasNoMoreArgumentsSpec/test.js +2 -7
- package/partialMock/newHasSameArgumentSpec/test.js +2 -10
- package/partialMock/newMockContext.js +12 -12
- package/partialMock/newMockContextSpec/test.js +6 -6
- package/partialMock/newThrowSpec/test.js +2 -3
- package/partialMock/returnSpec/test.js +2 -5
- package/partialMock/setExecuteSpec/test.js +2 -3
- package/partialMock/simple/expectRequireSpec/test.js +2 -4
- package/partialMock/simple/newMock/throwUnexpectedArgumentsSpec/test.js +3 -4
- package/partialMock/simple/newMockSpec/test.js +2 -3
- package/partialMock/simple/newPartialMock.js +2 -0
- package/partialMock/simple/newPartialMockSpec/test.js +2 -8
- package/partialMock/simple/newPartialMockSpec/testRepeat.js +2 -5
- package/partialMock/simple/newPartialMockSpec/testWithMultiArgs.js +2 -4
- package/partialMock/simple/newPartialMockSpec/testWithSingleArg.js +2 -7
- package/partialMock/simple/newRequireMockSpec/test.js +2 -3
- package/partialMock/verifySpec/test.js +1 -4
- package/partialMock.js +12 -10
- package/partialMockSpec/test.js +3 -10
- package/partialMockSpec/testIntegration.js +1 -2
- package/readmeSpec/testREADME.js +199 -0
- package/requireMockSpec/test.js +1 -2
- package/strictMockSpec/test.js +1 -2
- package/util.js +7 -7
- package/test.js +0 -17
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
test:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
- name: Use Node.js 24
|
|
13
|
+
uses: actions/setup-node@v4
|
|
14
|
+
with:
|
|
15
|
+
node-version: 24
|
|
16
|
+
- name: Install dependencies
|
|
17
|
+
run: npm install
|
|
18
|
+
- name: Run tests
|
|
19
|
+
run: npm test
|
package/README.md
CHANGED
|
@@ -1 +1,615 @@
|
|
|
1
|
-
|
|
1
|
+
_a_mock_
|
|
2
|
+
===
|
|
3
|
+
_Mocking framework_
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
The mocking framework can be used in any JavaScript testing framework.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
__To install:__
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install a_mock
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
__If you want the test framework, install it globally too:__
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
npm install a_mock -g
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
_Mocking_
|
|
26
|
+
===================
|
|
27
|
+
|
|
28
|
+
Mocking a function
|
|
29
|
+
------------------
|
|
30
|
+
|
|
31
|
+
__Partial mock__
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
var original = function() {
|
|
35
|
+
return 'realValue';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var mock = require('a_mock').mock(original);
|
|
39
|
+
original = mock;
|
|
40
|
+
mock.expect().return('fake');
|
|
41
|
+
|
|
42
|
+
original(); //returns 'fake'
|
|
43
|
+
original(); //returns 'realValue'
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Note: Consumers do not need to provide a `thisArg`. It is optional and only used to force a specific `this` when the original fallback is called (low-level partial mock usage).
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
__Strict mock__
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
var mock = require('a_mock').mock();
|
|
54
|
+
mock.expect().return('fake');
|
|
55
|
+
|
|
56
|
+
mock(); //returns 'fake'
|
|
57
|
+
mock(); //throws unexpected arguments
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
__Expecting arguments__
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
var mock = require('a_mock').mock();
|
|
66
|
+
mock.expect('testValue1').return('fake1');
|
|
67
|
+
mock.expect('testValue2').return('fake2');
|
|
68
|
+
|
|
69
|
+
mock('testValue1'); //returns 'fake1'
|
|
70
|
+
mock('testValue2'); //returns 'fake2'
|
|
71
|
+
mock(); //throws unexpected arguments
|
|
72
|
+
mock('foo'); //throws unexpected arguments
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
__Expecting multiple arguments__
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
var mock = require('a_mock').mock();
|
|
81
|
+
mock.expect('firstArg1', 'secondArg1').return('fake1');
|
|
82
|
+
mock.expect('firstArg2', 'secondArg2').return('fake2');
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
mock('firstArg1', 'secondArg1'); //returns 'fake1'
|
|
86
|
+
mock('firstArg2', 'secondArg2'); //returns 'fake2'
|
|
87
|
+
mock('foo'); //throws unexpected arguments
|
|
88
|
+
mock('foo', 'bar'); //throws unexpected arguments
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
__Expecting array__
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
var mock = require('a_mock').mock();
|
|
95
|
+
mock.expect(['a','b']).return('fake1');
|
|
96
|
+
mock.expect(['a','b']).return('fake2');
|
|
97
|
+
mock.expect(['c','d').return('fake3');
|
|
98
|
+
|
|
99
|
+
mock(['a','b']); //returns 'fake1'
|
|
100
|
+
mock(['a','b']); //returns 'fake2'
|
|
101
|
+
mock(['c','d']); //returns 'fake3'
|
|
102
|
+
mock(['a','b']); //throws unexpected arguments
|
|
103
|
+
mock(['foo', 'bar']); //throws unexpected arguments
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
__Expecting struct__
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
var mock = require('a_mock').mock();
|
|
110
|
+
var obj = {};
|
|
111
|
+
mock.expect({a : 1}).return('fake1');
|
|
112
|
+
mock.expect({a : 2}).return('fake2');
|
|
113
|
+
mock.expect({a : 2, b : {c : 'foo', d : ['me', 'too']}}).return('fake3');
|
|
114
|
+
mock.expect(obj).return('fake4');
|
|
115
|
+
mock.expect({}).return('will never happen');
|
|
116
|
+
|
|
117
|
+
mock({a : 'x'}); //throws unexpected arguments
|
|
118
|
+
mock({a : 1}); //returns 'fake1'
|
|
119
|
+
mock({a : 2}); //returns 'fake2'
|
|
120
|
+
mock({a : 2, b : {c : 'foo', d : ['me', 'too']}}); //returns 'fake3'
|
|
121
|
+
mock(obj); //returns 'fake4'
|
|
122
|
+
mock({}); //throws unexpected arguments
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
__Repeats__
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
var mock = require('a_mock').mock();
|
|
129
|
+
mock.expect().return('fake').repeat(2);
|
|
130
|
+
|
|
131
|
+
mock(); //returns 'fake'
|
|
132
|
+
mock(); //returns 'fake'
|
|
133
|
+
mock(); //throws unexpected arguments
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
__Infinite repeats__
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
var mock = require('a_mock').mock();
|
|
140
|
+
mock.expect().return('fake').repeatAny();
|
|
141
|
+
|
|
142
|
+
mock(); //returns 'fake'
|
|
143
|
+
mock(); //returns 'fake'
|
|
144
|
+
mock(); //returns 'fake'...
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
__Ignoring a single argument__
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
var mock = require('a_mock').mock();
|
|
152
|
+
mock.ignore().expect('foo').return('fake1');
|
|
153
|
+
|
|
154
|
+
mock('ignore me', 'foo'); //returns 'fake1'
|
|
155
|
+
mock(); //throws unexpected arguments
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
__Ignoring all arguments__
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
var mock = require('a_mock').mock();
|
|
162
|
+
mock.ignoreAll().return('fake1'); //same as expectAnything
|
|
163
|
+
|
|
164
|
+
mock('someRandomValue', 'whatever'); //returns 'fake1'
|
|
165
|
+
mock(); //throws unexpected arguments
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
__Throwing exceptions__
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
var mock = require('a_mock').mock();
|
|
173
|
+
var error = new Error('invalid operation');
|
|
174
|
+
mock.expect().throw(error);
|
|
175
|
+
mock.expect().return('fake');
|
|
176
|
+
|
|
177
|
+
mock(); //throws error
|
|
178
|
+
mock(); //returns 'fake'
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
__Intercepting__
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
var mock = require('a_mock').mock();
|
|
185
|
+
mock.expect('testValue').whenCalled(onCalled).return('fake1');
|
|
186
|
+
|
|
187
|
+
function onCalled(arg) {
|
|
188
|
+
//arg == 'testValue'
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
mock('testValue'); //returns 'fake1'
|
|
192
|
+
mock(); //throws unexpected arguments
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
__Verify (fail)__
|
|
196
|
+
|
|
197
|
+
```js
|
|
198
|
+
var mock = require('a_mock').mock();
|
|
199
|
+
mock.expect('testValue1').return('fake1');
|
|
200
|
+
mock.expect('testValue2').return('fake2');
|
|
201
|
+
|
|
202
|
+
mock('testValue1'); //returns 'fake1'
|
|
203
|
+
mock.verify(); //throws mock has 1 pending functions
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
__Verify (success)__
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
var mock = require('a_mock').mock();
|
|
210
|
+
mock.expect('testValue1').return('fake1');
|
|
211
|
+
mock.expect('testValue2').return('fake2');
|
|
212
|
+
|
|
213
|
+
mock('testValue1'); //returns 'fake1'
|
|
214
|
+
mock('testValue2'); //returns 'fake2'
|
|
215
|
+
mock.verify(); //returns true
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
__returning void (compact syntax)__
|
|
219
|
+
|
|
220
|
+
```js
|
|
221
|
+
var mock = require('a_mock').mock();
|
|
222
|
+
mock.expect('testValue1');
|
|
223
|
+
mock.expect('testValue2').repeat(2);
|
|
224
|
+
|
|
225
|
+
mock('testValue1'); //returns undefined
|
|
226
|
+
mock('testValue2'); //returns undefined
|
|
227
|
+
mock('testValue2'); //returns undefined
|
|
228
|
+
mock.verify(); //returns true
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
__..is equivalent to ..__
|
|
232
|
+
```js
|
|
233
|
+
var mock = require('a_mock').mock();
|
|
234
|
+
mock.expect('testValue1').return();
|
|
235
|
+
mock.expect('testValue2').return().repeat(2);
|
|
236
|
+
|
|
237
|
+
mock('testValue1'); //returns undefined
|
|
238
|
+
mock('testValue2'); //returns undefined
|
|
239
|
+
mock('testValue2'); //returns undefined
|
|
240
|
+
mock.verify(); //returns true
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
__Reset mock__
|
|
244
|
+
```js
|
|
245
|
+
var original = function() {
|
|
246
|
+
return 'realValue';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
var mock = require('a_mock').mock(original);
|
|
250
|
+
original = mock;
|
|
251
|
+
mock.expect().return('fake');
|
|
252
|
+
mock.reset();
|
|
253
|
+
|
|
254
|
+
original(); //returns 'realValue'
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
__Returning resolved promise__
|
|
258
|
+
```js
|
|
259
|
+
var mock = require('a_mock').mock();
|
|
260
|
+
mock.expect('foo').resolve('fake');
|
|
261
|
+
|
|
262
|
+
mock('foo').then(function(returned){
|
|
263
|
+
//returned == 'fake'
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
__Returning rejected promise__
|
|
268
|
+
```js
|
|
269
|
+
var mock = require('a_mock').mock();
|
|
270
|
+
mock.expect('foo').reject('fake');
|
|
271
|
+
|
|
272
|
+
mock('foo').then(null, function(returned){
|
|
273
|
+
//returned == 'fake'
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
__Strict mock - advanced scenario__
|
|
278
|
+
|
|
279
|
+
```js
|
|
280
|
+
var mock = require('a_mock').mock();
|
|
281
|
+
mock.expect('testValue').ignore().whenCalled(onCalled).return('fake1');
|
|
282
|
+
|
|
283
|
+
function onCalled(arg,callback) {
|
|
284
|
+
//arg == 'testValue'
|
|
285
|
+
//callback == foo
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function foo() {
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
mock('testValue', foo); //returns 'fake1'
|
|
293
|
+
mock.verify() //returns true
|
|
294
|
+
mock('testValue',foo); //throws unexpected arguments
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Mocking require
|
|
298
|
+
----------------
|
|
299
|
+
|
|
300
|
+
__expectRequire__
|
|
301
|
+
|
|
302
|
+
```js
|
|
303
|
+
var fakeDep = {};
|
|
304
|
+
|
|
305
|
+
var expectRequire = require('a_mock').expectRequire;
|
|
306
|
+
expectRequire('./realDep').return(fakeDep);
|
|
307
|
+
|
|
308
|
+
require('./realDep'); //returns fakeDep
|
|
309
|
+
require('./realDep'); //returns realDep (behaves like a partial mock)
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
__requireMock (compact syntax)__
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
var requireMock = require('a_mock').requireMock;
|
|
316
|
+
var fakeDep = requireMock('./realDep'); //returns a strict mock
|
|
317
|
+
|
|
318
|
+
require('./realDep'); //returns fakeDep
|
|
319
|
+
require('./realDep'); //returns realDep
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
__..is equivalent to ..__
|
|
323
|
+
|
|
324
|
+
```js
|
|
325
|
+
var mock = require('a_mock').mock();
|
|
326
|
+
var expectRequire = require('a_mock').expectRequire;
|
|
327
|
+
|
|
328
|
+
var fakeDep = mock;
|
|
329
|
+
expectRequire('./realDep').return(fakeDep);
|
|
330
|
+
|
|
331
|
+
require('./realDep'); //returns fakeDep
|
|
332
|
+
require('./realDep'); //returns realDep
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
__Reset mocks for require__
|
|
336
|
+
|
|
337
|
+
```js
|
|
338
|
+
var fakeDep = {};
|
|
339
|
+
|
|
340
|
+
var expectRequire = require('a_mock').expectRequire;
|
|
341
|
+
expectRequire('./realDep').return(fakeDep);
|
|
342
|
+
expectRequire.reset();
|
|
343
|
+
|
|
344
|
+
require('./realDep'); //returns realDep
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
__..is equivalent to ..__
|
|
348
|
+
|
|
349
|
+
```js
|
|
350
|
+
var requireMock = require('a_mock').requireMock;
|
|
351
|
+
var fakeDep = requireMock('./realDep'); //returns a strict mock
|
|
352
|
+
requireMock.reset(); //is an alias for expectRequire.reset()
|
|
353
|
+
|
|
354
|
+
require('./realDep'); //returns realDep
|
|
355
|
+
|
|
356
|
+
```
|
|
357
|
+
Mocking an object
|
|
358
|
+
-----------------
|
|
359
|
+
__Partial object mock__
|
|
360
|
+
|
|
361
|
+
```js
|
|
362
|
+
function newCustomer(_name) {
|
|
363
|
+
var c = {};
|
|
364
|
+
|
|
365
|
+
c.getName = function ()
|
|
366
|
+
{
|
|
367
|
+
return _name;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
return c;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
var customer = newCustomer('Alfonzo The Real');
|
|
374
|
+
var customerMock = mock(customer);
|
|
375
|
+
|
|
376
|
+
customerMock.getName.expect().return('Johnny Fake');
|
|
377
|
+
|
|
378
|
+
customer.getName(); //returns Johnny Fake
|
|
379
|
+
customer.getName(); //returns Alfonzo The Real
|
|
380
|
+
customerMock.verify(); //returns true
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
Mocking promises
|
|
384
|
+
-----------------
|
|
385
|
+
__Mocking resolve__
|
|
386
|
+
|
|
387
|
+
```js
|
|
388
|
+
var a = require('a_mock');
|
|
389
|
+
|
|
390
|
+
var promise = a.promise(); //mocked promise
|
|
391
|
+
|
|
392
|
+
promise.then(success,error);
|
|
393
|
+
promise.resolve('success');
|
|
394
|
+
|
|
395
|
+
function success(arg) {
|
|
396
|
+
console.log(arg);//success
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function error(e) {
|
|
400
|
+
//will not happen
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
__Mocking resolve (alternative syntax)__
|
|
405
|
+
|
|
406
|
+
```js
|
|
407
|
+
var a = require('a_mock');
|
|
408
|
+
|
|
409
|
+
var promise = a.promise(); //mocked promise
|
|
410
|
+
|
|
411
|
+
promise.then(success,error);
|
|
412
|
+
promise('success');
|
|
413
|
+
|
|
414
|
+
function success(arg) {
|
|
415
|
+
console.log(arg);//success
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function error(e) {
|
|
419
|
+
//will not happen
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
__Mocking reject__
|
|
424
|
+
|
|
425
|
+
```js
|
|
426
|
+
var a = require('a_mock');
|
|
427
|
+
|
|
428
|
+
var promise = a.promise();
|
|
429
|
+
|
|
430
|
+
promise.then(success,error);
|
|
431
|
+
promise.reject(new Error('error'));
|
|
432
|
+
|
|
433
|
+
function success(arg) {
|
|
434
|
+
//will not happen
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function error(e) {
|
|
438
|
+
console.log(e.stack);//will happen
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
__Mocking reject (alternative syntax)__
|
|
443
|
+
|
|
444
|
+
```js
|
|
445
|
+
var a = require('a_mock');
|
|
446
|
+
|
|
447
|
+
var promise = a.promise();
|
|
448
|
+
|
|
449
|
+
promise.then(success,error);
|
|
450
|
+
promise(null,new Error('error'));
|
|
451
|
+
|
|
452
|
+
function success(arg) {
|
|
453
|
+
//will not happen
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function error(e) {
|
|
457
|
+
console.log(e.stack);//will happen
|
|
458
|
+
}
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
_A test framework_
|
|
462
|
+
===================
|
|
463
|
+
<b>From version 3.0.0 this is in a separate package: [npmjs.com/package/a_test](https://npmjs.com/package/a_test)</b>
|
|
464
|
+
_A_ test framework is a simplistic, magic-free library providing unit-testing facilities with a compact, bdd-style syntax.
|
|
465
|
+
|
|
466
|
+
In contrast to other bdd-style test frameworks, it doesn't allow nesting suites in each other in order to test the SUT(subject under test) in different states. Instead, the framework relies on folder structure to describe the state. The SUT currently has that folder structure. Suite names are generated based on their filenames. As a result, there will be many small test files without nested test suites instead of a few big test files with nested test suites.
|
|
467
|
+
|
|
468
|
+
Test setup, the "Arrange-Act" part of suites, is separated from the "Assert" part. This way the same setup can be used across different suites. Test setups can be chained.
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
Examples below can be found here: https://github.com/alfateam/a_demo
|
|
472
|
+
|
|
473
|
+
Example
|
|
474
|
+
---------
|
|
475
|
+
The test runner ( _a_ ) will search for all files named 'when*.js' in the current and sub-directories.
|
|
476
|
+
|
|
477
|
+
Given the following file structure
|
|
478
|
+
|
|
479
|
+
- demo/
|
|
480
|
+
- counter.js
|
|
481
|
+
- counter_specs/
|
|
482
|
+
- new/
|
|
483
|
+
- increment.js
|
|
484
|
+
- when_incremented.js
|
|
485
|
+
- new.js
|
|
486
|
+
- when_new.js
|
|
487
|
+
|
|
488
|
+
__counter.js__
|
|
489
|
+
|
|
490
|
+
```js
|
|
491
|
+
module.exports = function () {
|
|
492
|
+
var counter = {
|
|
493
|
+
value: 0,
|
|
494
|
+
increment: function() { value++; }
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
return counter;
|
|
498
|
+
}
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
__counter_specs/new.js__
|
|
502
|
+
|
|
503
|
+
```js
|
|
504
|
+
function act(c) {
|
|
505
|
+
var createCounter = require('../counter');
|
|
506
|
+
c.sut = createCounter();
|
|
507
|
+
}
|
|
508
|
+
module.exports = act;
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
__counter_specs/when_new.js__
|
|
512
|
+
|
|
513
|
+
```js
|
|
514
|
+
var c = {}; //test context object
|
|
515
|
+
var when = require('a_mock').when;
|
|
516
|
+
|
|
517
|
+
when('./new', c). //set up
|
|
518
|
+
it('should be an object').
|
|
519
|
+
assertEqual('object', typeof c.sut)
|
|
520
|
+
it('should have value equal to zero').
|
|
521
|
+
assertEqual(0, c.sut.value).
|
|
522
|
+
it('should fail just for fun').
|
|
523
|
+
assertFail('error message');
|
|
524
|
+
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
__counter_specs/new/increment.js__
|
|
528
|
+
|
|
529
|
+
```js
|
|
530
|
+
function act(c) {
|
|
531
|
+
c.sut.increment();
|
|
532
|
+
}
|
|
533
|
+
act.base = '../new';
|
|
534
|
+
module.exports = act;
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
__counter_specs/new/when_incremented.js__
|
|
538
|
+
|
|
539
|
+
```js
|
|
540
|
+
var c = {};
|
|
541
|
+
var when = require('a_mock').when;
|
|
542
|
+
|
|
543
|
+
when('./increment', c).
|
|
544
|
+
it('should have value equal to 1').
|
|
545
|
+
assertEqual(1, c.sut.value);
|
|
546
|
+
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
__In demo directory, run _a_ __
|
|
550
|
+
|
|
551
|
+
user@localhost:~/a_demo $ a
|
|
552
|
+
|
|
553
|
+
» counter_specs » new
|
|
554
|
+
|
|
555
|
+
✓ should be an object
|
|
556
|
+
✓ should have value equal to zero
|
|
557
|
+
✘ should fail just for fun
|
|
558
|
+
|
|
559
|
+
» counter_specs » new » increment
|
|
560
|
+
|
|
561
|
+
✓ should have value equal to 1
|
|
562
|
+
|
|
563
|
+
========== Summary =============
|
|
564
|
+
|
|
565
|
+
counter_specs » new
|
|
566
|
+
✘ should fail just for fun
|
|
567
|
+
|
|
568
|
+
AssertionError: error message
|
|
569
|
+
at retval.assertFail (/home/user/a_demo/node_modules/a/when/it.js:14:11)
|
|
570
|
+
at Object.test (/home/user/a_demo/node_modules/a/when/test_invoker.js:5:3)
|
|
571
|
+
at Object.retval.assertFail (/home/user/a_demo/node_modules/a/when/it.js:13:5)
|
|
572
|
+
at Object.<anonymous> (/home/user/a_demo/counter_specs/when_new.js:11:3)
|
|
573
|
+
at Module._compile (module.js:449:26)
|
|
574
|
+
at Object.Module._extensions..js (module.js:467:10)
|
|
575
|
+
at Module.load (module.js:356:32)
|
|
576
|
+
at Function.Module._load (module.js:312:12)
|
|
577
|
+
at Module.require (module.js:362:17)
|
|
578
|
+
at require (module.js:378:17)
|
|
579
|
+
------------
|
|
580
|
+
|
|
581
|
+
suites: 2, passed: 3, failed: 1
|
|
582
|
+
|
|
583
|
+
Async test support
|
|
584
|
+
------------------
|
|
585
|
+
Modified act files should look like this:
|
|
586
|
+
|
|
587
|
+
```js
|
|
588
|
+
|
|
589
|
+
function act(c) {
|
|
590
|
+
...
|
|
591
|
+
return c.sut(c.arguments); //returns promise
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
or
|
|
597
|
+
|
|
598
|
+
```js
|
|
599
|
+
|
|
600
|
+
async function act(c) {
|
|
601
|
+
...
|
|
602
|
+
await c.sut(c.arguments);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
```
|
|
606
|
+
Test file should look like this:
|
|
607
|
+
```js
|
|
608
|
+
var when = require('a_mock').when;
|
|
609
|
+
var c = {};
|
|
610
|
+
|
|
611
|
+
when(c).then(it => {
|
|
612
|
+
it('....').assertXXXX();
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
```
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
var assert = require('assert');
|
|
2
|
-
var test = require('
|
|
2
|
+
var test = require('node:test');
|
|
3
3
|
var newRequireMock = require('../../partialMock/simple/newRequireMock');
|
|
4
4
|
var newMock = require('../../partialMock/simple/newMock');
|
|
5
5
|
|
|
@@ -8,13 +8,11 @@ var newSut = require('../newBinaryAnd');
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
(function(){
|
|
11
|
-
console.log('newBinaryAnd');
|
|
12
11
|
var predicate = newMock();
|
|
13
12
|
var predicate2 = newMock();
|
|
14
13
|
var sut = newSut(predicate,predicate2);
|
|
15
14
|
|
|
16
15
|
(function() {
|
|
17
|
-
console.log('execute when predicate1 returns false');
|
|
18
16
|
var arg = {};
|
|
19
17
|
predicate.expect(arg).return(false);
|
|
20
18
|
var returned = sut(arg);
|
|
@@ -25,7 +23,6 @@ var newSut = require('../newBinaryAnd');
|
|
|
25
23
|
})();
|
|
26
24
|
|
|
27
25
|
(function() {
|
|
28
|
-
console.log('execute when predicate1 returns true, predicate2 returns false');
|
|
29
26
|
var arg = {};
|
|
30
27
|
predicate.expect(arg).return(true);
|
|
31
28
|
predicate2.expect(arg).return(false);
|
|
@@ -37,7 +34,6 @@ var newSut = require('../newBinaryAnd');
|
|
|
37
34
|
})();
|
|
38
35
|
|
|
39
36
|
(function() {
|
|
40
|
-
console.log('execute when predicate1 returns true, predicate2 returns true');
|
|
41
37
|
var arg = {};
|
|
42
38
|
predicate.expect(arg).return(true);
|
|
43
39
|
predicate2.expect(arg).return(true);
|
|
@@ -49,7 +45,6 @@ var newSut = require('../newBinaryAnd');
|
|
|
49
45
|
})();
|
|
50
46
|
|
|
51
47
|
(function() {
|
|
52
|
-
console.log('add');
|
|
53
48
|
var binaryAnd = {};
|
|
54
49
|
var predicate = {};
|
|
55
50
|
var predicate2 = {};
|
|
@@ -64,4 +59,3 @@ var newSut = require('../newBinaryAnd');
|
|
|
64
59
|
});
|
|
65
60
|
})();
|
|
66
61
|
});
|
|
67
|
-
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
var assert = require('assert');
|
|
2
|
-
var test = require('
|
|
2
|
+
var test = require('node:test');
|
|
3
3
|
var newRequireMock = require('../../partialMock/simple/newRequireMock');
|
|
4
4
|
var newMock = require('../../partialMock/simple/newMock');
|
|
5
5
|
|
|
@@ -8,12 +8,10 @@ var newSut = require('../newMonadicAnd');
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
(function(){
|
|
11
|
-
console.log('newMonadicAnd');
|
|
12
11
|
var predicate = newMock();
|
|
13
12
|
var sut = newSut(predicate);
|
|
14
13
|
|
|
15
14
|
(function() {
|
|
16
|
-
console.log('execute');
|
|
17
15
|
var arg = {};
|
|
18
16
|
var expected = {};
|
|
19
17
|
predicate.expect(arg).return(expected);
|
|
@@ -27,7 +25,6 @@ var newSut = require('../newMonadicAnd');
|
|
|
27
25
|
|
|
28
26
|
|
|
29
27
|
(function() {
|
|
30
|
-
console.log('add');
|
|
31
28
|
var binaryAnd = {};
|
|
32
29
|
var predicate = {};
|
|
33
30
|
var predicate2 = {};
|
|
@@ -42,4 +39,4 @@ var newSut = require('../newMonadicAnd');
|
|
|
42
39
|
});
|
|
43
40
|
})();
|
|
44
41
|
|
|
45
|
-
})();
|
|
42
|
+
})();
|