1- import fs from 'fs' ;
1+ import fs , { PathOrFileDescriptor } from 'fs' ;
22
33import { CommandInfo } from '../command' ;
44import { ExpandWildcard } from './expand-wildcard' ;
@@ -23,12 +23,53 @@ afterEach(() => {
2323} ) ;
2424
2525describe ( 'ExpandWildcard#readDeno' , ( ) => {
26- it ( 'can read deno' , ( ) => {
26+ it ( 'can read deno.json ' , ( ) => {
2727 const expectedDeno = {
2828 name : 'deno' ,
2929 version : '1.14.0' ,
3030 } ;
31- jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path ) => {
31+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
32+ return path === 'deno.json' ;
33+ } ) ;
34+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
35+ if ( path === 'deno.json' ) {
36+ return JSON . stringify ( expectedDeno ) ;
37+ }
38+ return '' ;
39+ } ) ;
40+
41+ const actualReadDeno = ExpandWildcard . readDeno ( ) ;
42+ expect ( actualReadDeno ) . toEqual ( expectedDeno ) ;
43+ } ) ;
44+
45+ it ( 'can read deno.jsonc' , ( ) => {
46+ const expectedDeno = {
47+ name : 'deno' ,
48+ version : '1.14.0' ,
49+ } ;
50+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
51+ return path === 'deno.jsonc' ;
52+ } ) ;
53+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
54+ if ( path === 'deno.jsonc' ) {
55+ return '/* comment */\n' + JSON . stringify ( expectedDeno ) ;
56+ }
57+ return '' ;
58+ } ) ;
59+
60+ const actualReadDeno = ExpandWildcard . readDeno ( ) ;
61+ expect ( actualReadDeno ) . toEqual ( expectedDeno ) ;
62+ } ) ;
63+
64+ it ( 'prefers deno.json over deno.jsonc' , ( ) => {
65+ const expectedDeno = {
66+ name : 'deno' ,
67+ version : '1.14.0' ,
68+ } ;
69+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
70+ return path === 'deno.json' || path === 'deno.jsonc' ;
71+ } ) ;
72+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
3273 if ( path === 'deno.json' ) {
3374 return JSON . stringify ( expectedDeno ) ;
3475 }
@@ -40,6 +81,7 @@ describe('ExpandWildcard#readDeno', () => {
4081 } ) ;
4182
4283 it ( 'can handle errors reading deno' , ( ) => {
84+ jest . spyOn ( fs , 'existsSync' ) . mockReturnValue ( true ) ;
4385 jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( ) => {
4486 throw new Error ( 'Error reading deno' ) ;
4587 } ) ;
@@ -55,7 +97,7 @@ describe('ExpandWildcard#readPackage', () => {
5597 name : 'concurrently' ,
5698 version : '6.4.0' ,
5799 } ;
58- jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path ) => {
100+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
59101 if ( path === 'package.json' ) {
60102 return JSON . stringify ( expectedPackage ) ;
61103 }
@@ -105,7 +147,7 @@ it('expands to nothing if no scripts exist in package.json', () => {
105147 expect ( parser . parse ( createCommandInfo ( 'npm run foo-*-baz qux' ) ) ) . toEqual ( [ ] ) ;
106148} ) ;
107149
108- it ( 'expands to nothing if no tasks exist in deno.json and no scripts exist in package.json ' , ( ) => {
150+ it ( 'expands to nothing if no tasks exist in Deno config and no scripts exist in NodeJS config ' , ( ) => {
109151 readDeno . mockReturnValue ( { } ) ;
110152 readPackage . mockReturnValue ( { } ) ;
111153
@@ -192,7 +234,7 @@ describe.each(['npm run', 'yarn run', 'pnpm run', 'bun run', 'node --run'])(
192234 expect ( readPackage ) . toHaveBeenCalledTimes ( 1 ) ;
193235 } ) ;
194236
195- it ( "doesn't read deno.json " , ( ) => {
237+ it ( "doesn't read Deno config " , ( ) => {
196238 readPackage . mockReturnValue ( { } ) ;
197239
198240 parser . parse ( createCommandInfo ( `${ command } foo-*-baz qux` ) ) ;
0 commit comments