@@ -2,7 +2,7 @@ import { jest } from '@jest/globals'
22
33import type { readFile , writeFile } from 'node:fs/promises'
44
5- import type yaml from 'js-yaml'
5+ import { type load , type dump , YAML11_SCHEMA } from 'js-yaml'
66
77import type {
88 CLIConfig ,
@@ -20,15 +20,17 @@ jest.unstable_mockModule('node:fs/promises', () => ({
2020 writeFile : writeFileMock ,
2121} ) )
2222
23- const yamlLoadMock = jest . fn < typeof yaml . load > ( )
24- const yamlDumpMock = jest . fn < typeof yaml . dump > ( )
23+ const yamlLoadMock = jest . fn < typeof load > ( )
24+ const yamlDumpMock = jest . fn < typeof dump > ( )
2525jest . unstable_mockModule ( 'js-yaml' , ( ) => ( {
26- default : {
27- load : yamlLoadMock ,
28- dump : yamlDumpMock ,
29- } ,
26+ load : yamlLoadMock ,
27+ dump : yamlDumpMock ,
28+ // eslint-disable-next-line @typescript-eslint/naming-convention
29+ YAML11_SCHEMA ,
3030} ) )
3131
32+ const yamlLoadOptions = { schema : YAML11_SCHEMA }
33+
3234const { loggerMock, warnMock } = await import ( '../test-lib/logger-mock.js' )
3335
3436const yamlExistsMock = jest . fn < typeof yamlExists > ( )
@@ -67,18 +69,16 @@ describe('loadConfigFile', () => {
6769 expect ( readFileMock ) . toHaveBeenCalledTimes ( 0 )
6870 } )
6971
70- it ( 'returns empty object for empty file' , async ( ) => {
71- readFileMock . mockResolvedValueOnce ( 'empty contents' )
72- yamlLoadMock . mockReturnValueOnce ( '' )
72+ it . each ( [ undefined , null , '' ] ) ( 'returns empty object for empty file' , async ( fileContents ) => {
73+ readFileMock . mockResolvedValueOnce ( fileContents as string )
7374
7475 expect ( await loadConfigFile ( 'empty file' ) ) . toEqual ( { } )
7576
7677 expect ( yamlExistsMock ) . toHaveBeenCalledTimes ( 1 )
7778 expect ( yamlExistsMock ) . toHaveBeenCalledWith ( 'empty file' )
7879 expect ( readFileMock ) . toHaveBeenCalledTimes ( 1 )
7980 expect ( readFileMock ) . toHaveBeenCalledWith ( 'empty file' , 'utf-8' )
80- expect ( yamlLoadMock ) . toHaveBeenCalledTimes ( 1 )
81- expect ( yamlLoadMock ) . toHaveBeenCalledWith ( 'empty contents' )
81+ expect ( yamlLoadMock ) . not . toHaveBeenCalled ( )
8282 } )
8383
8484 it . each ( [ 'string' , [ 'array' ] ] ) ( 'throws error for non-object yaml file' , async ( parsedYAML ) => {
@@ -92,7 +92,7 @@ describe('loadConfigFile', () => {
9292 expect ( readFileMock ) . toHaveBeenCalledTimes ( 1 )
9393 expect ( readFileMock ) . toHaveBeenCalledWith ( 'configFilename.json' , 'utf-8' )
9494 expect ( yamlLoadMock ) . toHaveBeenCalledTimes ( 1 )
95- expect ( yamlLoadMock ) . toHaveBeenCalledWith ( 'file contents' )
95+ expect ( yamlLoadMock ) . toHaveBeenCalledWith ( 'file contents' , yamlLoadOptions )
9696 expect ( fatalErrorMock ) . toHaveBeenCalledExactlyOnceWith (
9797 'invalid config file format\n' + seeConfigDocs ,
9898 )
@@ -113,7 +113,7 @@ describe('loadConfigFile', () => {
113113 expect ( readFileMock ) . toHaveBeenCalledTimes ( 1 )
114114 expect ( readFileMock ) . toHaveBeenCalledWith ( 'file with bad configs' , 'utf-8' )
115115 expect ( yamlLoadMock ) . toHaveBeenCalledTimes ( 1 )
116- expect ( yamlLoadMock ) . toHaveBeenCalledWith ( 'config with multiple errors' )
116+ expect ( yamlLoadMock ) . toHaveBeenCalledWith ( 'config with multiple errors' , yamlLoadOptions )
117117 expect ( fatalErrorMock ) . toHaveBeenCalledExactlyOnceWith (
118118 'bad profile badProfile1; profile must be an object\n' +
119119 'bad profile badProfile2; profile must be an object\n' +
@@ -141,7 +141,7 @@ describe('loadConfigFile', () => {
141141 expect ( readFileMock ) . toHaveBeenCalledTimes ( 1 )
142142 expect ( readFileMock ) . toHaveBeenCalledWith ( 'good file' , 'utf-8' )
143143 expect ( yamlLoadMock ) . toHaveBeenCalledTimes ( 1 )
144- expect ( yamlLoadMock ) . toHaveBeenCalledWith ( 'good contents' )
144+ expect ( yamlLoadMock ) . toHaveBeenCalledWith ( 'good contents' , yamlLoadOptions )
145145 } )
146146} )
147147
0 commit comments