diff --git a/packages/toolkit/src/utils/helpers.js b/packages/toolkit/src/utils/helpers.js index 1ff5476..7af1393 100644 --- a/packages/toolkit/src/utils/helpers.js +++ b/packages/toolkit/src/utils/helpers.js @@ -29,6 +29,7 @@ export const runCommand = ( tool, files, globs, args = [] ) => { ); child.on( 'exit', ( code ) => { + process.exitCode = code ?? 1; if ( code === 0 ) { log.success( `Completed ${ tool } successfully.` ); } else { diff --git a/packages/toolkit/tests/helpers.test.js b/packages/toolkit/tests/helpers.test.js new file mode 100644 index 0000000..81e8f57 --- /dev/null +++ b/packages/toolkit/tests/helpers.test.js @@ -0,0 +1,44 @@ +import { beforeEach, afterEach, describe, expect, test, vi } from 'vitest'; +import { spawn } from 'child_process'; +import { runCommand } from '../src/utils/helpers.js'; + +vi.mock( 'child_process', () => ( { + exec: vi.fn(), + spawn: vi.fn(), +} ) ); + +vi.mock( '../src/utils/logger.js', () => ( { + default: { + info: vi.fn(), + success: vi.fn(), + error: vi.fn(), + }, +} ) ); + +describe( 'runCommand', () => { + let exitHandler; + + beforeEach( () => { + spawn.mockReturnValue( { + on: vi.fn( ( event, handler ) => { + if ( event === 'exit' ) exitHandler = handler; + } ), + } ); + } ); + + afterEach( () => { + process.exitCode = undefined; + vi.clearAllMocks(); + } ); + + test.each( [ + [ 0, 0 ], + [ 1, 1 ], + [ null, 1 ], + ] )( 'sets process.exitCode to %s when the child exits', ( code, expected ) => { + runCommand( 'eslint', [], [] ); + exitHandler( code ); + + expect( process.exitCode ).toBe( expected ); + } ); +} ); diff --git a/packages/toolkit/vitest.config.mjs b/packages/toolkit/vitest.config.mjs new file mode 100644 index 0000000..add21f1 --- /dev/null +++ b/packages/toolkit/vitest.config.mjs @@ -0,0 +1,9 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig( { + test: { + globals: true, + environment: 'node', + include: [ 'tests/**/*.test.js' ], + }, +} ); diff --git a/vitest.config.mjs b/vitest.config.mjs index 4a07609..d82281c 100644 --- a/vitest.config.mjs +++ b/vitest.config.mjs @@ -5,6 +5,7 @@ export default defineConfig( { projects: [ 'packages/eslint-config/vitest.config.mjs', 'packages/prettier-config/vitest.config.mjs', + 'packages/toolkit/vitest.config.mjs', 'packages/wp-scripts-config/vitest.config.mjs', ], },