a_mock 2.0.1 → 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.
Files changed (89) hide show
  1. package/.github/workflows/ci.yml +19 -0
  2. package/README.md +615 -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 +136 -94
  15. package/mock/mockContextSpec/test.js +2 -5
  16. package/mock/mockFuncProperties.js +24 -1
  17. package/mock/mockFuncPropertiesSpec/test.js +2 -5
  18. package/mock/objectMock.js +42 -4
  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 +0 -2
  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 +3 -2
  42. package/partialMock/executeSpec/test.js +1 -4
  43. package/partialMock/expectAnything.js +3 -1
  44. package/partialMock/expectAnythingSpec/test.js +2 -8
  45. package/partialMock/expectArraySpec/test.js +2 -3
  46. package/partialMock/expectCore.js +3 -1
  47. package/partialMock/expectCoreSpec/test.js +2 -14
  48. package/partialMock/expectEmptySpec/test.js +2 -10
  49. package/partialMock/expectSpec/test.js +2 -3
  50. package/partialMock/hasSameArgument/isEqualArgSpec/test.js +7 -31
  51. package/partialMock/ignoreSpec/test.js +2 -3
  52. package/partialMock/mockContext/reset.js +4 -3
  53. package/partialMock/mockContext/resetSpec/test.js +4 -4
  54. package/partialMock/negotiateDecrementExpectCountSpec/test.js +1 -4
  55. package/partialMock/negotiateEndSpec/test.js +1 -6
  56. package/partialMock/negotiateIncrementExpectCountSpec/test.js +1 -4
  57. package/partialMock/newExecute.js +2 -2
  58. package/partialMock/newExecuteSpec/test.js +2 -6
  59. package/partialMock/newFallbackWrapper.js +9 -5
  60. package/partialMock/newFallbackWrapperSpec/test.js +2 -6
  61. package/partialMock/newFallbackWrapperSpec/testWithMockError.js +2 -4
  62. package/partialMock/newFallbackWrapperSpec/testWithOtherError.js +2 -5
  63. package/partialMock/newHasArgumentSpec/test.js +2 -5
  64. package/partialMock/newHasEqualArgumentArraySpec/test.js +2 -7
  65. package/partialMock/newHasNoMoreArgumentsSpec/test.js +2 -7
  66. package/partialMock/newHasSameArgumentSpec/test.js +2 -10
  67. package/partialMock/newMockContext.js +4 -4
  68. package/partialMock/newMockContextSpec/test.js +6 -6
  69. package/partialMock/newThrowSpec/test.js +2 -3
  70. package/partialMock/returnSpec/test.js +2 -5
  71. package/partialMock/setExecuteSpec/test.js +2 -3
  72. package/partialMock/simple/expectRequireSpec/test.js +2 -4
  73. package/partialMock/simple/newMock/throwUnexpectedArgumentsSpec/test.js +3 -4
  74. package/partialMock/simple/newMockSpec/test.js +2 -3
  75. package/partialMock/simple/newPartialMock.js +2 -0
  76. package/partialMock/simple/newPartialMockSpec/test.js +2 -8
  77. package/partialMock/simple/newPartialMockSpec/testRepeat.js +2 -5
  78. package/partialMock/simple/newPartialMockSpec/testWithMultiArgs.js +2 -4
  79. package/partialMock/simple/newPartialMockSpec/testWithSingleArg.js +2 -7
  80. package/partialMock/simple/newRequireMockSpec/test.js +2 -3
  81. package/partialMock/verifySpec/test.js +1 -4
  82. package/partialMock.js +5 -3
  83. package/partialMockSpec/test.js +3 -10
  84. package/partialMockSpec/testIntegration.js +1 -2
  85. package/readmeSpec/testREADME.js +199 -0
  86. package/requireMockSpec/test.js +1 -2
  87. package/strictMockSpec/test.js +1 -2
  88. package/util.js +7 -7
  89. package/test.js +0 -17
