|
1 | | -import { describe, expect, test } from 'vitest'; |
| 1 | +import { Command } from 'commander'; |
| 2 | +import consola from 'consola'; |
| 3 | +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest'; |
2 | 4 |
|
3 | | -import { createFilter } from '../../src/commands/build'; |
| 5 | +import { build, createFilter } from '../../src/commands/build'; |
| 6 | +import { program } from '../../src/program'; |
| 7 | + |
| 8 | +const cleanup = vi.fn(); |
| 9 | + |
| 10 | +beforeAll(() => { |
| 11 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 12 | + vi.spyOn(process, 'exit').mockImplementation(() => null as never); |
| 13 | + |
| 14 | + consola.wrapAll(); |
| 15 | + |
| 16 | + vi.mock('../../src/lib/mk-temp-dir', () => ({ |
| 17 | + mkTempDir: vi.fn(() => ['/tmp/test', cleanup]), |
| 18 | + })); |
| 19 | + |
| 20 | + vi.mock('node:fs/promises', () => ({ |
| 21 | + cp: vi.fn(), |
| 22 | + })); |
| 23 | + |
| 24 | + vi.mock('nypm', () => ({ |
| 25 | + installDependencies: vi.fn(), |
| 26 | + addDevDependency: vi.fn(), |
| 27 | + runScript: vi.fn(), |
| 28 | + })); |
| 29 | + |
| 30 | + vi.mock('execa', () => ({ |
| 31 | + execa: vi.fn(), |
| 32 | + })); |
| 33 | +}); |
| 34 | + |
| 35 | +beforeEach(() => { |
| 36 | + consola.mockTypes(() => vi.fn()); |
| 37 | +}); |
| 38 | + |
| 39 | +afterEach(() => { |
| 40 | + vi.clearAllMocks(); |
| 41 | +}); |
| 42 | + |
| 43 | +afterAll(() => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | +}); |
| 46 | + |
| 47 | +test('properly configured Command instance', () => { |
| 48 | + expect(build).toBeInstanceOf(Command); |
| 49 | + expect(build.name()).toBe('build'); |
| 50 | + expect(build.options).toEqual( |
| 51 | + expect.arrayContaining([expect.objectContaining({ flags: '--keep-temp-dir' })]), |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +test('does not remove temp dir if --keep-temp-dir is passed', async () => { |
| 56 | + await program.parseAsync(['node', 'catalyst', 'build', '--keep-temp-dir']); |
| 57 | + expect(cleanup).not.toHaveBeenCalled(); |
| 58 | +}); |
| 59 | + |
| 60 | +test('removes temp dir if --keep-temp-dir is not passed', async () => { |
| 61 | + await program.parseAsync(['node', 'catalyst', 'build']); |
| 62 | + expect(cleanup).toHaveBeenCalled(); |
| 63 | +}); |
| 64 | + |
| 65 | +test('successfully builds project', async () => { |
| 66 | + await program.parseAsync(['node', 'catalyst', 'build']); |
| 67 | + |
| 68 | + expect(consola.success).toHaveBeenCalledWith('Project built'); |
| 69 | + expect(consola.success).toHaveBeenCalledWith('Build copied to project'); |
| 70 | +}); |
| 71 | + |
| 72 | +test('handles error if cp throws during build', async () => { |
| 73 | + // Dynamically mock cp for this test only |
| 74 | + // eslint-disable-next-line import/dynamic-import-chunkname |
| 75 | + const cpModule = await import('node:fs/promises'); |
| 76 | + const originalCp = cpModule.cp; |
| 77 | + const cpMock = vi.fn(() => { |
| 78 | + throw new Error('cp failed'); |
| 79 | + }); |
| 80 | + |
| 81 | + cpModule.cp = cpMock; |
| 82 | + |
| 83 | + await program.parseAsync(['node', 'catalyst', 'build']); |
| 84 | + |
| 85 | + expect(consola.error).toHaveBeenCalledWith(expect.any(Error)); |
| 86 | + |
| 87 | + cpModule.cp = originalCp; |
| 88 | +}); |
4 | 89 |
|
5 | 90 | describe('createFilter', () => { |
6 | 91 | const ROOT = '/my/project'; |
|
0 commit comments