@@ -10,6 +10,7 @@ class MockRegistry {
10
10
#authorization
11
11
#basic
12
12
#debug
13
+ #strict
13
14
14
15
constructor ( opts ) {
15
16
if ( ! opts . registry ) {
@@ -19,20 +20,33 @@ class MockRegistry {
19
20
this . #authorization = opts . authorization
20
21
this . #basic = opts . basic
21
22
this . #debug = opts . debug
23
+ this . #strict = opts . strict
22
24
// Required for this.package
23
25
this . #tap = opts . tap
26
+ if ( this . #tap) {
27
+ this . startNock ( )
28
+ }
24
29
}
25
30
26
- static tnock ( t , host , opts , { debug = false } = { } ) {
27
- if ( debug ) {
28
- Nock . emitter . on ( 'no match' , req => console . error ( 'NO MATCH' , req . options ) )
31
+ static tnock ( t , host , opts , { debug = false , strict } = { } ) {
32
+ const noMatch = ( req ) => {
33
+ if ( strict ) {
34
+ t . fail ( `Unmatched request: ${ JSON . stringify ( req . options , null , 2 ) } ` )
35
+ }
36
+ if ( debug ) {
37
+ console . error ( 'NO MATCH' , t . name , req . options )
38
+ }
29
39
}
40
+
41
+ Nock . emitter . on ( 'no match' , noMatch )
30
42
Nock . disableNetConnect ( )
31
43
const server = Nock ( host , opts )
32
44
t . teardown ( ( ) => {
33
45
Nock . enableNetConnect ( )
34
46
server . done ( )
47
+ Nock . emitter . off ( 'no match' , noMatch )
35
48
} )
49
+
36
50
return server
37
51
}
38
52
@@ -41,31 +55,38 @@ class MockRegistry {
41
55
}
42
56
43
57
get nock ( ) {
44
- if ( ! this . #nock) {
45
- if ( ! this . #tap) {
46
- throw new Error ( 'cannot mock packages without a tap fixture' )
47
- }
48
- const reqheaders = { }
49
- if ( this . #authorization) {
50
- reqheaders . authorization = `Bearer ${ this . #authorization} `
51
- }
52
- if ( this . #basic) {
53
- reqheaders . authorization = `Basic ${ this . #basic} `
54
- }
55
- this . #nock = MockRegistry . tnock (
56
- this . #tap,
57
- this . #registry,
58
- { reqheaders } ,
59
- { debug : this . #debug }
60
- )
61
- }
62
58
return this . #nock
63
59
}
64
60
65
61
set nock ( nock ) {
66
62
this . #nock = nock
67
63
}
68
64
65
+ startNock ( ) {
66
+ if ( this . nock ) {
67
+ return
68
+ }
69
+
70
+ if ( ! this . #tap) {
71
+ throw new Error ( 'cannot mock packages without a tap fixture' )
72
+ }
73
+
74
+ const reqheaders = { }
75
+ if ( this . #authorization) {
76
+ reqheaders . authorization = `Bearer ${ this . #authorization} `
77
+ }
78
+ if ( this . #basic) {
79
+ reqheaders . authorization = `Basic ${ this . #basic} `
80
+ }
81
+
82
+ this . nock = MockRegistry . tnock (
83
+ this . #tap,
84
+ this . #registry,
85
+ { reqheaders } ,
86
+ { debug : this . #debug, strict : this . #strict }
87
+ )
88
+ }
89
+
69
90
search ( { responseCode = 200 , results = [ ] , error } ) {
70
91
// the flags, score, and searchScore parts of the response are never used
71
92
// by npm, only package is used
@@ -296,13 +317,14 @@ class MockRegistry {
296
317
manifest . users = users
297
318
}
298
319
for ( const packument of packuments ) {
320
+ const unscoped = name . includes ( '/' ) ? name . split ( '/' ) [ 1 ] : name
299
321
manifest . versions [ packument . version ] = {
300
322
_id : `${ name } @${ packument . version } ` ,
301
323
name,
302
324
description : 'test package mock manifest' ,
303
325
dependencies : { } ,
304
326
dist : {
305
- tarball : `${ this . #registry} /${ name } /-/${ name } -${ packument . version } .tgz` ,
327
+ tarball : `${ this . #registry} /${ name } /-/${ unscoped } -${ packument . version } .tgz` ,
306
328
} ,
307
329
maintainers : [ ] ,
308
330
...packument ,
0 commit comments