@@ -60,7 +60,7 @@ describe('npmPublish', () => {
6060 notifySlack : slackSpy ,
6161 } ) ;
6262
63- spawnSpy = jasmine . createSpy ( 'spawn' ) ;
63+ spawnSpy = jasmine . createSpy ( 'spawn' ) . and . returnValue ( Promise . resolve ( '' ) ) ;
6464
6565 mock ( './spawn' , {
6666 spawn : spawnSpy ,
@@ -113,7 +113,9 @@ describe('npmPublish', () => {
113113 it ( 'should publish to NPM without npm-token' , async ( ) => {
114114 mockNpmToken = '' ;
115115
116- const { npmPublish } = getUtil ( ) ;
116+ const { npmPublish, nodeVersionGetter } = getUtil ( ) ;
117+
118+ spyOn ( nodeVersionGetter , 'getVersion' ) . and . returnValue ( 'v24.0.0' ) ;
117119
118120 await npmPublish ( ) ;
119121
@@ -216,4 +218,89 @@ describe('npmPublish', () => {
216218 'Aborted publishing to NPM because the version listed in package.json (1.0.0) does not match the git tag (1.1.0)!' ,
217219 ) ;
218220 } ) ;
221+
222+ it ( 'should use npm from Node.js 24 when current Node version is below 24 and no token is provided' , async ( ) => {
223+ mockNpmToken = '' ;
224+
225+ const { npmPublish, nodeVersionGetter } = getUtil ( ) ;
226+
227+ spyOn ( nodeVersionGetter , 'getVersion' ) . and . returnValue ( 'v20.0.0' ) ;
228+
229+ spawnSpy . and . callFake ( ( command : string , _args : string [ ] ) => {
230+ if ( command === 'sh' ) {
231+ return Promise . resolve ( '/mock/nvm/versions/node/v24.0.0/bin/npm' ) ;
232+ }
233+ return Promise . resolve ( ) ;
234+ } ) ;
235+
236+ await npmPublish ( ) ;
237+
238+ expect ( spawnSpy ) . toHaveBeenCalledWith ( 'sh' , [
239+ '-c' ,
240+ 'ls $NVM_DIR/versions/node/v24.*/bin/npm' ,
241+ ] ) ;
242+ expect ( spawnSpy ) . toHaveBeenCalledWith (
243+ '/mock/nvm/versions/node/v24.0.0/bin/npm' ,
244+ [ 'publish' , '--access' , 'public' , '--tag' , 'latest' ] ,
245+ {
246+ cwd : path . join ( process . cwd ( ) , 'MOCK_WORKING_DIRECTORY' , 'dist' ) ,
247+ stdio : 'inherit' ,
248+ } ,
249+ ) ;
250+ } ) ;
251+
252+ it ( 'should fail if Node.js 24 is not available when current Node version is below 24 and no token is provided' , async ( ) => {
253+ mockNpmToken = '' ;
254+
255+ const { npmPublish, nodeVersionGetter } = getUtil ( ) ;
256+
257+ spyOn ( nodeVersionGetter , 'getVersion' ) . and . returnValue ( 'v20.0.0' ) ;
258+
259+ await expectAsync ( npmPublish ( ) ) . toBeRejectedWith (
260+ 'Aborted publishing to NPM with trusted publishing because NPM from Node.js 24 could not be found!' ,
261+ ) ;
262+
263+ expect ( spawnSpy ) . toHaveBeenCalledWith ( 'sh' , [
264+ '-c' ,
265+ 'ls $NVM_DIR/versions/node/v24.*/bin/npm' ,
266+ ] ) ;
267+ } ) ;
268+
269+ it ( 'should use regular npm when Node version is 24 or above and no token is provided' , async ( ) => {
270+ mockNpmToken = '' ;
271+
272+ const { npmPublish, nodeVersionGetter } = getUtil ( ) ;
273+
274+ spyOn ( nodeVersionGetter , 'getVersion' ) . and . returnValue ( 'v24.0.0' ) ;
275+
276+ await npmPublish ( ) ;
277+
278+ expect ( spawnSpy ) . toHaveBeenCalledWith (
279+ 'npm' ,
280+ [ 'publish' , '--access' , 'public' , '--tag' , 'latest' ] ,
281+ {
282+ cwd : path . join ( process . cwd ( ) , 'MOCK_WORKING_DIRECTORY' , 'dist' ) ,
283+ stdio : 'inherit' ,
284+ } ,
285+ ) ;
286+ expect ( spawnSpy ) . not . toHaveBeenCalledWith ( 'ls' , jasmine . any ( Array ) ) ;
287+ } ) ;
288+
289+ it ( 'should use regular npm when token is provided regardless of Node version' , async ( ) => {
290+ const { npmPublish, nodeVersionGetter } = getUtil ( ) ;
291+
292+ spyOn ( nodeVersionGetter , 'getVersion' ) . and . returnValue ( 'v20.0.0' ) ;
293+
294+ await npmPublish ( ) ;
295+
296+ expect ( spawnSpy ) . toHaveBeenCalledWith (
297+ 'npm' ,
298+ [ 'publish' , '--access' , 'public' , '--tag' , 'latest' ] ,
299+ {
300+ cwd : path . join ( process . cwd ( ) , 'MOCK_WORKING_DIRECTORY' , 'dist' ) ,
301+ stdio : 'inherit' ,
302+ } ,
303+ ) ;
304+ expect ( spawnSpy ) . not . toHaveBeenCalledWith ( 'ls' , jasmine . any ( Array ) ) ;
305+ } ) ;
219306} ) ;
0 commit comments