@@ -11,14 +11,27 @@ jest.mock('execa', () => ({
1111 execa : jest . fn ( ) ,
1212} ) ) ;
1313
14+ jest . mock ( 'gh-pages' , ( ) => ( {
15+ __esModule : true ,
16+ default : {
17+ publish : jest . fn ( ) ,
18+ } ,
19+ } ) ) ;
20+
21+ jest . mock ( '../github.js' , ( ) => ( {
22+ checkGhAuth : jest . fn ( ) . mockResolvedValue ( { ok : false } ) ,
23+ } ) ) ;
24+
1425import path from 'path' ;
1526import fs from 'fs-extra' ;
1627import { execa } from 'execa' ;
28+ import ghPages from 'gh-pages' ;
1729import { runDeployToGitHubPages } from '../gh-pages.js' ;
1830
1931const mockPathExists = fs . pathExists as jest . MockedFunction < typeof fs . pathExists > ;
2032const mockReadJson = fs . readJson as jest . MockedFunction < typeof fs . readJson > ;
2133const mockExeca = execa as jest . MockedFunction < typeof execa > ;
34+ const mockGhPagesPublish = ghPages . publish as jest . MockedFunction < typeof ghPages . publish > ;
2235
2336const cwd = '/tmp/my-app' ;
2437
@@ -57,7 +70,7 @@ describe('runDeployToGitHubPages', () => {
5770
5871 it ( 'throws when git remote origin is not configured' , async ( ) => {
5972 setupPathExists ( { 'package.json' : true } ) ;
60- mockExeca . mockResolvedValue ( undefined as unknown as Awaited < ReturnType < typeof execa > > ) ;
73+ mockExeca . mockRejectedValue ( new Error ( 'not a git repo' ) ) ;
6174
6275 await expect ( runDeployToGitHubPages ( cwd ) ) . rejects . toThrow (
6376 'Please save your changes first, before deploying to GitHub Pages.'
@@ -71,7 +84,11 @@ describe('runDeployToGitHubPages', () => {
7184 it ( 'throws when no build script and skipBuild is false' , async ( ) => {
7285 setupPathExists ( { 'package.json' : true } ) ;
7386 mockReadJson . mockResolvedValueOnce ( { scripts : { } } ) ;
74- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
87+ mockExeca . mockResolvedValue ( {
88+ stdout : 'https://github.com/user/repo.git' ,
89+ stderr : '' ,
90+ exitCode : 0 ,
91+ } as Awaited < ReturnType < typeof execa > > ) ;
7592
7693 await expect ( runDeployToGitHubPages ( cwd , { skipBuild : false } ) ) . rejects . toThrow (
7794 'No "build" script found in package.json'
@@ -85,19 +102,25 @@ describe('runDeployToGitHubPages', () => {
85102 mockReadJson . mockResolvedValueOnce ( {
86103 scripts : { build : 'webpack --config webpack.prod.js' } ,
87104 } ) ;
88- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
105+ mockExeca . mockResolvedValue ( {
106+ stdout : 'https://github.com/user/repo.git' ,
107+ stderr : '' ,
108+ exitCode : 0 ,
109+ } as Awaited < ReturnType < typeof execa > > ) ;
110+ mockGhPagesPublish . mockImplementation ( ( _dir , _opts , cb ) => cb ( null ) ) ;
89111
90112 await runDeployToGitHubPages ( cwd , { skipBuild : false } ) ;
91113
92- expect ( mockExeca ) . toHaveBeenCalledTimes ( 3 ) ; // git, npm run build, gh-pages
114+ expect ( mockExeca ) . toHaveBeenCalledTimes ( 2 ) ; // git, npm run build
93115 expect ( mockExeca ) . toHaveBeenNthCalledWith ( 2 , 'npm' , [ 'run' , 'build' ] , {
94116 cwd,
95117 stdio : 'inherit' ,
96118 } ) ;
97- expect ( mockExeca ) . toHaveBeenNthCalledWith ( 3 , 'gh-pages' , [ '-d' , 'dist' , '-b' , 'gh-pages' ] , {
98- cwd,
99- stdio : 'inherit' ,
100- } ) ;
119+ expect ( mockGhPagesPublish ) . toHaveBeenCalledWith (
120+ path . join ( cwd , 'dist' ) ,
121+ { branch : 'gh-pages' , repo : 'https://github.com/user/repo.git' } ,
122+ expect . any ( Function )
123+ ) ;
101124 expect ( consoleLogSpy ) . toHaveBeenCalledWith (
102125 expect . stringContaining ( 'Running build' )
103126 ) ;
@@ -111,7 +134,12 @@ describe('runDeployToGitHubPages', () => {
111134 mockReadJson . mockResolvedValueOnce ( {
112135 scripts : { build : 'webpack' } ,
113136 } ) ;
114- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
137+ mockExeca . mockResolvedValue ( {
138+ stdout : 'https://github.com/user/repo.git' ,
139+ stderr : '' ,
140+ exitCode : 0 ,
141+ } as Awaited < ReturnType < typeof execa > > ) ;
142+ mockGhPagesPublish . mockImplementation ( ( _dir , _opts , cb ) => cb ( null ) ) ;
115143
116144 await runDeployToGitHubPages ( cwd , { skipBuild : false } ) ;
117145
@@ -126,7 +154,12 @@ describe('runDeployToGitHubPages', () => {
126154 mockReadJson . mockResolvedValueOnce ( {
127155 scripts : { build : 'vite build' } ,
128156 } ) ;
129- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
157+ mockExeca . mockResolvedValue ( {
158+ stdout : 'https://github.com/user/repo.git' ,
159+ stderr : '' ,
160+ exitCode : 0 ,
161+ } as Awaited < ReturnType < typeof execa > > ) ;
162+ mockGhPagesPublish . mockImplementation ( ( _dir , _opts , cb ) => cb ( null ) ) ;
130163
131164 await runDeployToGitHubPages ( cwd , { skipBuild : false } ) ;
132165
@@ -138,24 +171,34 @@ describe('runDeployToGitHubPages', () => {
138171
139172 it ( 'skips build and deploys when skipBuild is true' , async ( ) => {
140173 setupPathExists ( { 'package.json' : true , 'dist' : true } ) ;
141- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
174+ mockExeca . mockResolvedValue ( {
175+ stdout : 'https://github.com/user/repo.git' ,
176+ stderr : '' ,
177+ exitCode : 0 ,
178+ } as Awaited < ReturnType < typeof execa > > ) ;
179+ mockGhPagesPublish . mockImplementation ( ( _dir , _opts , cb ) => cb ( null ) ) ;
142180
143181 await runDeployToGitHubPages ( cwd , { skipBuild : true } ) ;
144182
145183 expect ( mockReadJson ) . not . toHaveBeenCalled ( ) ;
146- expect ( mockExeca ) . toHaveBeenCalledTimes ( 2 ) ; // git, gh-pages
147- expect ( mockExeca ) . toHaveBeenNthCalledWith ( 2 , 'gh-pages' , [ '-d' , 'dist' , '-b' , 'gh-pages' ] , {
148- cwd,
149- stdio : 'inherit' ,
150- } ) ;
184+ expect ( mockExeca ) . toHaveBeenCalledTimes ( 1 ) ; // git only
185+ expect ( mockGhPagesPublish ) . toHaveBeenCalledWith (
186+ path . join ( cwd , 'dist' ) ,
187+ { branch : 'gh-pages' , repo : 'https://github.com/user/repo.git' } ,
188+ expect . any ( Function )
189+ ) ;
151190 } ) ;
152191
153192 it ( 'throws when dist directory does not exist (after build)' , async ( ) => {
154193 setupPathExists ( { 'package.json' : true , 'dist' : false } ) ;
155194 mockReadJson . mockResolvedValueOnce ( {
156195 scripts : { build : 'npm run build' } ,
157196 } ) ;
158- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
197+ mockExeca . mockResolvedValue ( {
198+ stdout : 'https://github.com/user/repo.git' ,
199+ stderr : '' ,
200+ exitCode : 0 ,
201+ } as Awaited < ReturnType < typeof execa > > ) ;
159202
160203 await expect ( runDeployToGitHubPages ( cwd , { skipBuild : false } ) ) . rejects . toThrow (
161204 'Build output directory "dist" does not exist'
@@ -165,7 +208,11 @@ describe('runDeployToGitHubPages', () => {
165208
166209 it ( 'throws when dist directory does not exist with skipBuild true' , async ( ) => {
167210 setupPathExists ( { 'package.json' : true , 'dist' : false } ) ;
168- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
211+ mockExeca . mockResolvedValue ( {
212+ stdout : 'https://github.com/user/repo.git' ,
213+ stderr : '' ,
214+ exitCode : 0 ,
215+ } as Awaited < ReturnType < typeof execa > > ) ;
169216
170217 await expect ( runDeployToGitHubPages ( cwd , { skipBuild : true } ) ) . rejects . toThrow (
171218 'Build output directory "dist" does not exist'
@@ -175,18 +222,24 @@ describe('runDeployToGitHubPages', () => {
175222
176223 it ( 'uses custom distDir and branch options' , async ( ) => {
177224 setupPathExists ( { 'package.json' : true , 'build' : true } ) ;
178- mockExeca . mockResolvedValue ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > ) ;
225+ mockExeca . mockResolvedValue ( {
226+ stdout : 'https://github.com/user/repo.git' ,
227+ stderr : '' ,
228+ exitCode : 0 ,
229+ } as Awaited < ReturnType < typeof execa > > ) ;
230+ mockGhPagesPublish . mockImplementation ( ( _dir , _opts , cb ) => cb ( null ) ) ;
179231
180232 await runDeployToGitHubPages ( cwd , {
181233 skipBuild : true ,
182234 distDir : 'build' ,
183235 branch : 'pages' ,
184236 } ) ;
185237
186- expect ( mockExeca ) . toHaveBeenNthCalledWith ( 2 , 'gh-pages' , [ '-d' , 'build' , '-b' , 'pages' ] , {
187- cwd,
188- stdio : 'inherit' ,
189- } ) ;
238+ expect ( mockGhPagesPublish ) . toHaveBeenCalledWith (
239+ path . join ( cwd , 'build' ) ,
240+ { branch : 'pages' , repo : 'https://github.com/user/repo.git' } ,
241+ expect . any ( Function )
242+ ) ;
190243 expect ( consoleLogSpy ) . toHaveBeenCalledWith (
191244 expect . stringContaining ( 'Deploying "build" to GitHub Pages (branch: pages)' )
192245 ) ;
@@ -198,7 +251,11 @@ describe('runDeployToGitHubPages', () => {
198251 scripts : { build : 'webpack' } ,
199252 } ) ;
200253 mockExeca
201- . mockResolvedValueOnce ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > )
254+ . mockResolvedValueOnce ( {
255+ stdout : 'https://github.com/user/repo.git' ,
256+ stderr : '' ,
257+ exitCode : 0 ,
258+ } as Awaited < ReturnType < typeof execa > > )
202259 . mockRejectedValueOnce ( new Error ( 'Build failed' ) ) ;
203260
204261 await expect ( runDeployToGitHubPages ( cwd , { skipBuild : false } ) ) . rejects . toThrow (
@@ -209,13 +266,18 @@ describe('runDeployToGitHubPages', () => {
209266
210267 it ( 'propagates gh-pages deploy failure' , async ( ) => {
211268 setupPathExists ( { 'package.json' : true , 'dist' : true } ) ;
212- mockExeca
213- . mockResolvedValueOnce ( { stdout : '' , stderr : '' , exitCode : 0 } as Awaited < ReturnType < typeof execa > > )
214- . mockRejectedValueOnce ( new Error ( 'Deploy failed' ) ) ;
269+ mockExeca . mockResolvedValue ( {
270+ stdout : 'https://github.com/user/repo.git' ,
271+ stderr : '' ,
272+ exitCode : 0 ,
273+ } as Awaited < ReturnType < typeof execa > > ) ;
274+ mockGhPagesPublish . mockImplementation ( ( _dir , _opts , cb ) =>
275+ cb ( new Error ( 'Deploy failed' ) )
276+ ) ;
215277
216278 await expect ( runDeployToGitHubPages ( cwd , { skipBuild : true } ) ) . rejects . toThrow (
217279 'Deploy failed'
218280 ) ;
219- expect ( mockExeca ) . toHaveBeenCalledTimes ( 2 ) ; // git, gh-pages
281+ expect ( mockExeca ) . toHaveBeenCalledTimes ( 1 ) ; // git only
220282 } ) ;
221283} ) ;
0 commit comments