a_mock 2.0.3 → 2.0.5
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 +15 -178
- package/index.d.ts +17 -21
- package/package.json +18 -7
- package/partialMock/expectAnything.js +4 -3
- package/partialMock/expectCore.js +8 -7
- package/partialMock/expectEmpty.js +5 -4
package/README.md
CHANGED
|
@@ -13,15 +13,6 @@ npm install a_mock
|
|
|
13
13
|
```
|
|
14
14
|
|
|
15
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
16
|
_Mocking_
|
|
26
17
|
===================
|
|
27
18
|
|
|
@@ -103,24 +94,26 @@ mock(['a','b']); //throws unexpected arguments
|
|
|
103
94
|
mock(['foo', 'bar']); //throws unexpected arguments
|
|
104
95
|
```
|
|
105
96
|
|
|
106
|
-
__Expecting struct__
|
|
107
|
-
|
|
108
|
-
```js
|
|
109
|
-
var mock = require('a_mock').mock();
|
|
110
|
-
var obj = {};
|
|
97
|
+
__Expecting struct__
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
var mock = require('a_mock').mock();
|
|
101
|
+
var obj = {};
|
|
111
102
|
mock.expect({a : 1}).return('fake1');
|
|
112
103
|
mock.expect({a : 2}).return('fake2');
|
|
113
104
|
mock.expect({a : 2, b : {c : 'foo', d : ['me', 'too']}}).return('fake3');
|
|
114
105
|
mock.expect(obj).return('fake4');
|
|
115
106
|
mock.expect({}).return('will never happen');
|
|
116
107
|
|
|
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
|
-
```
|
|
108
|
+
mock({a : 'x'}); //throws unexpected arguments
|
|
109
|
+
mock({a : 1}); //returns 'fake1'
|
|
110
|
+
mock({a : 2}); //returns 'fake2'
|
|
111
|
+
mock({a : 2, b : {c : 'foo', d : ['me', 'too']}}); //returns 'fake3'
|
|
112
|
+
mock(obj); //returns 'fake4'
|
|
113
|
+
mock({}); //throws unexpected arguments cause leaf properties are not equal
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Note: Struct matching is strict on leaf properties. All leaf property values must be equal to match, and an empty object does not match a non-empty expected struct.
|
|
124
117
|
|
|
125
118
|
__Repeats__
|
|
126
119
|
|
|
@@ -456,160 +449,4 @@ function success(arg) {
|
|
|
456
449
|
function error(e) {
|
|
457
450
|
console.log(e.stack);//will happen
|
|
458
451
|
}
|
|
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
|
-
```
|
|
452
|
+
```
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
-
|
|
3
1
|
type AnyFunction = (...args: any[]) => any;
|
|
4
2
|
type Tail<T extends any[]> = T extends [any, ...infer R] ? R : [];
|
|
5
3
|
type OverloadedParameters<T> = T extends {
|
|
@@ -26,9 +24,7 @@ type FunctionProps<T> = T extends (...args: any[]) => any
|
|
|
26
24
|
: {};
|
|
27
25
|
type MockedFunction<T extends AnyFunction> = MockFunction<
|
|
28
26
|
OverloadedParameters<T>,
|
|
29
|
-
T
|
|
30
|
-
? Promise<AxiosResponse<any, any, {}>>
|
|
31
|
-
: OverloadedReturnType<T>
|
|
27
|
+
OverloadedReturnType<T>
|
|
32
28
|
> &
|
|
33
29
|
T &
|
|
34
30
|
FunctionProps<T>;
|
|
@@ -40,7 +36,7 @@ interface RepeatControl {
|
|
|
40
36
|
|
|
41
37
|
interface ExpectationTerminal<R, TArgs extends any[], TOrigArgs extends any[] = TArgs> {
|
|
42
38
|
return(value: R): RepeatControl;
|
|
43
|
-
|
|
39
|
+
returnLoose(value?: any): RepeatControl;
|
|
44
40
|
whenCalled(callback: CallbackFor<TOrigArgs>): ExpectationTerminal<R, TArgs, TOrigArgs>;
|
|
45
41
|
repeat(times: number): RepeatControl;
|
|
46
42
|
repeatAny(): RepeatControl;
|
|
@@ -55,25 +51,25 @@ interface ExpectationChain<TArgs extends any[], R, TOrigArgs extends any[] = TAr
|
|
|
55
51
|
expect(arg?: TArgs[0]): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
|
|
56
52
|
/** @deprecated Use ignoreAll() instead. */
|
|
57
53
|
expectAnything(): ExpectationChain<any[], R, TOrigArgs>;
|
|
58
|
-
ignoreAll(): ExpectationChain<any[], R, TOrigArgs>;
|
|
59
|
-
expectLoose(...args: any[]): ExpectationChain<any[], any, TOrigArgs>;
|
|
60
|
-
ignore(): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
|
|
61
|
-
/** @deprecated Use expect() instead. */
|
|
62
|
-
expectArray(
|
|
63
|
-
value?: TArgs[0] extends any[] ? TArgs[0] : any[]
|
|
64
|
-
): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
|
|
54
|
+
ignoreAll(): ExpectationChain<any[], R, TOrigArgs>;
|
|
55
|
+
expectLoose(...args: any[]): ExpectationChain<any[], any, TOrigArgs>;
|
|
56
|
+
ignore(): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
|
|
57
|
+
/** @deprecated Use expect() instead. */
|
|
58
|
+
expectArray(
|
|
59
|
+
value?: TArgs[0] extends any[] ? TArgs[0] : any[]
|
|
60
|
+
): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
|
|
65
61
|
}
|
|
66
62
|
|
|
67
63
|
interface MockFunction<TArgs extends any[], R> {
|
|
68
64
|
(...args: TArgs): R;
|
|
69
|
-
// Allow any-args calls in addition to the original signature.
|
|
70
|
-
(...args: any[]): any;
|
|
71
|
-
expect(...args: OptionalTuple<TArgs>): ExpectationChain<Tail<TArgs>, R, TArgs>;
|
|
72
|
-
ignore(): ExpectationChain<Tail<TArgs>, R, TArgs>;
|
|
73
|
-
/** @deprecated Use expect() instead. */
|
|
74
|
-
expectArray(
|
|
75
|
-
value?: TArgs[0] extends any[] ? TArgs[0] : any[]
|
|
76
|
-
): ExpectationChain<Tail<TArgs>, R, TArgs>;
|
|
65
|
+
// Allow any-args calls in addition to the original signature.
|
|
66
|
+
(...args: any[]): any;
|
|
67
|
+
expect(...args: OptionalTuple<TArgs>): ExpectationChain<Tail<TArgs>, R, TArgs>;
|
|
68
|
+
ignore(): ExpectationChain<Tail<TArgs>, R, TArgs>;
|
|
69
|
+
/** @deprecated Use expect() instead. */
|
|
70
|
+
expectArray(
|
|
71
|
+
value?: TArgs[0] extends any[] ? TArgs[0] : any[]
|
|
72
|
+
): ExpectationChain<Tail<TArgs>, R, TArgs>;
|
|
77
73
|
/** @deprecated Use ignoreAll() instead. */
|
|
78
74
|
expectAnything(): ExpectationChain<any[], R, TArgs>;
|
|
79
75
|
ignoreAll(): ExpectationChain<any[], R, TArgs>;
|
package/package.json
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "a_mock",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"title": "a_mock",
|
|
7
7
|
"description": "Sub package of a. Mocking framework",
|
|
8
|
-
"keywords": [
|
|
8
|
+
"keywords": [
|
|
9
|
+
"mock",
|
|
10
|
+
"mocking",
|
|
11
|
+
"partial mock",
|
|
12
|
+
"strict mock",
|
|
13
|
+
"tdd",
|
|
14
|
+
"stub",
|
|
15
|
+
"stubbing",
|
|
16
|
+
"mock require",
|
|
17
|
+
"verify"
|
|
18
|
+
],
|
|
9
19
|
"scripts": {
|
|
10
20
|
"test": "node --test **/*Spec/test*.js"
|
|
11
|
-
|
|
12
21
|
},
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git@github.com:alfateam/a_mock.git"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"axios": "^1.13.2"
|
|
17
28
|
}
|
|
18
29
|
}
|
|
@@ -26,9 +26,10 @@ function expectAnything(index,mockContext) {
|
|
|
26
26
|
|
|
27
27
|
c.ignoreAll = c.expectAnything;
|
|
28
28
|
|
|
29
|
-
c.return = function(arg) {
|
|
30
|
-
return _return(arg,index+1,mockContext);
|
|
31
|
-
};
|
|
29
|
+
c.return = function(arg) {
|
|
30
|
+
return _return(arg,index+1,mockContext);
|
|
31
|
+
};
|
|
32
|
+
c.returnLoose = c.return;
|
|
32
33
|
|
|
33
34
|
c.whenCalled = function(callback) {
|
|
34
35
|
mockContext.whenCalledEmitter.add(callback);
|
|
@@ -35,9 +35,10 @@ function expect(hasCorrectArgument,index,mockContext) {
|
|
|
35
35
|
return expectArray(index+1,mockContext,array);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
c.return = function(arg) {
|
|
39
|
-
return _return(arg,index+1,mockContext);
|
|
40
|
-
};
|
|
38
|
+
c.return = function(arg) {
|
|
39
|
+
return _return(arg,index+1,mockContext);
|
|
40
|
+
};
|
|
41
|
+
c.returnLoose = c.return;
|
|
41
42
|
|
|
42
43
|
c.whenCalled = function(callback) {
|
|
43
44
|
mockContext.whenCalledEmitter.add(callback);
|
|
@@ -57,10 +58,10 @@ function expect(hasCorrectArgument,index,mockContext) {
|
|
|
57
58
|
return _return(undefined,index+1,mockContext).repeatAny();
|
|
58
59
|
};
|
|
59
60
|
|
|
60
|
-
c.resolve = function(value) {
|
|
61
|
-
var promise = newThen.resolve(value);
|
|
62
|
-
return c.return(promise);
|
|
63
|
-
};
|
|
61
|
+
c.resolve = function(value) {
|
|
62
|
+
var promise = newThen.resolve(value);
|
|
63
|
+
return c.return(promise);
|
|
64
|
+
};
|
|
64
65
|
|
|
65
66
|
c.reject = function(value) {
|
|
66
67
|
var promise = newThen.reject(value);
|
|
@@ -10,9 +10,10 @@ function expectEmpty(mockContext) {
|
|
|
10
10
|
mockContext.compositeAreCorrectArguments.add(hasCorrectArgument);
|
|
11
11
|
mockContext.numberOfArgs = 0;
|
|
12
12
|
|
|
13
|
-
c.return = function(arg) {
|
|
14
|
-
return _return(arg,0,mockContext);
|
|
15
|
-
};
|
|
13
|
+
c.return = function(arg) {
|
|
14
|
+
return _return(arg,0,mockContext);
|
|
15
|
+
};
|
|
16
|
+
c.returnLoose = c.return;
|
|
16
17
|
|
|
17
18
|
c.whenCalled = function(callback) {
|
|
18
19
|
mockContext.whenCalledEmitter.add(callback);
|
|
@@ -45,4 +46,4 @@ function expectEmpty(mockContext) {
|
|
|
45
46
|
return c;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
module.exports = expectEmpty;
|
|
49
|
+
module.exports = expectEmpty;
|