1- import { existsSync } from "fs" ;
1+ import { existsSync } from "node:fs" ;
2+ import { resolve } from "node:path" ;
23import { runCommand } from "helpers/command" ;
34import { installPackages , installWrangler , npmInstall } from "helpers/packages" ;
45import { afterEach , beforeEach , describe , expect , test , vi } from "vitest" ;
56import whichPMRuns from "which-pm-runs" ;
67import { createTestContext } from "../../__tests__/helpers" ;
8+ import * as files from "../files" ;
79import { mockPackageManager } from "./mocks" ;
810
911vi . mock ( "fs" ) ;
1012vi . mock ( "which-pm-runs" ) ;
1113vi . mock ( "which-pm-runs" ) ;
1214vi . mock ( "helpers/command" ) ;
15+ vi . mock ( "../files" , ( ) => ( {
16+ readJSON : vi . fn ( ) ,
17+ writeJSON : vi . fn ( ) ,
18+ } ) ) ;
19+ const mockReadJSON = vi . mocked ( files . readJSON ) ;
20+ const mockWriteJSON = vi . mocked ( files . writeJSON ) ;
1321
1422describe ( "Package Helpers" , ( ) => {
1523 beforeEach ( ( ) => {
1624 vi . mocked ( whichPMRuns ) . mockReturnValue ( { name : "npm" , version : "8.3.1" } ) ;
1725 vi . mocked ( existsSync ) . mockImplementation ( ( ) => false ) ;
1826 } ) ;
1927
20- afterEach ( ( ) => { } ) ;
28+ afterEach ( ( ) => {
29+ vi . clearAllMocks ( ) ;
30+ } ) ;
2131
2232 describe ( "npmInstall" , ( ) => {
2333 test ( "npm" , async ( ) => {
@@ -62,13 +72,37 @@ describe("Package Helpers", () => {
6272 "with $pm" ,
6373 async ( { pm, initialArgs, additionalArgs } ) => {
6474 mockPackageManager ( pm ) ;
65- const packages = [ "foo" , "bar" , "baz" ] ;
75+ mockReadJSON . mockReturnValue ( {
76+ [ "dependencies" ] : {
77+ foo : "^1.0.0" ,
78+ bar : "^2.0.0" ,
79+ baz : "^1.2.3" ,
80+ } ,
81+ } ) ;
82+ const packages = [ "foo" , "bar@latest" , "[email protected] " ] ; 6683 await installPackages ( packages ) ;
6784
6885 expect ( vi . mocked ( runCommand ) ) . toHaveBeenCalledWith (
6986 [ ...initialArgs , ...packages , ...( additionalArgs ?? [ ] ) ] ,
7087 expect . anything ( ) ,
7188 ) ;
89+
90+ if ( pm === "npm" ) {
91+ // Check that package.json was updated for npm
92+ expect ( mockReadJSON ) . toHaveBeenCalledWith (
93+ resolve ( process . cwd ( ) , "package.json" ) ,
94+ ) ;
95+ expect ( mockWriteJSON ) . toHaveBeenCalledWith (
96+ resolve ( process . cwd ( ) , "package.json" ) ,
97+ expect . objectContaining ( {
98+ [ "dependencies" ] : {
99+ foo : "^1.0.0" ,
100+ bar : "^2.0.0" ,
101+ baz : "1.2.3" ,
102+ } ,
103+ } ) ,
104+ ) ;
105+ }
72106 } ,
73107 ) ;
74108
@@ -87,18 +121,47 @@ describe("Package Helpers", () => {
87121 "with $pm (dev = true)" ,
88122 async ( { pm, initialArgs, additionalArgs } ) => {
89123 mockPackageManager ( pm ) ;
90- const packages = [ "foo" , "bar" , "baz" ] ;
124+ mockReadJSON . mockReturnValue ( {
125+ [ "devDependencies" ] : {
126+ foo : "^1.0.0" ,
127+ bar : "^2.0.0" ,
128+ baz : "^1.2.3" ,
129+ } ,
130+ } ) ;
131+ const packages = [ "foo" , "bar@latest" , "[email protected] " ] ; 91132 await installPackages ( packages , { dev : true } ) ;
92133
93134 expect ( vi . mocked ( runCommand ) ) . toHaveBeenCalledWith (
94135 [ ...initialArgs , ...packages , ...( additionalArgs ?? [ ] ) ] ,
95136 expect . anything ( ) ,
96137 ) ;
138+
139+ if ( pm === "npm" ) {
140+ // Check that package.json was updated for npm
141+ expect ( mockReadJSON ) . toHaveBeenCalledWith (
142+ resolve ( process . cwd ( ) , "package.json" ) ,
143+ ) ;
144+ expect ( mockWriteJSON ) . toHaveBeenCalledWith (
145+ resolve ( process . cwd ( ) , "package.json" ) ,
146+ expect . objectContaining ( {
147+ [ "devDependencies" ] : {
148+ foo : "^1.0.0" ,
149+ bar : "^2.0.0" ,
150+ baz : "1.2.3" ,
151+ } ,
152+ } ) ,
153+ ) ;
154+ }
97155 } ,
98156 ) ;
99157 } ) ;
100158
101159 test ( "installWrangler" , async ( ) => {
160+ mockReadJSON . mockReturnValue ( {
161+ [ "devDependencies" ] : {
162+ wrangler : "^4.0.0" ,
163+ } ,
164+ } ) ;
102165 await installWrangler ( ) ;
103166
104167 expect ( vi . mocked ( runCommand ) ) . toHaveBeenCalledWith (
0 commit comments