a_mock 2.0.2 → 2.0.4

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.
Files changed (90) hide show
  1. package/.github/workflows/ci.yml +19 -0
  2. package/README.md +450 -1
  3. package/and/newBinaryAndSpec/test.js +1 -7
  4. package/and/newMonadicAndSpec/test.js +2 -5
  5. package/andSpec/test.js +1 -4
  6. package/andSpec/testIntegration.js +1 -2
  7. package/eventEmitter/addSpec/test.js +2 -4
  8. package/eventEmitter/emitterContextSpec/test.js +2 -3
  9. package/eventEmitter/removeSpec/test.js +2 -4
  10. package/eventEmitter/tryAddSpec/test.js +3 -6
  11. package/eventEmitter/tryRemoveSpec/test.js +2 -5
  12. package/eventEmitterSpec/test.js +1 -7
  13. package/expectRequireSpec/test.js +1 -5
  14. package/index.d.ts +134 -105
  15. package/mock/mockContextSpec/test.js +2 -5
  16. package/mock/mockFuncProperties.js +37 -37
  17. package/mock/mockFuncPropertiesSpec/test.js +2 -5
  18. package/mock/objectMock.js +59 -46
  19. package/mock/objectMockSpec/test.js +10 -11
  20. package/mock/propertyMockSpec/test.js +2 -4
  21. package/mock/throwUnexpectedArgumentsSpec/test.js +2 -3
  22. package/mock.js +8 -8
  23. package/mockSpec/test.js +1 -5
  24. package/mockSpec/testIntegration.js +1 -5
  25. package/newMutableAndSpec/test.js +1 -5
  26. package/newThenSpec/testExecute.js +1 -5
  27. package/newThenSpec/testNoCallback.js +1 -3
  28. package/newThenSpec/testPreReject.js +2 -2
  29. package/newThenSpec/testPreRejectAndThrow.js +2 -2
  30. package/newThenSpec/testPreResolve.js +1 -2
  31. package/newThenSpec/testPreResolveAndThrow.js +2 -2
  32. package/newThenSpec/testReject.js +2 -2
  33. package/newThenSpec/testRejectAndThrow.js +2 -2
  34. package/newThenSpec/testResolve.js +1 -2
  35. package/newThenSpec/testResolveAndThrow.js +2 -2
  36. package/newThenSpec/testResolveWithFailingPromise.js +1 -2
  37. package/newThenSpec/testResolveWithPromise.js +1 -2
  38. package/newTrueNTimesThenFalseSpec/test.js +1 -6
  39. package/newTrueOnceThenFalseSpec/test.js +1 -4
  40. package/package.json +5 -1
  41. package/partialMock/execute.js +10 -10
  42. package/partialMock/executeSpec/test.js +1 -4
  43. package/partialMock/expectAnything.js +7 -4
  44. package/partialMock/expectAnythingSpec/test.js +2 -8
  45. package/partialMock/expectArraySpec/test.js +2 -3
  46. package/partialMock/expectCore.js +11 -8
  47. package/partialMock/expectCoreSpec/test.js +2 -14
  48. package/partialMock/expectEmpty.js +5 -4
  49. package/partialMock/expectEmptySpec/test.js +2 -10
  50. package/partialMock/expectSpec/test.js +2 -3
  51. package/partialMock/hasSameArgument/isEqualArgSpec/test.js +7 -31
  52. package/partialMock/ignoreSpec/test.js +2 -3
  53. package/partialMock/mockContext/reset.js +15 -15
  54. package/partialMock/mockContext/resetSpec/test.js +4 -4
  55. package/partialMock/negotiateDecrementExpectCountSpec/test.js +1 -4
  56. package/partialMock/negotiateEndSpec/test.js +1 -6
  57. package/partialMock/negotiateIncrementExpectCountSpec/test.js +1 -4
  58. package/partialMock/newExecute.js +9 -9
  59. package/partialMock/newExecuteSpec/test.js +2 -6
  60. package/partialMock/newFallbackWrapper.js +29 -29
  61. package/partialMock/newFallbackWrapperSpec/test.js +2 -6
  62. package/partialMock/newFallbackWrapperSpec/testWithMockError.js +2 -4
  63. package/partialMock/newFallbackWrapperSpec/testWithOtherError.js +2 -5
  64. package/partialMock/newHasArgumentSpec/test.js +2 -5
  65. package/partialMock/newHasEqualArgumentArraySpec/test.js +2 -7
  66. package/partialMock/newHasNoMoreArgumentsSpec/test.js +2 -7
  67. package/partialMock/newHasSameArgumentSpec/test.js +2 -10
  68. package/partialMock/newMockContext.js +12 -12
  69. package/partialMock/newMockContextSpec/test.js +6 -6
  70. package/partialMock/newThrowSpec/test.js +2 -3
  71. package/partialMock/returnSpec/test.js +2 -5
  72. package/partialMock/setExecuteSpec/test.js +2 -3
  73. package/partialMock/simple/expectRequireSpec/test.js +2 -4
  74. package/partialMock/simple/newMock/throwUnexpectedArgumentsSpec/test.js +3 -4
  75. package/partialMock/simple/newMockSpec/test.js +2 -3
  76. package/partialMock/simple/newPartialMock.js +2 -0
  77. package/partialMock/simple/newPartialMockSpec/test.js +2 -8
  78. package/partialMock/simple/newPartialMockSpec/testRepeat.js +2 -5
  79. package/partialMock/simple/newPartialMockSpec/testWithMultiArgs.js +2 -4
  80. package/partialMock/simple/newPartialMockSpec/testWithSingleArg.js +2 -7
  81. package/partialMock/simple/newRequireMockSpec/test.js +2 -3
  82. package/partialMock/verifySpec/test.js +1 -4
  83. package/partialMock.js +12 -10
  84. package/partialMockSpec/test.js +3 -10
  85. package/partialMockSpec/testIntegration.js +1 -2
  86. package/readmeSpec/testREADME.js +199 -0
  87. package/requireMockSpec/test.js +1 -2
  88. package/strictMockSpec/test.js +1 -2
  89. package/util.js +7 -7
  90. package/test.js +0 -17
