1- import { spawn } from ' child_process' ;
2- import fs from 'fs' ;
3- import os from 'os' ;
4- import path from ' path' ;
5- import { afterEach , beforeEach , describe , expect , it , vi } from ' vitest' ;
6- import { enumify } from ' ../src/vite-plugin-enumify' ;
7-
8- vi . mock ( ' child_process' , ( ) => ( {
9- spawn : vi . fn ( ) ,
1+ import { spawn } from " child_process" ;
2+ import fs from "fs" ;
3+ import os from "os" ;
4+ import path from " path" ;
5+ import { afterEach , beforeEach , describe , expect , it , vi } from " vitest" ;
6+ import { enumify } from " ../src/vite-plugin-enumify" ;
7+
8+ vi . mock ( " child_process" , ( ) => ( {
9+ spawn : vi . fn ( ) ,
1010} ) ) ;
1111
1212const spawnMock = spawn as unknown as ReturnType < typeof vi . fn > ;
1313
1414function createTempProject ( ) : string {
15- const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , ' enumify-' ) ) ;
16- fs . mkdirSync ( path . join ( tempDir , ' config' ) , { recursive : true } ) ;
17- return tempDir ;
15+ const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , " enumify-" ) ) ;
16+ fs . mkdirSync ( path . join ( tempDir , " config" ) , { recursive : true } ) ;
17+ return tempDir ;
1818}
1919
2020function writeEnumifyConfig ( cwd : string , contents : string ) : void {
21- fs . writeFileSync ( path . join ( cwd , ' config' , ' enumify.php' ) , contents , ' utf8' ) ;
21+ fs . writeFileSync ( path . join ( cwd , " config" , " enumify.php" ) , contents , " utf8" ) ;
2222}
2323
2424function createLogger ( ) {
25- return {
26- info : vi . fn ( ) ,
27- warn : vi . fn ( ) ,
28- error : vi . fn ( ) ,
29- hasWarned : false ,
30- hasErrorLogged : false ,
31- clearScreen : vi . fn ( ) ,
32- } ;
25+ return {
26+ info : vi . fn ( ) ,
27+ warn : vi . fn ( ) ,
28+ error : vi . fn ( ) ,
29+ hasWarned : false ,
30+ hasErrorLogged : false ,
31+ clearScreen : vi . fn ( ) ,
32+ } ;
3333}
3434
3535function mockSpawnExit ( code = 0 ) {
36- spawnMock . mockImplementation ( ( ) => ( {
37- on ( event : string , handler : ( value ?: unknown ) => void ) {
38- if ( event === ' close' ) {
39- handler ( code ) ;
40- }
41- if ( event === ' error' && code !== 0 ) {
42- handler ( new Error ( ' spawn error' ) ) ;
43- }
44- return this ;
45- } ,
46- } ) ) ;
36+ spawnMock . mockImplementation ( ( ) => ( {
37+ on ( event : string , handler : ( value ?: unknown ) => void ) {
38+ if ( event === " close" ) {
39+ handler ( code ) ;
40+ }
41+ if ( event === " error" && code !== 0 ) {
42+ handler ( new Error ( " spawn error" ) ) ;
43+ }
44+ return this ;
45+ } ,
46+ } ) ) ;
4747}
4848
49- describe ( ' vite-plugin-enumify' , ( ) => {
50- beforeEach ( ( ) => {
51- spawnMock . mockReset ( ) ;
52- mockSpawnExit ( 0 ) ;
53- } ) ;
54-
55- afterEach ( ( ) => {
56- vi . useRealTimers ( ) ;
57- } ) ;
58-
59- it ( ' runs enum sync on build with defaults' , async ( ) => {
60- const cwd = createTempProject ( ) ;
61- const plugin = enumify ( { cwd } ) ;
62- const logger = createLogger ( ) ;
63-
64- plugin . configResolved ?.( { command : ' build' , logger } as any ) ;
65- await plugin . buildStart ?. call ( { error : vi . fn ( ) } ) ;
66-
67- expect ( spawnMock ) . toHaveBeenCalledTimes ( 1 ) ;
68- expect ( spawnMock ) . toHaveBeenCalledWith (
69- ' php' ,
70- [ ' artisan' , ' enumify:sync' , ' --force' , ' --quiet' ] ,
71- expect . objectContaining ( {
72- cwd,
73- stdio : ' inherit' ,
74- } ) ,
75- ) ;
76- } ) ;
77-
78- it ( ' respects configured enum paths for watching' , ( ) => {
79- const cwd = createTempProject ( ) ;
80- writeEnumifyConfig (
81- cwd ,
82- `<?php
49+ describe ( " vite-plugin-enumify" , ( ) => {
50+ beforeEach ( ( ) => {
51+ spawnMock . mockReset ( ) ;
52+ mockSpawnExit ( 0 ) ;
53+ } ) ;
54+
55+ afterEach ( ( ) => {
56+ vi . useRealTimers ( ) ;
57+ } ) ;
58+
59+ it ( " runs enum sync on build with defaults" , async ( ) => {
60+ const cwd = createTempProject ( ) ;
61+ const plugin = enumify ( { cwd } ) ;
62+ const logger = createLogger ( ) ;
63+
64+ plugin . configResolved ?.( { command : " build" , logger } as any ) ;
65+ await plugin . buildStart ?. call ( { error : vi . fn ( ) } ) ;
66+
67+ expect ( spawnMock ) . toHaveBeenCalledTimes ( 1 ) ;
68+ expect ( spawnMock ) . toHaveBeenCalledWith (
69+ " php" ,
70+ [ " artisan" , " enumify:sync" , " --force" , " --quiet" ] ,
71+ expect . objectContaining ( {
72+ cwd,
73+ stdio : " inherit" ,
74+ } ) ,
75+ ) ;
76+ } ) ;
77+
78+ it ( " respects configured enum paths for watching" , ( ) => {
79+ const cwd = createTempProject ( ) ;
80+ writeEnumifyConfig (
81+ cwd ,
82+ `<?php
8383return [
8484 'paths' => [
8585 'enums' => ['app/Enums', 'domain/Enums'],
@@ -90,51 +90,55 @@ return [
9090 ],
9191];
9292` ,
93- ) ;
94-
95- const plugin = enumify ( { cwd } ) ;
96- const logger = createLogger ( ) ;
97- const watcher = { add : vi . fn ( ) } ;
98-
99- plugin . configResolved ?.( { command : 'serve' , logger } as any ) ;
100- plugin . configureServer ?.( { watcher } as any ) ;
101-
102- expect ( watcher . add ) . toHaveBeenCalledWith ( path . join ( cwd , 'app/Enums' ) ) ;
103- expect ( watcher . add ) . toHaveBeenCalledWith ( path . join ( cwd , 'domain/Enums' ) ) ;
104- } ) ;
105-
106- it ( 'ignores changes in output directory' , ( ) => {
107- vi . useFakeTimers ( ) ;
108- const cwd = createTempProject ( ) ;
109- const plugin = enumify ( { cwd } ) ;
110- const logger = createLogger ( ) ;
111-
112- plugin . configResolved ?.( { command : 'serve' , logger } as any ) ;
113- plugin . handleHotUpdate ?.( { file : path . join ( cwd , 'resources/js/enums/Status.ts' ) } as any ) ;
114-
115- vi . advanceTimersByTime ( 300 ) ;
116- expect ( spawnMock ) . not . toHaveBeenCalled ( ) ;
117- } ) ;
118-
119- it ( 'triggers sync on enum changes when watching' , ( ) => {
120- vi . useFakeTimers ( ) ;
121- const cwd = createTempProject ( ) ;
122- const plugin = enumify ( { cwd } ) ;
123- const logger = createLogger ( ) ;
124-
125- plugin . configResolved ?.( { command : 'serve' , logger } as any ) ;
126- plugin . handleHotUpdate ?.( { file : path . join ( cwd , 'app/Enums/Status.php' ) } as any ) ;
127-
128- vi . advanceTimersByTime ( 300 ) ;
129- expect ( spawnMock ) . toHaveBeenCalledTimes ( 1 ) ;
130- } ) ;
131-
132- it ( 'does not watch when runtime.watch is false' , ( ) => {
133- vi . useFakeTimers ( ) ;
134- const cwd = createTempProject ( ) ;
135- writeEnumifyConfig (
136- cwd ,
137- `<?php
93+ ) ;
94+
95+ const plugin = enumify ( { cwd } ) ;
96+ const logger = createLogger ( ) ;
97+ const watcher = { add : vi . fn ( ) } ;
98+
99+ plugin . configResolved ?.( { command : "serve" , logger } as any ) ;
100+ plugin . configureServer ?.( { watcher } as any ) ;
101+
102+ expect ( watcher . add ) . toHaveBeenCalledWith ( path . join ( cwd , "app/Enums" ) ) ;
103+ expect ( watcher . add ) . toHaveBeenCalledWith ( path . join ( cwd , "domain/Enums" ) ) ;
104+ } ) ;
105+
106+ it ( "ignores changes in output directory" , ( ) => {
107+ vi . useFakeTimers ( ) ;
108+ const cwd = createTempProject ( ) ;
109+ const plugin = enumify ( { cwd } ) ;
110+ const logger = createLogger ( ) ;
111+
112+ plugin . configResolved ?.( { command : "serve" , logger } as any ) ;
113+ plugin . handleHotUpdate ?.( {
114+ file : path . join ( cwd , "resources/js/enums/Status.ts" ) ,
115+ } as any ) ;
116+
117+ vi . advanceTimersByTime ( 300 ) ;
118+ expect ( spawnMock ) . not . toHaveBeenCalled ( ) ;
119+ } ) ;
120+
121+ it ( "triggers sync on enum changes when watching" , ( ) => {
122+ vi . useFakeTimers ( ) ;
123+ const cwd = createTempProject ( ) ;
124+ const plugin = enumify ( { cwd } ) ;
125+ const logger = createLogger ( ) ;
126+
127+ plugin . configResolved ?.( { command : "serve" , logger } as any ) ;
128+ plugin . handleHotUpdate ?.( {
129+ file : path . join ( cwd , "app/Enums/Status.php" ) ,
130+ } as any ) ;
131+
132+ vi . advanceTimersByTime ( 300 ) ;
133+ expect ( spawnMock ) . toHaveBeenCalledTimes ( 1 ) ;
134+ } ) ;
135+
136+ it ( "does not watch when runtime.watch is false" , ( ) => {
137+ vi . useFakeTimers ( ) ;
138+ const cwd = createTempProject ( ) ;
139+ writeEnumifyConfig (
140+ cwd ,
141+ `<?php
138142return [
139143 'paths' => [
140144 'enums' => ['app/Enums'],
@@ -145,18 +149,20 @@ return [
145149 ],
146150];
147151` ,
148- ) ;
149-
150- const plugin = enumify ( { cwd } ) ;
151- const logger = createLogger ( ) ;
152- const watcher = { add : vi . fn ( ) } ;
153-
154- plugin . configResolved ?.( { command : 'serve' , logger } as any ) ;
155- plugin . configureServer ?.( { watcher } as any ) ;
156- plugin . handleHotUpdate ?.( { file : path . join ( cwd , 'app/Enums/Status.php' ) } as any ) ;
157-
158- vi . advanceTimersByTime ( 300 ) ;
159- expect ( watcher . add ) . not . toHaveBeenCalled ( ) ;
160- expect ( spawnMock ) . not . toHaveBeenCalled ( ) ;
161- } ) ;
152+ ) ;
153+
154+ const plugin = enumify ( { cwd } ) ;
155+ const logger = createLogger ( ) ;
156+ const watcher = { add : vi . fn ( ) } ;
157+
158+ plugin . configResolved ?.( { command : "serve" , logger } as any ) ;
159+ plugin . configureServer ?.( { watcher } as any ) ;
160+ plugin . handleHotUpdate ?.( {
161+ file : path . join ( cwd , "app/Enums/Status.php" ) ,
162+ } as any ) ;
163+
164+ vi . advanceTimersByTime ( 300 ) ;
165+ expect ( watcher . add ) . not . toHaveBeenCalled ( ) ;
166+ expect ( spawnMock ) . not . toHaveBeenCalled ( ) ;
167+ } ) ;
162168} ) ;
0 commit comments