|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
1 | 4 | import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' |
2 | 5 | import { loadTempestConfiguration } from './config' |
3 | 6 | import { mockTempestConfiguration } from './test-utils' |
4 | | -import fs from 'node:fs' |
5 | | -import path from 'node:path' |
6 | 7 | import * as utils from './utils' |
7 | 8 |
|
8 | | -test('the configuration can be loaded', async () => { |
9 | | - mockTempestConfiguration() |
| 9 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
10 | 10 |
|
11 | | - const config = await loadTempestConfiguration() |
| 11 | +describe('loading configuration via command or environment variable', () => { |
| 12 | + afterEach(() => { |
| 13 | + vi.restoreAllMocks() |
| 14 | + delete process.env.TEMPEST_PLUGIN_CONFIGURATION_OVERRIDE |
| 15 | + }) |
12 | 16 |
|
13 | | - expect(config).toHaveProperty('build_directory') |
14 | | - expect(config).toHaveProperty('bridge_file_name') |
15 | | - expect(config).toHaveProperty('manifest') |
16 | | - expect(config).toHaveProperty('entrypoints') |
17 | | -}) |
| 17 | + test('the configuration can be loaded', async () => { |
| 18 | + mockTempestConfiguration() |
18 | 19 |
|
19 | | -test('the configuration can be overriden', async () => { |
20 | | - process.env.TEMPEST_PLUGIN_CONFIGURATION_OVERRIDE = JSON.stringify({ |
21 | | - build_directory: 'build', |
22 | | - bridge_file_name: 'vite-tempest', |
23 | | - manifest: 'manifest.json', |
24 | | - entrypoints: [], |
| 20 | + const config = await loadTempestConfiguration() |
| 21 | + expect(config.build_directory).toBe('build') |
25 | 22 | }) |
26 | 23 |
|
27 | | - const config = await loadTempestConfiguration() |
| 24 | + test('the configuration can be overriden with an environment variable', async () => { |
| 25 | + const execSpy = vi.spyOn(utils, 'exec') |
| 26 | + process.env.TEMPEST_PLUGIN_CONFIGURATION_OVERRIDE = JSON.stringify({ |
| 27 | + build_directory: 'build/from-env', |
| 28 | + bridge_file_name: 'vite-tempest', |
| 29 | + manifest: 'manifest.json', |
| 30 | + entrypoints: [], |
| 31 | + }) |
28 | 32 |
|
29 | | - expect(config).toHaveProperty('build_directory') |
30 | | - expect(config).toHaveProperty('bridge_file_name') |
31 | | - expect(config).toHaveProperty('manifest') |
32 | | - expect(config).toHaveProperty('entrypoints') |
| 33 | + const config = await loadTempestConfiguration() |
| 34 | + |
| 35 | + expect(config.build_directory).toBe('build/from-env') |
| 36 | + expect(execSpy).not.toHaveBeenCalled() |
| 37 | + }) |
33 | 38 | }) |
34 | 39 |
|
35 | 40 | describe('loading configuration from a file', () => { |
36 | | - const tempConfigPath = path.join(__dirname, 'tempest-vite.test.json'); |
| 41 | + const tempConfigPath = path.join(__dirname, 'tempest-vite.test.json') |
37 | 42 | const mockConfig = { |
38 | 43 | build_directory: 'build/from-file', |
39 | 44 | bridge_file_name: 'test-bridge', |
40 | 45 | manifest: 'test-manifest.json', |
41 | 46 | entrypoints: ['resources/js/test.js'], |
42 | | - }; |
| 47 | + } |
43 | 48 |
|
44 | 49 | beforeEach(() => { |
45 | | - fs.writeFileSync(tempConfigPath, JSON.stringify(mockConfig)); |
46 | | - }); |
| 50 | + fs.writeFileSync(tempConfigPath, JSON.stringify(mockConfig)) |
| 51 | + }) |
47 | 52 |
|
48 | 53 | afterEach(() => { |
49 | | - fs.rmSync(tempConfigPath); |
50 | | - vi.restoreAllMocks(); |
51 | | - }); |
| 54 | + fs.rmSync(tempConfigPath) |
| 55 | + vi.restoreAllMocks() |
| 56 | + }) |
52 | 57 |
|
53 | 58 | test('it loads configuration from the specified file path', async () => { |
54 | 59 | const config = await loadTempestConfiguration({ |
55 | 60 | config_file_path: tempConfigPath, |
56 | | - }); |
| 61 | + }) |
57 | 62 |
|
58 | | - expect(config.build_directory).toBe('build/from-file'); |
59 | | - expect(config.entrypoints).toEqual(['resources/js/test.js']); |
60 | | - }); |
| 63 | + expect(config.build_directory).toBe('build/from-file') |
| 64 | + expect(config.entrypoints).toEqual(['resources/js/test.js']) |
| 65 | + }) |
61 | 66 |
|
62 | 67 | test('it does not execute the php command when a file path is provided', async () => { |
63 | | - const execSpy = vi.spyOn(utils, 'exec'); |
| 68 | + const execSpy = vi.spyOn(utils, 'exec') |
64 | 69 |
|
65 | 70 | await loadTempestConfiguration({ |
66 | 71 | config_file_path: tempConfigPath, |
67 | | - }); |
| 72 | + }) |
68 | 73 |
|
69 | | - expect(execSpy).not.toHaveBeenCalled(); |
70 | | - }); |
71 | | -}); |
| 74 | + expect(execSpy).not.toHaveBeenCalled() |
| 75 | + }) |
| 76 | +}) |
0 commit comments