@@ -1,5 +1,5 @@
1
1
  var assert = require('assert');
2
- var test = require('../test')
2
+ var test = require('node:test')
3
3
  var newRequireMock = require('../partialMock/simple/newRequireMock');
4
4
  var newMock = require('../partialMock/simple/newMock');
5
5
 
@@ -13,13 +13,11 @@ var tryRemove = newRequireMock('./eventEmitter/tryRemove');
13
13
  var newSut = require('../eventEmitter');
14
14
 
15
15
  (function(){
16
- console.log('newEventEmitter');
17
16
  var context = {};
18
17
  newEmitterContext.expect().return(context);
19
18
  var sut = newSut();
20
19
 
21
20
  (function() {
22
- console.log('add');
23
21
  var callback = {};
24
22
  var didAdd;
25
23
  add.expect(context).expect(callback).whenCalled(onAdd).return();
@@ -40,7 +38,6 @@ var newSut = require('../eventEmitter');
40
38
  })();
41
39
 
42
40
  (function() {
43
- console.log('tryAdd');
44
41
  var callback = {};
45
42
  var didAdd;
46
43
  tryAdd.expect(context).expect(callback).whenCalled(onAdd).return();
@@ -61,7 +58,6 @@ var newSut = require('../eventEmitter');
61
58
  })();
62
59
 
63
60
  (function() {
64
- console.log('remove');
65
61
  var callback = {};
66
62
  var didRemove;
67
63
  remove.expect(context).expect(callback).whenCalled(onRemove).return();
@@ -82,7 +78,6 @@ var newSut = require('../eventEmitter');
82
78
  })();
83
79
 
84
80
  (function() {
85
- console.log('tryRemove');
86
81
  var callback = {};
87
82
  var didtryRemove;
88
83
  tryRemove.expect(context).expect(callback).whenCalled(ontryRemove).return();
@@ -103,7 +98,6 @@ var newSut = require('../eventEmitter');
103
98
  })();
104
99
 
105
100
  (function() {
106
- console.log('emit');
107
101
  var callback = {};
108
102
  var didemit;
109
103
  var callback = newMock();
@@ -1,5 +1,5 @@
1
1
  var sut = require('../expectRequire');
2
- var test = require('../test');
2
+ var test = require('node:test');
3
3
  var assert = require('assert');
4
4
 
5
5
  var fooFake = {};
@@ -7,10 +7,8 @@ var bazFake = {};
7
7
 
8
8
 
9
9
  (function(){
10
- console.log('expectRequire');
11
10
 
12
11
  (function() {
13
- console.log('require');
14
12
  sut('./foo').return(fooFake);
15
13
  sut('./baz').return(bazFake);
16
14
 
@@ -42,10 +40,8 @@ var bazFake = {};
42
40
  })();
43
41
 
44
42
  (function(){
45
- console.log('expectRequire reset');
46
43
 
47
44
  (function() {
48
- console.log('require');
49
45
  sut('./foo').return(fooFake);
50
46
  sut.reset();
51
47
  var returned = require('./foo');
package/index.d.ts CHANGED
@@ -1,109 +1,138 @@
1
- type AnyFunction = (...args: any[]) => any;
2
- type Tail<T extends any[]> = T extends [any, ...infer R] ? R : [];
3
- type FunctionProps<T> = T extends (...args: any[]) => any
4
- ? { [K in Exclude<keyof T, keyof Function>]: Mocked<T[K]> }
5
- : {};
6
- type MockedFunction<T extends AnyFunction> = MockFunction<
7
- Parameters<T>,
8
- ReturnType<T>
9
- > &
10
- T &
11
- FunctionProps<T>;
12
-
13
- interface RepeatControl {
14
- repeat(times: number): RepeatControl;
15
- repeatAny(): RepeatControl;
16
- }
17
-
18
- interface ExpectationTerminal<R, TArgs extends any[]> {
19
- return(value?: R): RepeatControl;
20
- whenCalled(callback: (...args: TArgs) => void): ExpectationTerminal<R, TArgs>;
21
- repeat(times: number): RepeatControl;
22
- repeatAny(): RepeatControl;
23
- throw(error: unknown): ExpectationTerminal<R, TArgs>;
24
- resolve(value: any): RepeatControl;
25
- reject(value: any): RepeatControl;
26
- }
27
-
28
- interface ExpectationChain<TArgs extends any[], R>
29
- extends ExpectationTerminal<R, TArgs> {
30
- expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
31
- expectAnything(): ExpectationChain<Tail<TArgs>, R>;
32
- ignore(): ExpectationChain<Tail<TArgs>, R>;
1
+ import type { AxiosInstance, AxiosResponse } from 'axios';
2
+
3
+ type AnyFunction = (...args: any[]) => any;
4
+ type Tail<T extends any[]> = T extends [any, ...infer R] ? R : [];
5
+ type OverloadedParameters<T> = T extends {
6
+ (...args: infer A): any;
7
+ (...args: infer B): any;
8
+ }
9
+ ? A | B
10
+ : T extends (...args: infer A) => any
11
+ ? A
12
+ : never;
13
+ type OverloadedReturnType<T> = T extends {
14
+ (...args: any[]): infer A;
15
+ (...args: any[]): infer B;
16
+ }
17
+ ? A | B
18
+ : T extends (...args: any[]) => infer A
19
+ ? A
20
+ : never;
21
+ type OptionalTuple<T extends any[]> = { [K in keyof T]?: T[K] };
22
+ type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
23
+ type CallbackFor<TArgs> = TArgs extends any[] ? (...args: TArgs) => void : never;
24
+ type FunctionProps<T> = T extends (...args: any[]) => any
25
+ ? { [K in Exclude<keyof T, keyof Function>]: Mocked<T[K]> }
26
+ : {};
27
+ type MockedFunction<T extends AnyFunction> = MockFunction<
28
+ OverloadedParameters<T>,
29
+ T extends AxiosInstance
30
+ ? Promise<AxiosResponse<any, any, {}>>
31
+ : OverloadedReturnType<T>
32
+ > &
33
+ T &
34
+ FunctionProps<T>;
35
+
36
+ interface RepeatControl {
37
+ repeat(times: number): RepeatControl;
38
+ repeatAny(): RepeatControl;
39
+ }
40
+
41
+ interface ExpectationTerminal<R, TArgs extends any[], TOrigArgs extends any[] = TArgs> {
42
+ return(value: R): RepeatControl;
43
+ returnLoose(value?: any): RepeatControl;
44
+ whenCalled(callback: CallbackFor<TOrigArgs>): ExpectationTerminal<R, TArgs, TOrigArgs>;
45
+ repeat(times: number): RepeatControl;
46
+ repeatAny(): RepeatControl;
47
+ throw(error: any): ExpectationTerminal<R, TArgs, TOrigArgs>;
48
+ resolve(value: UnwrapPromise<R>): RepeatControl;
49
+ resolveLoose(value?: any): RepeatControl;
50
+ reject(error: Error): RepeatControl;
51
+ }
52
+
53
+ interface ExpectationChain<TArgs extends any[], R, TOrigArgs extends any[] = TArgs>
54
+ extends ExpectationTerminal<R, TArgs, TOrigArgs> {
55
+ expect(arg?: TArgs[0]): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
56
+ /** @deprecated Use ignoreAll() instead. */
57
+ 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. */
33
62
  expectArray(
34
- value: TArgs[0] extends any[] ? TArgs[0] : any[]
35
- ): ExpectationChain<Tail<TArgs>, R>;
36
- }
37
-
38
- interface MockFunction<TArgs extends any[], R> {
39
- (...args: TArgs): R;
63
+ value?: TArgs[0] extends any[] ? TArgs[0] : any[]
64
+ ): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
65
+ }
66
+
67
+ interface MockFunction<TArgs extends any[], R> {
68
+ (...args: TArgs): R;
40
69
  // Allow any-args calls in addition to the original signature.
41
70
  (...args: any[]): any;
42
- expect(): ExpectationTerminal<R, TArgs>;
43
- expect(...args: TArgs): ExpectationTerminal<R, TArgs>;
44
- expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
45
- expectAnything(): ExpectationChain<Tail<TArgs>, R>;
46
- ignore(): ExpectationChain<Tail<TArgs>, R>;
71
+ expect(...args: OptionalTuple<TArgs>): ExpectationChain<Tail<TArgs>, R, TArgs>;
72
+ ignore(): ExpectationChain<Tail<TArgs>, R, TArgs>;
73
+ /** @deprecated Use expect() instead. */
47
74
  expectArray(
48
- value: TArgs[0] extends any[] ? TArgs[0] : any[]
49
- ): ExpectationChain<Tail<TArgs>, R>;
50
- // Allow any-args expectations in addition to typed expectations.
51
- expect(...args: any[]): ExpectationTerminal<any, any[]>;
52
- verify(): true;
53
- reset(): void;
54
- }
55
-
56
- type Mocked<T> = T extends AnyFunction
57
- ? MockedFunction<T>
58
- : T extends object
59
- ? { [K in keyof T]: Mocked<T[K]> } & { verify: () => true }
60
- : T;
61
-
62
- interface RequireExpectation {
63
- return(value: any): RequireExpectation;
64
- repeat(times: number): RequireExpectation;
65
- repeatAny(): RequireExpectation;
66
- whenCalled(callback: (...args: any[]) => void): RequireExpectation;
67
- }
68
-
69
- interface ThenableMock<T = any> {
70
- (valueToResolveWith?: T | null, errorToFailWith?: any): ThenableMock<T>;
71
- then<U = T>(
72
- success?: (value: T) => U | ThenableMock<U> | PromiseLike<U>,
73
- fail?: (error: any) => U | ThenableMock<U> | PromiseLike<U>
74
- ): ThenableMock<U>;
75
- resolve(value?: T): ThenableMock<T>;
76
- reject(error: any): ThenableMock<never>;
77
- }
78
-
79
- declare function mock<TArgs extends any[] = any[], R = any>(): MockFunction<
80
- TArgs,
81
- R
82
- >;
83
- declare function mock<T extends AnyFunction>(
84
- original: T
85
- ): MockedFunction<T>;
86
- declare function mock<T extends object>(subject: T): Mocked<T>;
87
-
88
- declare function expectRequire(moduleName: string): RequireExpectation;
89
- declare namespace expectRequire {
90
- function reset(): void;
91
- }
92
-
93
- declare function requireMock<TArgs extends any[] = any[], R = any>(
94
- moduleName: string
95
- ): MockFunction<TArgs, R>;
96
- declare namespace requireMock {
97
- function reset(): void;
98
- }
99
-
100
- declare function promise<T = any>(): ThenableMock<T>;
101
-
102
- declare const aMock: {
103
- mock: typeof mock;
104
- expectRequire: typeof expectRequire;
105
- requireMock: typeof requireMock;
106
- promise: typeof promise;
107
- };
108
-
109
- export = aMock;
75
+ value?: TArgs[0] extends any[] ? TArgs[0] : any[]
76
+ ): ExpectationChain<Tail<TArgs>, R, TArgs>;
77
+ /** @deprecated Use ignoreAll() instead. */
78
+ expectAnything(): ExpectationChain<any[], R, TArgs>;
79
+ ignoreAll(): ExpectationChain<any[], R, TArgs>;
80
+ // Explicit loose expectations.
81
+ expectLoose(...args: any[]): ExpectationChain<any[], any, TArgs>;
82
+ verify(): true;
83
+ reset(): void;
84
+ }
85
+
86
+ type Mocked<T> = T extends AnyFunction
87
+ ? MockedFunction<T>
88
+ : T extends object
89
+ ? { [K in keyof T]: Mocked<T[K]> } & { verify: () => true }
90
+ : T;
91
+
92
+ interface RequireExpectation {
93
+ return(value: any): RequireExpectation;
94
+ repeat(times: number): RequireExpectation;
95
+ repeatAny(): RequireExpectation;
96
+ whenCalled(callback: (...args: any[]) => void): RequireExpectation;
97
+ }
98
+
99
+ interface ThenableMock<T = any> {
100
+ (valueToResolveWith?: T | null, errorToFailWith?: any): ThenableMock<T>;
101
+ then<U = T>(
102
+ success?: (value: T) => U | ThenableMock<U> | PromiseLike<U>,
103
+ fail?: (error: any) => U | ThenableMock<U> | PromiseLike<U>
104
+ ): ThenableMock<U>;
105
+ resolve(value?: T): ThenableMock<T>;
106
+ reject(error: any): ThenableMock<never>;
107
+ }
108
+
109
+ declare function mock<T = any[], R = any>(): T extends any[]
110
+ ? MockFunction<T, R>
111
+ : Mocked<T>;
112
+ declare function mock<T extends AnyFunction>(
113
+ original: T
114
+ ): MockedFunction<T>;
115
+ declare function mock<T extends object>(subject: T): Mocked<T>;
116
+
117
+ declare function expectRequire(moduleName: string): RequireExpectation;
118
+ declare namespace expectRequire {
119
+ function reset(): void;
120
+ }
121
+
122
+ declare function requireMock<TArgs extends any[] = any[], R = any>(
123
+ moduleName: string
124
+ ): MockFunction<TArgs, R>;
125
+ declare namespace requireMock {
126
+ function reset(): void;
127
+ }
128
+
129
+ declare function promise<T = any>(): ThenableMock<T>;
130
+
131
+ declare const aMock: {
132
+ mock: typeof mock;
133
+ expectRequire: typeof expectRequire;
134
+ requireMock: typeof requireMock;
135
+ promise: typeof promise;
136
+ };
137
+
138
+ export = aMock;
@@ -1,14 +1,12 @@
1
1
  var assert = require('assert');
2
- var test = require('../../test');
2
+ var test = require('node:test');
3
3
  var newRequireMock = require('../../partialMock/simple/newRequireMock');
4
4
 
5
5
  (function(){
6
- console.log('mockContext');
7
6
  var newVerify = newRequireMock('../newMutableAnd');
8
7
  var sut = require('../mockContext');
9
8
 
10
9
  (function() {
11
- console.log('empty input.new');
12
10
  var verify = {};
13
11
  newVerify.expect().return(verify);
14
12
  var returned = sut();
@@ -19,7 +17,6 @@ var newRequireMock = require('../../partialMock/simple/newRequireMock');
19
17
  })();
20
18
 
21
19
  (function() {
22
- console.log('nonEmpty input.new');
23
20
  var input = {};
24
21
  var returned = sut(input);
25
22
 
@@ -27,4 +24,4 @@ var newRequireMock = require('../../partialMock/simple/newRequireMock');
27
24
  assert.equal(returned,input);
28
25
  });
29
26
  })();
30
- })();
27
+ })();
@@ -1,39 +1,39 @@
1
- var propertyMock = require('./propertyMock');
2
-
3
- function collectPropertyNames(subject) {
4
- var names = {};
5
- for (var name in subject) {
6
- names[name] = true;
7
- }
8
- var proto = Object.getPrototypeOf(subject);
9
- while (proto && proto !== Object.prototype && proto !== Function.prototype) {
10
- var props = Object.getOwnPropertyNames(proto);
11
- for (var i = 0; i < props.length; i++) {
12
- var prop = props[i];
13
- if (prop === 'constructor' || names[prop])
14
- continue;
15
- var desc = Object.getOwnPropertyDescriptor(proto, prop);
16
- if (desc && typeof desc.value === 'function')
17
- names[prop] = true;
18
- }
19
- proto = Object.getPrototypeOf(proto);
20
- }
21
- return Object.keys(names);
22
- }
23
-
24
- function execute(subject,mockObject) {
25
- if (!(subject instanceof Object))
26
- return;
27
- var mockFuncProperties = require('./mockFuncProperties');
28
- var propertyNames = collectPropertyNames(subject);
29
- for(var i = 0; i < propertyNames.length; i++) {
30
- var propertyName = propertyNames[i];
31
- var property = subject[propertyName];
32
- var mockProperty = mockObject[propertyName];
33
- if (property instanceof Function)
34
- subject[propertyName] = propertyMock(subject,propertyName,mockProperty);
35
- mockFuncProperties(property,mockProperty);
36
- }
37
- }
1
+ var propertyMock = require('./propertyMock');
2
+
3
+ function collectPropertyNames(subject) {
4
+ var names = {};
5
+ for (var name in subject) {
6
+ names[name] = true;
7
+ }
8
+ var proto = Object.getPrototypeOf(subject);
9
+ while (proto && proto !== Object.prototype && proto !== Function.prototype) {
10
+ var props = Object.getOwnPropertyNames(proto);
11
+ for (var i = 0; i < props.length; i++) {
12
+ var prop = props[i];
13
+ if (prop === 'constructor' || names[prop])
14
+ continue;
15
+ var desc = Object.getOwnPropertyDescriptor(proto, prop);
16
+ if (desc && typeof desc.value === 'function')
17
+ names[prop] = true;
18
+ }
19
+ proto = Object.getPrototypeOf(proto);
20
+ }
21
+ return Object.keys(names);
22
+ }
23
+
24
+ function execute(subject,mockObject) {
25
+ if (!(subject instanceof Object))
26
+ return;
27
+ var mockFuncProperties = require('./mockFuncProperties');
28
+ var propertyNames = collectPropertyNames(subject);
29
+ for(var i = 0; i < propertyNames.length; i++) {
30
+ var propertyName = propertyNames[i];
31
+ var property = subject[propertyName];
32
+ var mockProperty = mockObject[propertyName];
33
+ if (property instanceof Function)
34
+ subject[propertyName] = propertyMock(subject,propertyName,mockProperty);
35
+ mockFuncProperties(property,mockProperty);
36
+ }
37
+ }
38
38
 
39
39
  module.exports = execute;
@@ -1,16 +1,14 @@
1
1
  var assert = require('assert');
2
- var test = require('../../test');
2
+ var test = require('node:test');
3
3
  var newRequireMock = require('../../partialMock/simple/newRequireMock');
4
4
  var newMock = require('../../partialMock/simple/newMock');
5
5
 
6
6
  (function(){
7
- console.log('mockFuncProperties');
8
7
  var newPropertyMock = newRequireMock('./propertyMock');
9
8
  var mockFuncProperties = newRequireMock('./mockFuncProperties');
10
9
  var sut = require('../mockFuncProperties');
11
10
 
12
11
  (function() {
13
- console.log('subject is non-primitive');
14
12
  var subject = {};
15
13
  var a2 = {};
16
14
  var a = function() {};
@@ -54,7 +52,6 @@ var newMock = require('../../partialMock/simple/newMock');
54
52
  })();
55
53
 
56
54
  (function() {
57
- console.log('subject is primitive');
58
55
  var subject = 'a';
59
56
  var mockObject = 'a';
60
57
 
@@ -64,4 +61,4 @@ var newMock = require('../../partialMock/simple/newMock');
64
61
  assert(true);
65
62
  });
66
63
  })();
67
- })();
64
+ })();
@@ -1,48 +1,61 @@
1
- var newMockContext = require('./mockContext');
2
- var newPartialMock = require('../partialMock');
3
-
4
- function collectPropertyNames(subject) {
5
- var names = {};
6
- for (var name in subject) {
7
- names[name] = true;
8
- }
9
- var proto = Object.getPrototypeOf(subject);
10
- while (proto && proto !== Object.prototype && proto !== Function.prototype) {
11
- var props = Object.getOwnPropertyNames(proto);
12
- for (var i = 0; i < props.length; i++) {
13
- var prop = props[i];
14
- if (prop === 'constructor' || names[prop])
15
- continue;
16
- var desc = Object.getOwnPropertyDescriptor(proto, prop);
17
- if (desc && typeof desc.value === 'function')
18
- names[prop] = true;
19
- }
20
- proto = Object.getPrototypeOf(proto);
21
- }
22
- return Object.keys(names);
23
- }
24
-
25
- function _new(subject,mockContext,parent) {
26
- var newObjectMock = require('./objectMock');
27
-
28
- var mockContext = newMockContext(mockContext);
29
- var mock = {};
30
- mock.verify = mockContext.verify;
31
- if (subject instanceof Function) {
32
- mock = newPartialMock(subject, parent);
33
- mockContext.verify.add(mock.verify);
34
- }
35
- if (!(subject instanceof Object))
36
- return mock;
37
- var propertyNames = collectPropertyNames(subject);
38
- for(var i = 0; i < propertyNames.length; i++) {
39
- var propertyName = propertyNames[i];
40
- if (Object.prototype.hasOwnProperty.call(mock, propertyName))
41
- continue;
42
- var property = subject[propertyName];
43
- mock[propertyName] = newObjectMock(property,mockContext,subject);
44
- }
45
- return mock;
46
- }
1
+ var newMockContext = require('./mockContext');
2
+ var newPartialMock = require('../partialMock');
3
+
4
+ function collectPropertyNames(subject) {
5
+ var names = {};
6
+ for (var name in subject) {
7
+ names[name] = true;
8
+ }
9
+ var proto = Object.getPrototypeOf(subject);
10
+ while (proto && proto !== Object.prototype && proto !== Function.prototype) {
11
+ var props = Object.getOwnPropertyNames(proto);
12
+ for (var i = 0; i < props.length; i++) {
13
+ var prop = props[i];
14
+ if (prop === 'constructor' || names[prop])
15
+ continue;
16
+ var desc = Object.getOwnPropertyDescriptor(proto, prop);
17
+ if (desc && typeof desc.value === 'function')
18
+ names[prop] = true;
19
+ }
20
+ proto = Object.getPrototypeOf(proto);
21
+ }
22
+ return Object.keys(names);
23
+ }
24
+
25
+ function _new(subject,mockContext,parent) {
26
+ var newObjectMock = require('./objectMock');
27
+
28
+ var mockContext = newMockContext(mockContext);
29
+ var mock = {};
30
+ if (subject && typeof subject === 'function' && typeof subject.expectAnything === 'function' && typeof subject.expect === 'function')
31
+ return subject;
32
+ var mockedMap = mockContext._mockedMap;
33
+ if (!mockedMap) {
34
+ mockedMap = new WeakMap();
35
+ mockContext._mockedMap = mockedMap;
36
+ }
37
+ if (subject && (typeof subject === 'object' || typeof subject === 'function')) {
38
+ var existingMock = mockedMap.get(subject);
39
+ if (existingMock)
40
+ return existingMock;
41
+ }
42
+ mock.verify = mockContext.verify;
43
+ if (subject instanceof Function) {
44
+ mock = newPartialMock(subject, parent);
45
+ mockContext.verify.add(mock.verify);
46
+ }
47
+ if (!(subject instanceof Object))
48
+ return mock;
49
+ mockedMap.set(subject, mock);
50
+ var propertyNames = collectPropertyNames(subject);
51
+ for(var i = 0; i < propertyNames.length; i++) {
52
+ var propertyName = propertyNames[i];
53
+ if (Object.prototype.hasOwnProperty.call(mock, propertyName))
54
+ continue;
55
+ var property = subject[propertyName];
56
+ mock[propertyName] = newObjectMock(property,mockContext,subject);
57
+ }
58
+ return mock;
59
+ }
47
60
 
