1
1
"use strict" ;
2
2
3
- jest . mock ( "@lerna/child-process" ) ;
4
- jest . mock ( "@lerna/has-npm-version" ) ;
5
- jest . mock ( "@lerna/log-packed" ) ;
3
+ jest . mock ( "@lerna/run-lifecycle" ) ;
4
+ jest . mock ( "libnpm/publish" ) ;
6
5
jest . mock ( "fs-extra" ) ;
7
6
8
7
// mocked modules
9
- const ChildProcessUtilities = require ( "@lerna/child-process" ) ;
10
8
const fs = require ( "fs-extra" ) ;
9
+ const publish = require ( "libnpm/publish" ) ;
10
+ const runLifecycle = require ( "@lerna/run-lifecycle" ) ;
11
11
12
12
// helpers
13
13
const path = require ( "path" ) ;
@@ -17,8 +17,12 @@ const Package = require("@lerna/package");
17
17
const npmPublish = require ( ".." ) ;
18
18
19
19
describe ( "npm-publish" , ( ) => {
20
+ const mockTarData = Buffer . from ( "MOCK" ) ;
21
+
22
+ fs . readFile . mockImplementation ( ( ) => Promise . resolve ( mockTarData ) ) ;
20
23
fs . remove . mockResolvedValue ( ) ;
21
- ChildProcessUtilities . exec . mockResolvedValue ( ) ;
24
+ publish . mockResolvedValue ( ) ;
25
+ runLifecycle . mockResolvedValue ( ) ;
22
26
23
27
const rootPath = path . normalize ( "/test" ) ;
24
28
const pkg = new Package (
@@ -27,96 +31,84 @@ describe("npm-publish", () => {
27
31
rootPath
28
32
) ;
29
33
30
- // technically decorated in npmPack , stubbed here
34
+ // technically decorated in ../../commands/publish , stubbed here
31
35
pkg . tarball = {
32
36
filename : "test-1.10.100.tgz" ,
33
37
} ;
34
38
35
- it ( "runs npm publish in a directory with --tag support" , async ( ) => {
36
- const result = await npmPublish ( pkg , "published-tag" , { npmClient : "npm" } ) ;
39
+ it ( "pipelines input package" , async ( ) => {
40
+ const opts = new Map ( ) ;
41
+ const result = await npmPublish ( pkg , "latest" , opts ) ;
37
42
38
43
expect ( result ) . toBe ( pkg ) ;
39
- expect ( ChildProcessUtilities . exec ) . toHaveBeenLastCalledWith (
40
- "npm" ,
41
- [ "publish" , "--ignore-scripts" , "--tag" , "published-tag" , "test-1.10.100.tgz" ] ,
42
- {
43
- cwd : pkg . location ,
44
- env : { } ,
45
- pkg,
46
- }
47
- ) ;
48
- expect ( fs . remove ) . toHaveBeenLastCalledWith ( path . join ( pkg . location , pkg . tarball . filename ) ) ;
49
44
} ) ;
50
45
51
- it ( "does not pass --tag when none present (npm default)" , async ( ) => {
52
- await npmPublish ( pkg , undefined , { npmClient : "npm" } ) ;
53
-
54
- expect ( ChildProcessUtilities . exec ) . toHaveBeenLastCalledWith (
55
- "npm" ,
56
- [ "publish" , "--ignore-scripts" , "test-1.10.100.tgz" ] ,
57
- {
58
- cwd : pkg . location ,
59
- env : { } ,
60
- pkg,
61
- }
62
- ) ;
46
+ it ( "calls libnpm/publish with correct arguments" , async ( ) => {
47
+ const opts = new Map ( ) ;
48
+
49
+ await npmPublish ( pkg , "published-tag" , opts ) ;
50
+
51
+ expect ( publish ) . toHaveBeenCalledWith ( { name : "test" , version : "1.10.100" } , mockTarData , {
52
+ projectScope : "test" ,
53
+ tag : "published-tag" ,
54
+ } ) ;
63
55
} ) ;
64
56
65
- it ( "trims trailing whitespace in tag parameter" , async ( ) => {
66
- await npmPublish ( pkg , "trailing-tag " , { npmClient : "npm" } ) ;
67
-
68
- expect ( ChildProcessUtilities . exec ) . toHaveBeenLastCalledWith (
69
- "npm" ,
70
- [ "publish" , "--ignore-scripts" , "--tag" , "trailing-tag" , "test-1.10.100.tgz" ] ,
71
- {
72
- cwd : pkg . location ,
73
- env : { } ,
74
- pkg,
75
- }
57
+ it ( "falls back to opts.tag for dist-tag" , async ( ) => {
58
+ const opts = new Map ( [ [ "tag" , "custom-default" ] ] ) ;
59
+
60
+ await npmPublish ( pkg , undefined , opts ) ;
61
+
62
+ expect ( publish ) . toHaveBeenCalledWith (
63
+ { name : "test" , version : "1.10.100" } ,
64
+ mockTarData ,
65
+ expect . objectContaining ( { tag : "custom-default" } )
76
66
) ;
77
67
} ) ;
78
68
79
- it ( "supports custom registry" , async ( ) => {
80
- const registry = "https://custom-registry/npmPublish" ;
81
-
82
- await npmPublish ( pkg , "custom-registry" , { npmClient : "npm" , registry } ) ;
83
-
84
- expect ( ChildProcessUtilities . exec ) . toHaveBeenLastCalledWith (
85
- "npm" ,
86
- [ "publish" , "--ignore-scripts" , "--tag" , "custom-registry" , "test-1.10.100.tgz" ] ,
87
- {
88
- cwd : pkg . location ,
89
- env : {
90
- npm_config_registry : registry ,
91
- } ,
92
- pkg,
93
- }
69
+ it ( "falls back to default tag" , async ( ) => {
70
+ await npmPublish ( pkg ) ;
71
+
72
+ expect ( publish ) . toHaveBeenCalledWith (
73
+ { name : "test" , version : "1.10.100" } ,
74
+ mockTarData ,
75
+ expect . objectContaining ( { tag : "latest" } )
94
76
) ;
95
77
} ) ;
96
78
97
- describe ( "with npmClient yarn" , ( ) => {
98
- it ( "appends --new-version to avoid interactive prompt" , async ( ) => {
99
- await npmPublish ( pkg , "yarn-publish" , { npmClient : "yarn" } ) ;
100
-
101
- expect ( ChildProcessUtilities . exec ) . toHaveBeenLastCalledWith (
102
- "yarn" ,
103
- [
104
- "publish" ,
105
- "--ignore-scripts" ,
106
- "--tag" ,
107
- "yarn-publish" ,
108
- "--new-version" ,
109
- pkg . version ,
110
- "--non-interactive" ,
111
- "--no-git-tag-version" ,
112
- "test-1.10.100.tgz" ,
113
- ] ,
114
- {
115
- cwd : pkg . location ,
116
- env : { } ,
117
- pkg,
118
- }
119
- ) ;
79
+ it ( "calls publish lifecycles" , async ( ) => {
80
+ await npmPublish ( pkg , "lifecycles" ) ;
81
+
82
+ // figgy-pudding Proxy hanky-panky defeats jest wizardry
83
+ const aFiggyPudding = expect . objectContaining ( { __isFiggyPudding : true } ) ;
84
+
85
+ expect ( runLifecycle ) . toHaveBeenCalledWith ( pkg , "publish" , aFiggyPudding ) ;
86
+ expect ( runLifecycle ) . toHaveBeenCalledWith ( pkg , "postpublish" , aFiggyPudding ) ;
87
+
88
+ runLifecycle . mock . calls . forEach ( args => {
89
+ const pud = args . slice ( ) . pop ( ) ;
90
+
91
+ expect ( pud . toJSON ( ) ) . toMatchObject ( {
92
+ projectScope : "test" ,
93
+ tag : "lifecycles" ,
94
+ } ) ;
120
95
} ) ;
121
96
} ) ;
97
+
98
+ it ( "omits opts.logstream" , async ( ) => {
99
+ const opts = new Map ( [ [ "logstream" , "SKIPPED" ] ] ) ;
100
+
101
+ await npmPublish ( pkg , "canary" , opts ) ;
102
+
103
+ const pud = runLifecycle . mock . calls . pop ( ) . pop ( ) ;
104
+
105
+ expect ( pud . toJSON ( ) ) . not . toHaveProperty ( "logstream" ) ;
106
+ } ) ;
107
+
108
+ it ( "removes tarball after success" , async ( ) => {
109
+ const opts = new Map ( ) ;
110
+ await npmPublish ( pkg , "latest" , opts ) ;
111
+
112
+ expect ( fs . remove ) . toHaveBeenLastCalledWith ( path . join ( pkg . location , pkg . tarball . filename ) ) ;
113
+ } ) ;
122
114
} ) ;
0 commit comments