-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathconfig.test.ts
68 lines (57 loc) · 2.44 KB
/
config.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// tests/config.test.ts
// Tests for the configuration loader.
import { afterEach, describe, test, expect, vi } from 'vitest';
import { loadConfiguration } from '../source/utilities/config.js';
import { logger } from '../source/utilities/logger.js';
import { Options } from '../source/types.js';
// The path to the fixtures for this test file.
const fixtures = 'tests/__fixtures__/config/';
// A helper function to load the configuration for a certain fixture.
const loadConfig = (
name: 'valid' | 'invalid' | 'non-existent' | 'deprecated',
args?: Partial<Options> = {},
) => loadConfiguration(process.cwd(), `${fixtures}/${name}`, args);
afterEach(() => {
vi.restoreAllMocks();
});
describe('utilities/config', () => {
// Make sure the configuration is parsed correctly when it is in the
// `serve.json` file.
test('parse valid config', async () => {
const configuration = await loadConfig('valid');
expect(configuration).toMatchSnapshot();
});
// Make sure the configuration is parsed correctly when it is a location
// specified by the `--config` option.
test('parse valid config at custom location', async () => {
const configuration = await loadConfig('custom', {
'--config': 'config.json',
});
expect(configuration).toMatchSnapshot();
});
// When the configuration in the file is invalid, the function will throw an
// error.
test('throw error if config is invalid', async () => {
loadConfig('invalid').catch((error: Error) => {
expect(error.message).toMatch(/invalid/);
});
});
// When no configuration file exists, the configuration should be populated
// with the `etag` and `symlink` options set to their default values, and
// the `public` option set to the path of the directory.
test('return default configuration when no source is found', async () => {
const configuration = await loadConfig('non-existent');
expect(configuration).toMatchSnapshot();
});
// When the configuration source is deprecated, i.e., the configuration lives
// in `now.json` or `package.json`, a warning should be printed.
test('warn when configuration comes from a deprecated source', async () => {
const consoleSpy = vi.spyOn(logger, 'warn');
const configuration = await loadConfig('deprecated');
expect(configuration).toMatchSnapshot();
expect(consoleSpy).toHaveBeenCalledOnce();
expect(consoleSpy).toHaveBeenLastCalledWith(
expect.stringContaining('deprecated'),
);
});
});