11'use strict' ;
22
33const common = require ( '../common' ) ;
4+ common . requireNoPackageJSONAbove ( ) ;
5+
46const { it, describe } = require ( 'node:test' ) ;
57const assert = require ( 'node:assert' ) ;
68
@@ -15,7 +17,6 @@ describe('node --run [command]', () => {
1517 { cwd : __dirname } ,
1618 ) ;
1719 assert . match ( child . stderr , / E x p e r i m e n t a l W a r n i n g : T a s k r u n n e r i s a n e x p e r i m e n t a l f e a t u r e a n d m i g h t c h a n g e a t a n y t i m e / ) ;
18- assert . match ( child . stderr , / C a n ' t r e a d p a c k a g e \. j s o n / ) ;
1920 assert . strictEqual ( child . stdout , '' ) ;
2021 assert . strictEqual ( child . code , 1 ) ;
2122 } ) ;
@@ -26,7 +27,9 @@ describe('node --run [command]', () => {
2627 [ '--no-warnings' , '--run' , 'test' ] ,
2728 { cwd : __dirname } ,
2829 ) ;
29- assert . match ( child . stderr , / C a n ' t r e a d p a c k a g e \. j s o n / ) ;
30+ assert . match ( child . stderr , / C a n ' t f i n d p a c k a g e \. j s o n [ \s \S ] * / ) ;
31+ // Ensure we show the path that starting path for the search
32+ assert ( child . stderr . includes ( __dirname ) ) ;
3033 assert . strictEqual ( child . stdout , '' ) ;
3134 assert . strictEqual ( child . code , 1 ) ;
3235 } ) ;
@@ -53,6 +56,101 @@ describe('node --run [command]', () => {
5356 assert . strictEqual ( child . code , 0 ) ;
5457 } ) ;
5558
59+ it ( 'chdirs into package directory' , async ( ) => {
60+ const child = await common . spawnPromisified (
61+ process . execPath ,
62+ [ '--no-warnings' , '--run' , `pwd${ envSuffix } ` ] ,
63+ { cwd : fixtures . path ( 'run-script/sub-directory' ) } ,
64+ ) ;
65+ assert . strictEqual ( child . stdout . trim ( ) , fixtures . path ( 'run-script' ) ) ;
66+ assert . strictEqual ( child . stderr , '' ) ;
67+ assert . strictEqual ( child . code , 0 ) ;
68+ } ) ;
69+
70+ it ( 'includes actionable info when possible' , async ( ) => {
71+ {
72+ const child = await common . spawnPromisified (
73+ process . execPath ,
74+ [ '--no-warnings' , '--run' , 'missing' ] ,
75+ { cwd : fixtures . path ( 'run-script' ) } ,
76+ ) ;
77+ assert . strictEqual ( child . stdout , '' ) ;
78+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/package.json' ) ) ) ;
79+ assert ( child . stderr . includes ( 'no test specified' ) ) ;
80+ assert . strictEqual ( child . code , 1 ) ;
81+ }
82+ {
83+ const child = await common . spawnPromisified (
84+ process . execPath ,
85+ [ '--no-warnings' , '--run' , 'test' ] ,
86+ { cwd : fixtures . path ( 'run-script/missing-scripts' ) } ,
87+ ) ;
88+ assert . strictEqual ( child . stdout , '' ) ;
89+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/missing-scripts/package.json' ) ) ) ;
90+ assert . strictEqual ( child . code , 1 ) ;
91+ }
92+ {
93+ const child = await common . spawnPromisified (
94+ process . execPath ,
95+ [ '--no-warnings' , '--run' , 'test' ] ,
96+ { cwd : fixtures . path ( 'run-script/invalid-json' ) } ,
97+ ) ;
98+ assert . strictEqual ( child . stdout , '' ) ;
99+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/invalid-json/package.json' ) ) ) ;
100+ assert . strictEqual ( child . code , 1 ) ;
101+ }
102+ {
103+ const child = await common . spawnPromisified (
104+ process . execPath ,
105+ [ '--no-warnings' , '--run' , 'array' ] ,
106+ { cwd : fixtures . path ( 'run-script/invalid-schema' ) } ,
107+ ) ;
108+ assert . strictEqual ( child . stdout , '' ) ;
109+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/invalid-schema/package.json' ) ) ) ;
110+ assert . strictEqual ( child . code , 1 ) ;
111+ }
112+ {
113+ const child = await common . spawnPromisified (
114+ process . execPath ,
115+ [ '--no-warnings' , '--run' , 'boolean' ] ,
116+ { cwd : fixtures . path ( 'run-script/invalid-schema' ) } ,
117+ ) ;
118+ assert . strictEqual ( child . stdout , '' ) ;
119+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/invalid-schema/package.json' ) ) ) ;
120+ assert . strictEqual ( child . code , 1 ) ;
121+ }
122+ {
123+ const child = await common . spawnPromisified (
124+ process . execPath ,
125+ [ '--no-warnings' , '--run' , 'null' ] ,
126+ { cwd : fixtures . path ( 'run-script/invalid-schema' ) } ,
127+ ) ;
128+ assert . strictEqual ( child . stdout , '' ) ;
129+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/invalid-schema/package.json' ) ) ) ;
130+ assert . strictEqual ( child . code , 1 ) ;
131+ }
132+ {
133+ const child = await common . spawnPromisified (
134+ process . execPath ,
135+ [ '--no-warnings' , '--run' , 'number' ] ,
136+ { cwd : fixtures . path ( 'run-script/invalid-schema' ) } ,
137+ ) ;
138+ assert . strictEqual ( child . stdout , '' ) ;
139+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/invalid-schema/package.json' ) ) ) ;
140+ assert . strictEqual ( child . code , 1 ) ;
141+ }
142+ {
143+ const child = await common . spawnPromisified (
144+ process . execPath ,
145+ [ '--no-warnings' , '--run' , 'object' ] ,
146+ { cwd : fixtures . path ( 'run-script/invalid-schema' ) } ,
147+ ) ;
148+ assert . strictEqual ( child . stdout , '' ) ;
149+ assert ( child . stderr . includes ( fixtures . path ( 'run-script/invalid-schema/package.json' ) ) ) ;
150+ assert . strictEqual ( child . code , 1 ) ;
151+ }
152+ } ) ;
153+
56154 it ( 'appends positional arguments' , async ( ) => {
57155 const child = await common . spawnPromisified (
58156 process . execPath ,
@@ -120,7 +218,7 @@ describe('node --run [command]', () => {
120218 [ '--no-warnings' , '--run' , 'test' ] ,
121219 { cwd : fixtures . path ( 'run-script/cannot-parse' ) } ,
122220 ) ;
123- assert . match ( child . stderr , / C a n ' t p a r s e p a c k a g e \. j s o n / ) ;
221+ assert . match ( child . stderr , / C a n ' t p a r s e / ) ;
124222 assert . strictEqual ( child . stdout , '' ) ;
125223 assert . strictEqual ( child . code , 1 ) ;
126224 } ) ;
@@ -131,7 +229,7 @@ describe('node --run [command]', () => {
131229 [ '--no-warnings' , '--run' , 'test' ] ,
132230 { cwd : fixtures . path ( 'run-script/cannot-find-script' ) } ,
133231 ) ;
134- assert . match ( child . stderr , / C a n ' t f i n d " s c r i p t s " f i e l d i n p a c k a g e \. j s o n / ) ;
232+ assert . match ( child . stderr , / C a n ' t f i n d " s c r i p t s " f i e l d i n / ) ;
135233 assert . strictEqual ( child . stdout , '' ) ;
136234 assert . strictEqual ( child . code , 1 ) ;
137235 } ) ;
0 commit comments