1- import { createStubExec } from "../adapters/exec.stubs " ;
1+ import { createStubFileSystem } from "../adapters/fileSystem.stub " ;
22import { findPackagesConfiguration } from "./findPackagesConfiguration" ;
33
44describe ( "findPackagesConfiguration" , ( ) => {
55 it ( "defaults the configuration file when one isn't provided" , async ( ) => {
66 // Arrange
7- const dependencies = { exec : createStubExec ( ) } ;
7+ const dependencies = {
8+ fileSystem : createStubFileSystem ( {
9+ data : "{}"
10+ } )
11+ }
812
913 // Act
1014 await findPackagesConfiguration ( dependencies , undefined ) ;
1115
1216 // Assert
13- expect ( dependencies . exec ) . toHaveBeenLastCalledWith ( `cat " ./package.json" ` ) ;
17+ expect ( dependencies . fileSystem . readFile ) . toHaveBeenLastCalledWith ( `./package.json` ) ;
1418 } ) ;
1519
16- it ( "includes a configuration file in the packages command when one is provided" , async ( ) => {
20+ it ( "uses the configuration file from the packages command when one is provided" , async ( ) => {
1721 // Arrange
18- const dependencies = { exec : createStubExec ( ) } ;
22+ const dependencies = {
23+ fileSystem : createStubFileSystem ( {
24+ data : "{}"
25+ } )
26+ }
1927 const config = "./custom/package.json" ;
2028
2129 // Act
2230 await findPackagesConfiguration ( dependencies , config ) ;
2331
2432 // Assert
25- expect ( dependencies . exec ) . toHaveBeenLastCalledWith ( `cat "./custom/package.json"` ) ;
33+ expect ( dependencies . fileSystem . readFile ) . toHaveBeenLastCalledWith ( `./custom/package.json` ) ;
34+ } ) ;
35+
36+ it ( "returns an error when readFile returns an error" , async ( ) => {
37+ // Arrange
38+ const error = new Error ( "Oh no!" ) ;
39+ const dependencies = {
40+ fileSystem : createStubFileSystem ( {
41+ data : error
42+ } )
43+ }
44+ const config = "./custom/package.json" ;
45+
46+ // Act
47+ const result = await findPackagesConfiguration ( dependencies , config ) ;
48+
49+ // Assert
50+ expect ( result ) . toBe ( error ) ;
2651 } ) ;
2752
2853 it ( "applies packages defaults when none are provided" , async ( ) => {
2954 // Arrange
30- const dependencies = { exec : createStubExec ( { stdout : "{}" } ) } ;
55+ const dependencies = {
56+ fileSystem : createStubFileSystem ( {
57+ data : "{}"
58+ } )
59+ }
3160 const config = "./package.json" ;
3261
3362 // Act
@@ -39,4 +68,28 @@ describe("findPackagesConfiguration", () => {
3968 devDependencies : { } ,
4069 } ) ;
4170 } ) ;
71+
72+ it ( "uses existing package data when it exists" , async ( ) => {
73+ // Arrange
74+ const data = {
75+ dependencies : {
76+ eslint : "^11.22.33" ,
77+ } ,
78+ devDependencies : {
79+ tslint : "^12.34.56"
80+ }
81+ }
82+ const dependencies = {
83+ fileSystem : createStubFileSystem ( {
84+ data : JSON . stringify ( data )
85+ } )
86+ }
87+ const config = "./package.json" ;
88+
89+ // Act
90+ const result = await findPackagesConfiguration ( dependencies , config ) ;
91+
92+ // Assert
93+ expect ( result ) . toEqual ( data ) ;
94+ } ) ;
4295} ) ;
0 commit comments