|
1 | 1 | import { Command } from 'commander'; |
2 | | -import consola from 'consola'; |
3 | | -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 2 | +import { expect, test } from 'vitest'; |
4 | 3 |
|
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('execa', () => ({ |
25 | | - execa: vi.fn(), |
26 | | - })); |
27 | | -}); |
28 | | - |
29 | | -beforeEach(() => { |
30 | | - consola.mockTypes(() => vi.fn()); |
31 | | -}); |
32 | | - |
33 | | -afterEach(() => { |
34 | | - vi.clearAllMocks(); |
35 | | -}); |
36 | | - |
37 | | -afterAll(() => { |
38 | | - vi.restoreAllMocks(); |
39 | | -}); |
| 4 | +import { build } from '../../src/commands/build'; |
40 | 5 |
|
41 | 6 | test('properly configured Command instance', () => { |
42 | 7 | expect(build).toBeInstanceOf(Command); |
43 | 8 | expect(build.name()).toBe('build'); |
44 | 9 | expect(build.options).toEqual( |
45 | 10 | expect.arrayContaining([ |
46 | | - expect.objectContaining({ long: '--keep-temp-dir' }), |
47 | 11 | expect.objectContaining({ long: '--framework' }), |
48 | 12 | expect.objectContaining({ long: '--project-uuid' }), |
49 | 13 | ]), |
50 | 14 | ); |
51 | 15 | }); |
52 | | - |
53 | | -test('does not remove temp dir if --keep-temp-dir is passed', async () => { |
54 | | - await program.parseAsync(['node', 'catalyst', 'build', '--keep-temp-dir']); |
55 | | - expect(cleanup).not.toHaveBeenCalled(); |
56 | | -}); |
57 | | - |
58 | | -test('removes temp dir if --keep-temp-dir is not passed', async () => { |
59 | | - await program.parseAsync(['node', 'catalyst', 'build']); |
60 | | - expect(cleanup).toHaveBeenCalled(); |
61 | | -}); |
62 | | - |
63 | | -test('successfully builds project', async () => { |
64 | | - await program.parseAsync(['node', 'catalyst', 'build']); |
65 | | - |
66 | | - expect(consola.success).toHaveBeenCalledWith('Project built'); |
67 | | - expect(consola.success).toHaveBeenCalledWith('Build copied to project'); |
68 | | -}); |
69 | | - |
70 | | -test('handles error if cp throws during build', async () => { |
71 | | - // Dynamically mock cp for this test only |
72 | | - // eslint-disable-next-line import/dynamic-import-chunkname |
73 | | - const cpModule = await import('node:fs/promises'); |
74 | | - const originalCp = cpModule.cp; |
75 | | - const cpMock = vi.fn(() => { |
76 | | - throw new Error('cp failed'); |
77 | | - }); |
78 | | - |
79 | | - cpModule.cp = cpMock; |
80 | | - |
81 | | - await program.parseAsync(['node', 'catalyst', 'build']); |
82 | | - |
83 | | - expect(consola.error).toHaveBeenCalledWith(expect.any(Error)); |
84 | | - |
85 | | - cpModule.cp = originalCp; |
86 | | -}); |
87 | | - |
88 | | -describe('createFilter', () => { |
89 | | - const ROOT = '/my/project'; |
90 | | - const SKIP_DIRS = new Set(['node_modules', '.git', 'dist']); |
91 | | - |
92 | | - const filter = createFilter(ROOT, SKIP_DIRS); |
93 | | - |
94 | | - test('allows files not in skip list', () => { |
95 | | - expect(filter('/my/project/src/index.ts')).toBe(true); |
96 | | - }); |
97 | | - |
98 | | - test('skips files inside a skipped directory', () => { |
99 | | - expect(filter('/my/project/node_modules/lodash/index.js')).toBe(false); |
100 | | - expect(filter('/my/project/.git/config')).toBe(false); |
101 | | - expect(filter('/my/project/dist/main.js')).toBe(false); |
102 | | - }); |
103 | | - |
104 | | - test('handles nested skipped folders', () => { |
105 | | - expect(filter('/my/project/src/node_modules/whatever.js')).toBe(false); |
106 | | - }); |
107 | | -}); |
0 commit comments