1- import {
2- askForToolConfigPath ,
3- createNewConfigFile ,
4- installPackage ,
5- } from '../../src/utils/clack-utils' ;
1+ import { installPackage } from '../../src/utils/clack-utils' ;
62
7- import * as fs from 'node:fs' ;
83import * as ChildProcess from 'node:child_process' ;
94import type { PackageManager } from '../../src/utils/package-manager' ;
10- import clack from '../../src/utils/clack' ;
115
126jest . mock ( 'node:child_process' , ( ) => ( {
137 __esModule : true ,
@@ -32,125 +26,7 @@ jest.mock('@clack/prompts', () => ({
3226 . mockImplementation ( ( ) => ( { start : jest . fn ( ) , stop : jest . fn ( ) } ) ) ,
3327} ) ) ;
3428
35- function mockUserResponse ( fn : jest . Mock , response : any ) {
36- fn . mockReturnValueOnce ( response ) ;
37- }
38-
39- describe ( 'askForToolConfigPath' , ( ) => {
40- afterEach ( ( ) => {
41- jest . clearAllMocks ( ) ;
42- } ) ;
43-
44- it ( 'returns undefined if users have no config file' , async ( ) => {
45- mockUserResponse ( clack . confirm as jest . Mock , false ) ;
46-
47- const result = await askForToolConfigPath ( 'Webpack' , 'webpack.config.js' ) ;
48-
49- expect ( clack . confirm ) . toHaveBeenCalledWith (
50- expect . objectContaining ( {
51- message : expect . stringContaining ( 'have a Webpack config file' ) ,
52- } ) ,
53- ) ;
54-
55- expect ( result ) . toBeUndefined ( ) ;
56- } ) ;
57-
58- it ( 'returns the path if users have a config file and the entered path is valid' , async ( ) => {
59- mockUserResponse ( clack . confirm as jest . Mock , true ) ;
60- mockUserResponse ( clack . text as jest . Mock , 'my.webpack.config.js' ) ;
61-
62- const result = await askForToolConfigPath ( 'Webpack' , 'webpack.config.js' ) ;
63-
64- expect ( clack . confirm ) . toHaveBeenCalledWith (
65- expect . objectContaining ( {
66- message : expect . stringContaining ( 'have a Webpack config file' ) ,
67- } ) ,
68- ) ;
69-
70- expect ( clack . text ) . toHaveBeenCalledWith (
71- expect . objectContaining ( {
72- message : expect . stringContaining (
73- 'enter the path to your Webpack config file' ,
74- ) ,
75- } ) ,
76- ) ;
77-
78- expect ( result ) . toBe ( 'my.webpack.config.js' ) ;
79- } ) ;
80- } ) ;
81-
82- describe ( 'createNewConfigFile' , ( ) => {
83- afterEach ( ( ) => {
84- jest . clearAllMocks ( ) ;
85- } ) ;
86-
87- it ( 'writes the file to disk and returns true if the file was created successfully' , async ( ) => {
88- const writeFileSpy = jest
89- . spyOn ( fs . promises , 'writeFile' )
90- . mockImplementation ( jest . fn ( ) ) ;
91-
92- const filename = '/webpack.config.js' ;
93- const code = 'module.exports = {/*config...*/}' ;
94-
95- const result = await createNewConfigFile ( filename , code , {
96- installDir : process . cwd ( ) ,
97- } ) ;
98-
99- expect ( result ) . toBe ( true ) ;
100- expect ( writeFileSpy ) . toHaveBeenCalledWith ( filename , code ) ;
101- } ) ;
102-
103- it ( 'logs more information if provided as an argument' , async ( ) => {
104- jest . spyOn ( fs . promises , 'writeFile' ) . mockImplementation ( jest . fn ( ) ) ;
105-
106- const filename = '/webpack.config.js' ;
107- const code = 'module.exports = {/*config...*/}' ;
108- const moreInfo = 'More information...' ;
109-
110- await createNewConfigFile (
111- filename ,
112- code ,
113- {
114- installDir : process . cwd ( ) ,
115- } ,
116- moreInfo ,
117- ) ;
118-
119- expect ( clack . log . info ) . toHaveBeenCalledTimes ( 1 ) ;
120- expect ( clack . log . info ) . toHaveBeenCalledWith (
121- expect . stringContaining ( moreInfo ) ,
122- ) ;
123- } ) ;
124-
125- it ( 'returns false and logs a warning if the file could not be created' , async ( ) => {
126- const writeFileSpy = jest
127- . spyOn ( fs . promises , 'writeFile' )
128- . mockImplementation ( ( ) => Promise . reject ( new Error ( 'Could not write' ) ) ) ;
129-
130- const filename = '/webpack.config.js' ;
131- const code = 'module.exports = {/*config...*/}' ;
132-
133- const result = await createNewConfigFile ( filename , code , {
134- installDir : process . cwd ( ) ,
135- } ) ;
136-
137- expect ( result ) . toBe ( false ) ;
138- expect ( writeFileSpy ) . toHaveBeenCalledWith ( filename , code ) ;
139- expect ( clack . log . warn ) . toHaveBeenCalledTimes ( 1 ) ;
140- } ) ;
141-
142- it ( 'returns false if the passed path is not absolute' , async ( ) => {
143- const result = await createNewConfigFile (
144- './relative/webpack.config.js' ,
145- '' ,
146- { installDir : process . cwd ( ) } ,
147- ) ;
148-
149- expect ( result ) . toBe ( false ) ;
150- } ) ;
151- } ) ;
152-
153- describe ( 'installPackage' , ( ) => {
29+ describe . skip ( 'installPackage' , ( ) => {
15430 afterEach ( ( ) => {
15531 jest . clearAllMocks ( ) ;
15632 } ) ;
0 commit comments