48
61
  module.exports = _new
@@ -1,17 +1,15 @@
1
1
  var assert = require('assert');
2
- var test = require('../../test');
2
+ var test = require('node:test');
3
3
  var newRequireMock = require('../../partialMock/simple/newRequireMock');
4
4
  var newMock = require('../../partialMock/simple/newMock');
5
5
 
6
6
  (function(){
7
- console.log('objectMock');
8
7
  var newMockContext = newRequireMock('./mockContext');
9
8
  var newPartialMock = newRequireMock('../partialMock');
10
9
  var newSut = require('../objectMock');
11
10
 
12
11
 
13
12
  (function() {
14
- console.log('subject is object.new');
15
13
  var newObjectMock = newRequireMock('./objectMock');
16
14
  var initialMockContext = {}
17
15
  var mockContext = {};
@@ -26,9 +24,11 @@ var newMock = require('../../partialMock/simple/newMock');
26
24
  var bMock = {};
27
25
 
28
26
  newMockContext.expect(initialMockContext).return(mockContext);
29
- newObjectMock.expect(a).expect(mockContext).return(aMock);
30
- newObjectMock.expect(b).expect(mockContext).return(bMock);
27
+ // parent is optional metadata; behavior should not require callers to pass it.
28
+ newObjectMock.expect(a).expect(mockContext).expect(subject).return(aMock);
29
+ newObjectMock.expect(b).expect(mockContext).expect(subject).return(bMock);
31
30
 
31
+ //todo
32
32
  var returned = newSut(subject,initialMockContext);
33
33
 
34
34
  test('it should not return subject', function(){
@@ -49,7 +49,6 @@ var newMock = require('../../partialMock/simple/newMock');
49
49
  })();
50
50
 
51
51
  (function() {
52
- console.log('subject is func.new');
53
52
  var newObjectMock = newRequireMock('./objectMock');
54
53
  var initialMockContext = {}
55
54
  var mockContext = {};
@@ -69,9 +68,10 @@ var newMock = require('../../partialMock/simple/newMock');
69
68
  var didAddVerifyToComposite;
70
69
 
71
70
  newMockContext.expect(initialMockContext).return(mockContext);
72
- newObjectMock.expect(a).expect(mockContext).return(aMock);
73
- newObjectMock.expect(b).expect(mockContext).return(bMock);
74
- newPartialMock.expect(subject).return(partialMock);
71
+ // parent is optional metadata; behavior should not require callers to pass it.
72
+ newObjectMock.expect(a).expect(mockContext).expect(subject).return(aMock);
73
+ newObjectMock.expect(b).expect(mockContext).expect(subject).return(bMock);
74
+ newPartialMock.expect(subject).expectAnything().return(partialMock);
75
75
  compositeVerify.add.expect(verify).whenCalled(onAddVerify).return(null);
76
76
 
77
77
  function onAddVerify() {
@@ -99,7 +99,6 @@ var newMock = require('../../partialMock/simple/newMock');
99
99
  })();
100
100
 
101
101
  (function() {
102
- console.log('subject is primitive');
103
102
  var newObjectMock = newRequireMock('./objectMock');
104
103
  var initialMockContext = {}
105
104
  var mockContext = {};
@@ -120,4 +119,4 @@ var newMock = require('../../partialMock/simple/newMock');
120
119
  assert.equal(returned.verify,verify);
121
120
  });
122
121
  })();
123
- })();
122
+ })();
@@ -1,9 +1,8 @@
1
1
  var assert = require('assert');
2
- var test = require('../../test');
2
+ var test = require('node:test');
3
3
  var newMock = require('../../partialMock/simple/newMock');
4
4
 
5
5
  (function(){
6
- console.log('propertyMock.new');
7
6
  newSut = require('../propertyMock');
8
7
 
9
8
  var subject = {};
@@ -22,7 +21,6 @@ var newMock = require('../../partialMock/simple/newMock');
22
21
  });
23
22
 
24
23
  (function() {
25
- console.log('execute mocked property');
26
24
  var mockedValue = {};
27
25
  var arg = {};
28
26
  mock.expect(arg).return(mockedValue);
@@ -33,4 +31,4 @@ var newMock = require('../../partialMock/simple/newMock');
33
31
  });
34
32
  })();
35
33
 
36
- })();
34
+ })();