1
1
# Mock
2
2
3
- [ ![ version] ( https://img.shields.io/badge/release-v0.9.5-success )] ( https://github.com/udibo/mock/tree/v0.9.5 )
4
- [ ![ deno doc
] ( https://img.shields.io/badge/deno-doc-success?logo=deno )] ( https://doc.deno.land/https/deno.land/x/[email protected] /mod.ts )
5
- [ ![ deno version] ( https://img.shields.io/badge/deno-v1.9.0-success?logo=deno )] ( https://github.com/denoland/deno/tree/v1.9.0 )
3
+ [ ![ release] ( https://img.shields.io/badge/release-v0.10.0-success )] ( https://github.com/udibo/mock/releases/tag/v0.10.0 )
4
+ [ ![ deno doc
] ( https://doc.deno.land/badge.svg )] ( https://doc.deno.land/https/deno.land/x/[email protected] /mod.ts )
6
5
[ ![ CI] ( https://github.com/udibo/mock/workflows/CI/badge.svg )] ( https://github.com/udibo/mock/actions?query=workflow%3ACI )
6
+ [ ![ codecov] ( https://codecov.io/gh/udibo/mock/branch/master/graph/badge.svg?token=TXORMSEHM7 )] ( https://codecov.io/gh/udibo/mock )
7
7
[ ![ license] ( https://img.shields.io/github/license/udibo/mock )] ( https://github.com/udibo/mock/blob/master/LICENSE )
8
8
9
- This module provides utilities to help mock behavior and spy on function calls
10
- for tests.
9
+ Utilities to help mock behavior, spy on function calls, stub methods and fake
10
+ time for tests.
11
11
12
12
## Features
13
13
@@ -30,22 +30,22 @@ imported directly from GitHub using raw content URLs.
30
30
31
31
``` ts
32
32
// Import from Deno's third party module registry
33
- import { spy , Spy } from " https://deno.land/x/mock@v0.9.5 /mod.ts" ;
33
+ import { spy , Spy } from " https://deno.land/x/mock@v0.10.0 /mod.ts" ;
34
34
// Import from GitHub
35
- import { spy , Spy } " https://raw.githubusercontent.com/udibo/mock/v0.9.5 /mod.ts" ;
35
+ import { spy , Spy } " https://raw.githubusercontent.com/udibo/mock/v0.10.0 /mod.ts" ;
36
36
` ` `
37
37
38
38
If you do not need all of the sub-modules, you can choose to just import the
39
39
sub-modules you need.
40
40
41
41
` ` ` ts
42
42
// Import from Deno's third party module registry
43
- import { Spy , spy } from " https://deno.land/x/mock@v0.9.5 /spy.ts" ;
43
+ import { Spy , spy } from " https://deno.land/x/mock@v0.10.0 /spy.ts" ;
44
44
// Import from GitHub
45
45
import {
46
46
Spy ,
47
47
spy ,
48
- } from " https://raw.githubusercontent.com/udibo/mock/v0.9.5 /spy.ts" ;
48
+ } from " https://raw.githubusercontent.com/udibo/mock/v0.10.0 /spy.ts" ;
49
49
```
50
50
51
51
#### Sub-modules
@@ -69,15 +69,15 @@ If a Node.js package has the type "module" specified in its package.json file,
69
69
the JavaScript bundle can be imported as a ` .js ` file.
70
70
71
71
``` js
72
- import { Spy , spy } from " ./mock_v0.9.5 .js" ;
72
+ import { Spy , spy } from " ./mock_v0.10.0 .js" ;
73
73
```
74
74
75
75
The default type for Node.js packages is "commonjs". To import the bundle into a
76
76
commonjs package, the file extension of the JavaScript bundle must be changed
77
77
from ` .js ` to ` .mjs ` .
78
78
79
79
``` js
80
- import { Spy , spy } from " ./mock_v0.9.5 .mjs" ;
80
+ import { Spy , spy } from " ./mock_v0.10.0 .mjs" ;
81
81
```
82
82
83
83
See [ Node.js Documentation] ( https://nodejs.org/api/esm.html ) for more
@@ -96,15 +96,15 @@ modules must have the type attribute set to "module".
96
96
97
97
``` js
98
98
// main.js
99
- import { Spy , spy } from " ./mock_v0.9.5 .js" ;
99
+ import { Spy , spy } from " ./mock_v0.10.0 .js" ;
100
100
```
101
101
102
102
You can also embed a module script directly into an HTML file by placing the
103
103
JavaScript code within the body of the script tag.
104
104
105
105
``` html
106
106
<script type =" module" >
107
- import { spy , Spy } from " ./mock_v0.9.5 .js" ;
107
+ import { spy , Spy } from " ./mock_v0.10.0 .js" ;
108
108
</script >
109
109
```
110
110
@@ -120,7 +120,7 @@ a try block then restore the function in a finally block to ensure the original
120
120
instance method is restored before continuing to other tests. The same applies
121
121
when using fake time.
122
122
123
- See [ deno docs] ( https://doc.deno.land/https/deno.land/x/mock@v0.9.5 /mod.ts ) for
123
+ See [ deno docs] ( https://doc.deno.land/https/deno.land/x/mock@v0.10.0 /mod.ts ) for
124
124
more information.
125
125
126
126
### Spy
@@ -135,8 +135,9 @@ anything, you can create an empty spy. An empty spy will just return undefined
135
135
for any calls made to it.
136
136
137
137
``` ts
138
- import {
assertEquals }
from " https://deno.land/[email protected] /testing/asserts.ts" ;
139
- import {
Spy ,
spy }
from " https://deno.land/x/[email protected] /spy.ts" ;
138
+ import {
assertEquals }
from " https://deno.land/[email protected] /testing/asserts.ts" ;
139
+ import {
assertSpyCall }
from " https://deno.land/x/[email protected] /asserts.ts" ;
140
+ import {
Spy ,
spy }
from " https://deno.land/x/[email protected] /spy.ts" ;
140
141
141
142
function add(
142
143
a : number ,
@@ -152,20 +153,18 @@ Deno.test("calls fake callback", () => {
152
153
const callback: Spy <void > = spy ();
153
154
154
155
assertEquals (add (2 , 3 , callback ), undefined );
156
+ assertSpyCall (callback , 1 , { args: [undefined , 5 ] });
155
157
assertEquals (add (5 , 4 , callback ), undefined );
156
- assertEquals (callback .calls , [
157
- { args: [undefined , 5 ] },
158
- { args: [undefined , 9 ] },
159
- ]);
158
+ assertSpyCall (callback , 1 , { args: [undefined , 9 ] });
160
159
});
161
160
```
162
161
163
162
If you have a function that takes a callback that needs to still behave
164
163
normally, you can wrap it with a spy.
165
164
166
165
``` ts
167
- import { assertEquals } from " https://deno.land/std@0.93 .0/testing/asserts.ts" ;
168
- import { Spy , spy } from " https://deno.land/x/mock@v0.9.5 /spy.ts" ;
166
+ import { assertEquals } from " https://deno.land/std@0.98 .0/testing/asserts.ts" ;
167
+ import { Spy , spy } from " https://deno.land/x/mock@v0.10.0 /spy.ts" ;
169
168
170
169
function filter<T >(values : T [], callback : (value : T ) => boolean ): any [] {
171
170
return values .filter (callback );
@@ -196,8 +195,8 @@ method. If it is not restored and you attempt to wrap it again, it will throw a
196
195
spy error saying "already spying on function".
197
196
198
197
``` ts
199
- import { assertEquals } from " https://deno.land/std@0.93 .0/testing/asserts.ts" ;
200
- import { Spy , spy } from " https://deno.land/x/mock@v0.9.5 /spy.ts" ;
198
+ import { assertEquals } from " https://deno.land/std@0.98 .0/testing/asserts.ts" ;
199
+ import { Spy , spy } from " https://deno.land/x/mock@v0.10.0 /spy.ts" ;
201
200
202
201
class Database {
203
202
private queries: any ;
@@ -287,8 +286,8 @@ return values after initialization by replacing or adding to the `stub.returns`
287
286
queue. When the returns queue is empty, it will return undefined.
288
287
289
288
``` ts
290
- import { assertEquals } from " https://deno.land/std@0.93 .0/testing/asserts.ts" ;
291
- import { Stub , stub } from " https://deno.land/x/mock@v0.9.5 /stub.ts" ;
289
+ import { assertEquals } from " https://deno.land/std@0.98 .0/testing/asserts.ts" ;
290
+ import { Stub , stub } from " https://deno.land/x/mock@v0.10.0 /stub.ts" ;
292
291
293
292
class Cat {
294
293
action(name : string ): any {
@@ -324,8 +323,8 @@ them returned. You can add more return values after initialization by replacing
324
323
or adding to the ` stub.returns ` queue.
325
324
326
325
``` ts
327
- import { assertEquals } from " https://deno.land/std@0.93 .0/testing/asserts.ts" ;
328
- import { Stub , stub } from " https://deno.land/x/mock@v0.9.5 /stub.ts" ;
326
+ import { assertEquals } from " https://deno.land/std@0.98 .0/testing/asserts.ts" ;
327
+ import { Stub , stub } from " https://deno.land/x/mock@v0.10.0 /stub.ts" ;
329
328
330
329
class Database {
331
330
query(query : string , params : any []): any [][] {
@@ -400,8 +399,8 @@ initialization by replacing or adding to the `stub.returns` queue. When the
400
399
returns queue is empty, it will call the replacement function.
401
400
402
401
``` ts
403
- import { assertEquals } from " https://deno.land/std@0.93 .0/testing/asserts.ts" ;
404
- import { Stub , stub } from " https://deno.land/x/mock@v0.9.5 /stub.ts" ;
402
+ import { assertEquals } from " https://deno.land/std@0.98 .0/testing/asserts.ts" ;
403
+ import { Stub , stub } from " https://deno.land/x/mock@v0.10.0 /stub.ts" ;
405
404
406
405
class Database {
407
406
query(query : string , params : any []): any [][] {
@@ -476,9 +475,9 @@ Overrides the real Date object and timer functions with fake ones that can be
476
475
controlled through the fake time instance.
477
476
478
477
``` ts
479
- import { assertEquals } from " https://deno.land/std@0.93 .0/testing/asserts.ts" ;
480
- import { Spy , spy } from " https://deno.land/x/mock@v0.9.5 /spy.ts" ;
481
- import { FakeTime } from " https://deno.land/x/mock@v0.9.5 /time.ts" ;
478
+ import { assertEquals } from " https://deno.land/std@0.98 .0/testing/asserts.ts" ;
479
+ import { Spy , spy } from " https://deno.land/x/mock@v0.10.0 /spy.ts" ;
480
+ import { FakeTime } from " https://deno.land/x/mock@v0.10.0 /time.ts" ;
482
481
483
482
function secondInterval(cb : () => void ): void {
484
483
setInterval (cb , 1000 );
0 commit comments