package/partialMock.js CHANGED
@@ -1,4 +1,4 @@
1
- function create(originalFunc) {
1
+ function create(originalFunc, thisArg) {
2
2
  var newMockContext = require('./partialMock/newMockContext');
3
3
  var expect = require('./partialMock/expect');
4
4
  var expectAnything = require('./partialMock/expectAnything');
@@ -7,13 +7,13 @@ function create(originalFunc) {
7
7
  var verify = require('./partialMock/verify');
8
8
  var expectEmpty = require('./partialMock/expectEmpty');
9
9
  var newEmptyAnd = require('./newMutableAnd');
10
- var mockContext = newMockContext(originalFunc);
10
+ var mockContext = newMockContext(originalFunc, thisArg);
11
11
  var _negotiateEnd = require('./partialMock/negotiateEnd');
12
12
 
13
13
  function mock() {
14
14
  negotiateEnd();
15
15
  mockContext.arguments = arguments;
16
- return mockContext.execute.apply(null,arguments);
16
+ return mockContext.execute.apply(this,arguments);
17
17
  }
18
18
 
19
19
  mock.expect = function() {
@@ -35,6 +35,8 @@ function create(originalFunc) {
35
35
  return expectAnything.apply(null,args);
36
36
  };
37
37
 
38
+ mock.ignoreAll = mock.expectAnything;
39
+
38
40
  mock.ignore = function() {
39
41
  negotiateEnd();
40
42
  mockContext.compositeAreCorrectArguments = newEmptyAnd();
@@ -1,4 +1,4 @@
1
- var test = require('../test');
1
+ var test = require('node:test');
2
2
  var assert = require('assert');
3
3
  var newMock = require('../partialMock/simple/newMock');
4
4
  var newRequireMock = require('../partialMock/simple/newRequireMock');
@@ -18,7 +18,6 @@ var mutableAnd = {};
18
18
  function fallback() {}
19
19
 
20
20
  (function () {
21
- console.log('partialMock');
22
21
  var sut;
23
22
  var fallbackMock;
24
23
  var mockContext;
@@ -28,7 +27,8 @@ function fallback() {}
28
27
  function stubMockContext() {
29
28
  mockContext = {};
30
29
  mockContext.reset = {};
31
- newMockContext.expect(fallback).return(mockContext);
30
+ // Allow optional thisArg without changing public usage.
31
+ newMockContext.expect(fallback).expectAnything().return(mockContext);
32
32
  }
33
33
 
34
34
  sut = newSut(fallback);
@@ -41,7 +41,6 @@ function fallback() {}
41
41
  });
42
42
 
43
43
  (function() {
44
- console.log('when execute ');
45
44
  var arg = {};
46
45
  var expected = {};
47
46
  var execute = newMock();
@@ -67,7 +66,6 @@ function fallback() {}
67
66
 
68
67
 
69
68
  (function() {
70
- console.log('when execute');
71
69
  var expected = {};
72
70
  mockContext.compositeAreCorrectArguments = null;
73
71
  var arg = 'arg';
@@ -98,7 +96,6 @@ function fallback() {}
98
96
  })();
99
97
 
100
98
  (function() {
101
- console.log('when expect empty');
102
99
  var expected = {};
103
100
  var didNegotiateEnd;
104
101
  mockContext.compositeAreCorrectArguments = null;
@@ -128,7 +125,6 @@ function fallback() {}
128
125
 
129
126
 
130
127
  (function() {
131
- console.log('when expectAnything');
132
128
  var expected = {};
133
129
  var didNegotiateEnd;
134
130
  mockContext.compositeAreCorrectArguments = null;
@@ -155,7 +151,6 @@ function fallback() {}
155
151
  })();
156
152
 
157
153
  (function() {
158
- console.log('when ignore');
159
154
  var expected = {};
160
155
  var didNegotiateEnd;
161
156
  mockContext.compositeAreCorrectArguments = null;
@@ -182,7 +177,6 @@ function fallback() {}
182
177
  })();
183
178
 
184
179
  (function() {
185
- console.log('when verify');
186
180
  var expected = {};
187
181
  var didNegotiateEnd;
188
182
  verify.expect(mockContext).return(expected);
@@ -203,7 +197,6 @@ function fallback() {}
203
197
  })();
204
198
 
205
199
  (function() {
206
- console.log('when expectArray');
207
200
  var expected = {};
208
201
  var didNegotiateEnd;
209
202
  mockContext.compositeAreCorrectArguments = null;
@@ -1,5 +1,5 @@
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
  var newSut = require('../partialMock');
5
5
 
@@ -11,7 +11,6 @@ function fallback(arg, arg2) {
11
11
  }
12
12
 
13
13
  (function() {
14
- console.log('partialMock');
15
14
 
16
15
  test('it should return expected given stubbed twice with repeat', function() {
17
16
  var arg = 'a';
@@ -0,0 +1,199 @@
1
+ const assert = require('assert');
2
+ const { describe, it } = require('node:test');
3
+ const aMock = require('..');
4
+
5
+ describe('README examples', () => {
6
+ it('partial mock returns fake then real', () => {
7
+ let original = function () {
8
+ return 'realValue';
9
+ };
10
+
11
+ const mock = aMock.mock(original);
12
+ original = mock;
13
+ mock.expect().return('fake');
14
+
15
+ assert.equal(original(), 'fake');
16
+ assert.equal(original(), 'realValue');
17
+ });
18
+
19
+ it('strict mock throws on unexpected call', () => {
20
+ const mock = aMock.mock();
21
+ mock.expect().return('fake');
22
+
23
+ assert.equal(mock(), 'fake');
24
+ assert.throws(() => mock(), /Unexpected arguments/);
25
+ });
26
+
27
+ it('expecting arguments', () => {
28
+ const mock = aMock.mock();
29
+ mock.expect('testValue1').return('fake1');
30
+ mock.expect('testValue2').return('fake2');
31
+
32
+ assert.equal(mock('testValue1'), 'fake1');
33
+ assert.equal(mock('testValue2'), 'fake2');
34
+ assert.throws(() => mock(), /Unexpected arguments/);
35
+ assert.throws(() => mock('foo'), /Unexpected arguments/);
36
+ });
37
+
38
+ it('expecting multiple arguments', () => {
39
+ const mock = aMock.mock();
40
+ mock.expect('firstArg1', 'secondArg1').return('fake1');
41
+ mock.expect('firstArg2', 'secondArg2').return('fake2');
42
+
43
+ assert.equal(mock('firstArg1', 'secondArg1'), 'fake1');
44
+ assert.equal(mock('firstArg2', 'secondArg2'), 'fake2');
45
+ assert.throws(() => mock('foo'), /Unexpected arguments/);
46
+ assert.throws(() => mock('foo', 'bar'), /Unexpected arguments/);
47
+ });
48
+
49
+ it('expecting array', () => {
50
+ const mock = aMock.mock();
51
+ mock.expect(['a', 'b']).return('fake1');
52
+ mock.expect(['a', 'b']).return('fake2');
53
+ mock.expect(['c', 'd']).return('fake3');
54
+
55
+ assert.equal(mock(['a', 'b']), 'fake1');
56
+ assert.equal(mock(['a', 'b']), 'fake2');
57
+ assert.equal(mock(['c', 'd']), 'fake3');
58
+ assert.throws(() => mock(['a', 'b']), /Unexpected arguments/);
59
+ assert.throws(() => mock(['foo', 'bar']), /Unexpected arguments/);
60
+ });
61
+
62
+ it('expecting struct', () => {
63
+ const mock = aMock.mock();
64
+ const obj = {};
65
+ mock.expect({ a: 1 }).return('fake1');
66
+ mock.expect({ a: 2 }).return('fake2');
67
+ mock.expect({ a: 2, b: { c: 'foo', d: ['me', 'too'] } }).return('fake3');
68
+ mock.expect(obj).return('fake4');
69
+ mock.expect({}).return('will never happen');
70
+
71
+ assert.equal(mock({ a: 1 }), 'fake1');
72
+ assert.equal(mock({ a: 2 }), 'fake2');
73
+ assert.equal(mock({ a: 2, b: { c: 'foo', d: ['me', 'too'] } }), 'fake3');
74
+ assert.equal(mock(obj), 'fake4');
75
+ assert.throws(() => mock({}), /Unexpected arguments/);
76
+ });
77
+
78
+ it('repeats', () => {
79
+ const mock = aMock.mock();
80
+ mock.expect().return('fake').repeat(2);
81
+
82
+ assert.equal(mock(), 'fake');
83
+ assert.equal(mock(), 'fake');
84
+ assert.throws(() => mock(), /Unexpected arguments/);
85
+ });
86
+
87
+ it('repeatAny', () => {
88
+ const mock = aMock.mock();
89
+ mock.expect().return('fake').repeatAny();
90
+
91
+ assert.equal(mock(), 'fake');
92
+ assert.equal(mock(), 'fake');
93
+ assert.equal(mock(), 'fake');
94
+ });
95
+
96
+ it('ignoring a single argument', () => {
97
+ const mock = aMock.mock();
98
+ mock.ignore().expect('foo').return('fake1');
99
+
100
+ assert.equal(mock('ignore me', 'foo'), 'fake1');
101
+ assert.throws(() => mock(), /Unexpected arguments/);
102
+ });
103
+
104
+ it('ignoring all arguments', () => {
105
+ const mock = aMock.mock();
106
+ mock.ignoreAll().return('fake1');
107
+
108
+ assert.equal(mock('someRandomValue', 'whatever'), 'fake1');
109
+ assert.throws(() => mock(), /Unexpected arguments/);
110
+ });
111
+
112
+ it('throwing exceptions', () => {
113
+ const mock = aMock.mock();
114
+ const error = new Error('invalid operation');
115
+ mock.expect().throw(error);
116
+ mock.expect().return('fake');
117
+
118
+ assert.throws(() => mock(), /invalid operation/);
119
+ assert.equal(mock(), 'fake');
120
+ });
121
+
122
+ it('intercepting whenCalled', () => {
123
+ const mock = aMock.mock();
124
+ let calledArg;
125
+ mock.expect('testValue').whenCalled(function (arg) {
126
+ calledArg = arg;
127
+ }).return('fake1');
128
+
129
+ assert.equal(mock('testValue'), 'fake1');
130
+ assert.equal(calledArg, 'testValue');
131
+ assert.throws(() => mock(), /Unexpected arguments/);
132
+ });
133
+
134
+ it('verify (fail)', () => {
135
+ const mock = aMock.mock();
136
+ mock.expect('testValue1').return('fake1');
137
+ mock.expect('testValue2').return('fake2');
138
+
139
+ assert.equal(mock('testValue1'), 'fake1');
140
+ assert.throws(() => mock.verify(), /mock has 1 pending functions/);
141
+ });
142
+
143
+ it('verify (success)', () => {
144
+ const mock = aMock.mock();
145
+ mock.expect('testValue1').return('fake1');
146
+ mock.expect('testValue2').return('fake2');
147
+
148
+ assert.equal(mock('testValue1'), 'fake1');
149
+ assert.equal(mock('testValue2'), 'fake2');
150
+ assert.equal(mock.verify(), true);
151
+ });
152
+
153
+ it('returning void (compact syntax)', () => {
154
+ const mock = aMock.mock();
155
+ mock.expect('testValue1');
156
+ mock.expect('testValue2').repeat(2);
157
+
158
+ assert.equal(mock('testValue1'), undefined);
159
+ assert.equal(mock('testValue2'), undefined);
160
+ assert.equal(mock('testValue2'), undefined);
161
+ assert.equal(mock.verify(), true);
162
+ });
163
+
164
+ it('reset mock', () => {
165
+ let original = function () {
166
+ return 'realValue';
167
+ };
168
+
169
+ const mock = aMock.mock(original);
170
+ original = mock;
171
+ mock.expect().return('fake');
172
+ mock.reset();
173
+
174
+ assert.equal(original(), 'realValue');
175
+ });
176
+
177
+ it('returning resolved promise', () => {
178
+ const mock = aMock.mock();
179
+ mock.expect('foo').resolve('fake');
180
+
181
+ return mock('foo').then(function (returned) {
182
+ assert.equal(returned, 'fake');
183
+ });
184
+ });
185
+
186
+ it('returning rejected promise', () => {
187
+ const mock = aMock.mock();
188
+ mock.expect('foo').reject('fake');
189
+
190
+ return mock('foo').then(
191
+ function () {
192
+ assert.fail('expected rejection');
193
+ },
194
+ function (returned) {
195
+ assert.equal(returned, 'fake');
196
+ }
197
+ );
198
+ });
199
+ });
@@ -1,5 +1,5 @@
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
  var expectRequire = require('../partialMock/simple/expectRequire');
5
5
 
@@ -11,7 +11,6 @@ expectRequire('./mock').return(newMockMock);
11
11
  expectRequire('./expectRequire').return(expectRequireMock);
12
12
 
13
13
  (function() {
14
- console.log('newRequireMock');
15
14
  var didExpectRequireReturnMock = false;
16
15
  var mock = {};
17
16
  var moduleName = {};
@@ -1,5 +1,5 @@
1
1
  var assert = require('assert');
2
- var test = require('../test');
2
+ var test = require('node:test');
3
3
  var expectRequire = require('../partialMock/simple/expectRequire');
4
4
  var newMockCore = require('../partialMock/simple/newPartialMock');
5
5
 
@@ -16,7 +16,6 @@ expectRequire('./partialMock').return(newPartialMock);
16
16
  expectRequire('./mock/throwUnexpectedArguments').return(throwUnexpectedArguments);
17
17
 
18
18
  (function(){
19
- console.log('newMock');
20
19
  var partialMock = {};
21
20
  newPartialMock.expect(throwUnexpectedArguments).return(partialMock);
22
21
  var returned = require('../strictMock')();
package/util.js CHANGED
@@ -1,7 +1,7 @@
1
- function isDate(x) {
2
- return Object.prototype.toString.call(x) === '[object Date]';
3
- }
4
-
5
- module.exports = {
6
- isDate: isDate
7
- };
1
+ function isDate(x) {
2
+ return Object.prototype.toString.call(x) === '[object Date]';
3
+ }
4
+
5
+ module.exports = {
6
+ isDate: isDate
7
+ };
package/test.js DELETED
@@ -1,17 +0,0 @@
1
- var red, green, reset;
2
- red = '\u001b[31m';
3
- green = '\u001b[32m';
4
- reset = '\u001b[0m';
5
-
6
- function test(message, fn) {
7
- try {
8
- fn();
9
- console.log(green + ' OK ' + reset + message);
10
- }
11
- catch(err) {
12
- console.log(red + ' FAIL ' + reset + message);
13
- console.log(err);
14
- }
15
- }
16
-
17
- module.exports = test;