1- import { symlink } from 'fs/promises'
1+ import { symlink , rm } from 'fs/promises'
22import { symLinkConfig } from '../symLinkConfig'
3+ import { fileExists } from '../fileExists'
34
45jest . mock ( 'fs/promises' )
6+ jest . mock ( '../fileExists' )
57
68// suppress console.log so that it doesn't clutter the test output
79jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
810
9- it ( 'should create a symlink successfully' , async ( ) => {
11+ it ( 'should create a symlink successfully when one does not exist' , async ( ) => {
12+ ; ( fileExists as jest . Mock ) . mockResolvedValue ( false )
1013 ; ( symlink as jest . Mock ) . mockResolvedValue ( undefined )
1114
1215 await symLinkConfig ( '/astro' , '/consumer' )
@@ -17,11 +20,36 @@ it('should create a symlink successfully', async () => {
1720 )
1821} )
1922
23+ it ( 'should not try to remove a file that does not exist' , async ( ) => {
24+ ; ( fileExists as jest . Mock ) . mockResolvedValue ( false )
25+ ; ( symlink as jest . Mock ) . mockResolvedValue ( undefined )
26+
27+ await symLinkConfig ( '/astro' , '/consumer' )
28+
29+ expect ( rm ) . not . toHaveBeenCalled ( )
30+ } )
31+
32+ it ( 'should remove existing file before creating symlink' , async ( ) => {
33+ ; ( fileExists as jest . Mock ) . mockResolvedValue ( true )
34+ ; ( rm as jest . Mock ) . mockResolvedValue ( undefined )
35+ ; ( symlink as jest . Mock ) . mockResolvedValue ( undefined )
36+
37+ await symLinkConfig ( '/astro' , '/consumer' )
38+
39+ expect ( fileExists ) . toHaveBeenCalledWith ( '/astro/pf-docs.config.mjs' )
40+ expect ( rm ) . toHaveBeenCalledWith ( '/astro/pf-docs.config.mjs' )
41+ expect ( symlink ) . toHaveBeenCalledWith (
42+ '/consumer/pf-docs.config.mjs' ,
43+ '/astro/pf-docs.config.mjs' ,
44+ )
45+ } )
46+
2047it ( 'should log an error if symlink creation fails' , async ( ) => {
2148 const consoleErrorSpy = jest
2249 . spyOn ( console , 'error' )
2350 . mockImplementation ( ( ) => { } )
2451
52+ ; ( fileExists as jest . Mock ) . mockResolvedValue ( false )
2553 const error = new Error ( 'Symlink creation failed' )
2654 ; ( symlink as jest . Mock ) . mockRejectedValue ( error )
2755
@@ -36,6 +64,9 @@ it('should log an error if symlink creation fails', async () => {
3664it ( 'should log a success message after creating the symlink' , async ( ) => {
3765 const consoleLogSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
3866
67+ ; ( fileExists as jest . Mock ) . mockResolvedValue ( false )
68+ ; ( symlink as jest . Mock ) . mockResolvedValue ( undefined )
69+
3970 await symLinkConfig ( '/astro' , '/consumer' )
4071
4172 expect ( consoleLogSpy ) . toHaveBeenCalledWith (
0 commit comments