@@ -2,10 +2,10 @@ import fs from 'fs';
22import path from 'path' ;
33import { SemVer } from 'semver' ;
44import { withSandbox } from '../tests/helpers' ;
5- import { readManifest } from './package-manifest' ;
5+ import { readPackageManifest } from './package-manifest' ;
66
77describe ( 'package-manifest' , ( ) => {
8- describe ( 'readManifest ' , ( ) => {
8+ describe ( 'readPackageManifest ' , ( ) => {
99 it ( 'reads a minimal package manifest, expanding it by filling in values for optional fields' , async ( ) => {
1010 await withSandbox ( async ( sandbox ) => {
1111 const manifestPath = path . join ( sandbox . directoryPath , 'package.json' ) ;
@@ -29,7 +29,69 @@ describe('package-manifest', () => {
2929 JSON . stringify ( unvalidatedManifest ) ,
3030 ) ;
3131
32- expect ( await readManifest ( manifestPath ) ) . toStrictEqual ( {
32+ expect ( await readPackageManifest ( manifestPath ) ) . toStrictEqual ( {
33+ unvalidatedManifest,
34+ validatedManifest,
35+ } ) ;
36+ } ) ;
37+ } ) ;
38+
39+ it ( 'reads a package manifest where "private" is true' , async ( ) => {
40+ await withSandbox ( async ( sandbox ) => {
41+ const manifestPath = path . join ( sandbox . directoryPath , 'package.json' ) ;
42+ const unvalidatedManifest = {
43+ name : 'foo' ,
44+ version : '1.2.3' ,
45+ private : true ,
46+ } ;
47+ const validatedManifest = {
48+ name : 'foo' ,
49+ version : new SemVer ( '1.2.3' ) ,
50+ workspaces : [ ] ,
51+ private : true ,
52+ bundledDependencies : { } ,
53+ dependencies : { } ,
54+ devDependencies : { } ,
55+ optionalDependencies : { } ,
56+ peerDependencies : { } ,
57+ } ;
58+ await fs . promises . writeFile (
59+ manifestPath ,
60+ JSON . stringify ( unvalidatedManifest ) ,
61+ ) ;
62+
63+ expect ( await readPackageManifest ( manifestPath ) ) . toStrictEqual ( {
64+ unvalidatedManifest,
65+ validatedManifest,
66+ } ) ;
67+ } ) ;
68+ } ) ;
69+
70+ it ( 'reads a package manifest where "private" is false' , async ( ) => {
71+ await withSandbox ( async ( sandbox ) => {
72+ const manifestPath = path . join ( sandbox . directoryPath , 'package.json' ) ;
73+ const unvalidatedManifest = {
74+ name : 'foo' ,
75+ version : '1.2.3' ,
76+ private : false ,
77+ } ;
78+ const validatedManifest = {
79+ name : 'foo' ,
80+ version : new SemVer ( '1.2.3' ) ,
81+ workspaces : [ ] ,
82+ private : false ,
83+ bundledDependencies : { } ,
84+ dependencies : { } ,
85+ devDependencies : { } ,
86+ optionalDependencies : { } ,
87+ peerDependencies : { } ,
88+ } ;
89+ await fs . promises . writeFile (
90+ manifestPath ,
91+ JSON . stringify ( unvalidatedManifest ) ,
92+ ) ;
93+
94+ expect ( await readPackageManifest ( manifestPath ) ) . toStrictEqual ( {
3395 unvalidatedManifest,
3496 validatedManifest,
3597 } ) ;
@@ -44,12 +106,73 @@ describe('package-manifest', () => {
44106 version : '1.2.3' ,
45107 workspaces : [ 'packages/*' ] ,
46108 private : true ,
109+ bundledDependencies : {
110+ foo : 'bar' ,
111+ } ,
112+ dependencies : {
113+ foo : 'bar' ,
114+ } ,
115+ devDependencies : {
116+ foo : 'bar' ,
117+ } ,
118+ optionalDependencies : {
119+ foo : 'bar' ,
120+ } ,
121+ peerDependencies : {
122+ foo : 'bar' ,
123+ } ,
47124 } ;
48125 const validatedManifest = {
49126 name : 'foo' ,
50127 version : new SemVer ( '1.2.3' ) ,
51128 workspaces : [ 'packages/*' ] ,
52129 private : true ,
130+ bundledDependencies : {
131+ foo : 'bar' ,
132+ } ,
133+ dependencies : {
134+ foo : 'bar' ,
135+ } ,
136+ devDependencies : {
137+ foo : 'bar' ,
138+ } ,
139+ optionalDependencies : {
140+ foo : 'bar' ,
141+ } ,
142+ peerDependencies : {
143+ foo : 'bar' ,
144+ } ,
145+ } ;
146+ await fs . promises . writeFile (
147+ manifestPath ,
148+ JSON . stringify ( unvalidatedManifest ) ,
149+ ) ;
150+
151+ expect ( await readPackageManifest ( manifestPath ) ) . toStrictEqual ( {
152+ unvalidatedManifest,
153+ validatedManifest,
154+ } ) ;
155+ } ) ;
156+ } ) ;
157+
158+ it ( 'reads a package manifest where dependencies fields are provided but empty' , async ( ) => {
159+ await withSandbox ( async ( sandbox ) => {
160+ const manifestPath = path . join ( sandbox . directoryPath , 'package.json' ) ;
161+ const unvalidatedManifest = {
162+ name : 'foo' ,
163+ version : '1.2.3' ,
164+ private : true ,
165+ bundledDependencies : { } ,
166+ dependencies : { } ,
167+ devDependencies : { } ,
168+ optionalDependencies : { } ,
169+ peerDependencies : { } ,
170+ } ;
171+ const validatedManifest = {
172+ name : 'foo' ,
173+ version : new SemVer ( '1.2.3' ) ,
174+ workspaces : [ ] ,
175+ private : true ,
53176 bundledDependencies : { } ,
54177 dependencies : { } ,
55178 devDependencies : { } ,
@@ -61,7 +184,7 @@ describe('package-manifest', () => {
61184 JSON . stringify ( unvalidatedManifest ) ,
62185 ) ;
63186
64- expect ( await readManifest ( manifestPath ) ) . toStrictEqual ( {
187+ expect ( await readPackageManifest ( manifestPath ) ) . toStrictEqual ( {
65188 unvalidatedManifest,
66189 validatedManifest,
67190 } ) ;
@@ -92,7 +215,7 @@ describe('package-manifest', () => {
92215 JSON . stringify ( unvalidatedManifest ) ,
93216 ) ;
94217
95- expect ( await readManifest ( manifestPath ) ) . toStrictEqual ( {
218+ expect ( await readPackageManifest ( manifestPath ) ) . toStrictEqual ( {
96219 unvalidatedManifest,
97220 validatedManifest,
98221 } ) ;
@@ -109,7 +232,7 @@ describe('package-manifest', () => {
109232 } ) ,
110233 ) ;
111234
112- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
235+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
113236 `The value of "name" in the manifest located at "${ sandbox . directoryPath } " must be a non-empty string` ,
114237 ) ;
115238 } ) ;
@@ -126,7 +249,7 @@ describe('package-manifest', () => {
126249 } ) ,
127250 ) ;
128251
129- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
252+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
130253 `The value of "name" in the manifest located at "${ sandbox . directoryPath } " must be a non-empty string` ,
131254 ) ;
132255 } ) ;
@@ -143,7 +266,7 @@ describe('package-manifest', () => {
143266 } ) ,
144267 ) ;
145268
146- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
269+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
147270 `The value of "name" in the manifest located at "${ sandbox . directoryPath } " must be a non-empty string` ,
148271 ) ;
149272 } ) ;
@@ -159,7 +282,7 @@ describe('package-manifest', () => {
159282 } ) ,
160283 ) ;
161284
162- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
285+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
163286 'The value of "version" in the manifest for "foo" must be a valid SemVer version string' ,
164287 ) ;
165288 } ) ;
@@ -176,7 +299,7 @@ describe('package-manifest', () => {
176299 } ) ,
177300 ) ;
178301
179- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
302+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
180303 'The value of "version" in the manifest for "foo" must be a valid SemVer version string' ,
181304 ) ;
182305 } ) ;
@@ -194,30 +317,12 @@ describe('package-manifest', () => {
194317 } ) ,
195318 ) ;
196319
197- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
320+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
198321 'The value of "workspaces" in the manifest for "foo" must be an array of non-empty strings (if present)' ,
199322 ) ;
200323 } ) ;
201324 } ) ;
202325
203- it ( 'throws if "private" is not a boolean' , async ( ) => {
204- await withSandbox ( async ( sandbox ) => {
205- const manifestPath = path . join ( sandbox . directoryPath , 'package.json' ) ;
206- await fs . promises . writeFile (
207- manifestPath ,
208- JSON . stringify ( {
209- name : 'foo' ,
210- version : '1.2.3' ,
211- private : 12345 ,
212- } ) ,
213- ) ;
214-
215- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
216- 'The value of "private" in the manifest for "foo" must be true or false (if present)' ,
217- ) ;
218- } ) ;
219- } ) ;
220-
221326 [
222327 'bundledDependencies' ,
223328 'dependencies' ,
@@ -237,7 +342,7 @@ describe('package-manifest', () => {
237342 } ) ,
238343 ) ;
239344
240- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
345+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
241346 `The value of "${ fieldName } " in the manifest for "foo" must be an object with non-empty string keys and non-empty string values` ,
242347 ) ;
243348 } ) ;
@@ -255,7 +360,7 @@ describe('package-manifest', () => {
255360 } ) ,
256361 ) ;
257362
258- await expect ( readManifest ( manifestPath ) ) . rejects . toThrow (
363+ await expect ( readPackageManifest ( manifestPath ) ) . rejects . toThrow (
259364 `The value of "${ fieldName } " in the manifest for "foo" must be an object with non-empty string keys and non-empty string values` ,
260365 ) ;
261366 } ) ;
0 commit comments