/
load-config.js
120 lines (102 loc) 路 2.83 KB
/
load-config.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const path = require('path');
const tap = require('tap');
const loadConfig = require('../lib/load-config');
const {test} = tap;
tap.afterEach(done => {
process.chdir(path.resolve(__dirname, '..'));
done();
});
const changeDir = fixtureDir => {
process.chdir(path.resolve(__dirname, 'fixture', 'load-config', fixtureDir));
};
test('finds config in package.json', t => {
changeDir('package-only');
const conf = loadConfig();
t.is(conf.failFast, true);
t.end();
});
test('loads config from a particular directory', t => {
changeDir('throws');
const conf = loadConfig(path.resolve(__dirname, 'fixture', 'load-config', 'package-only'));
t.is(conf.failFast, true);
t.end();
});
test('throws a warning of both configs are present', t => {
changeDir('package-yes-file-yes');
t.throws(loadConfig);
t.end();
});
test('merges in defaults passed with initial call', t => {
changeDir('package-only');
const defaults = {
files: ['123', '!456']
};
const {files, failFast} = loadConfig(undefined, defaults);
t.is(failFast, true, 'preserves original props');
t.is(files, defaults.files, 'merges in extra props');
t.end();
});
test('loads config from file with `export default` syntax', t => {
changeDir('package-no-file-yes');
const conf = loadConfig();
t.is(conf.files, 'config-file-esm-test-value');
t.end();
});
test('loads config from factory function', t => {
changeDir('package-no-file-yes-factory');
const conf = loadConfig();
t.ok(conf.files.startsWith(__dirname));
t.end();
});
test('supports require() inside config file', t => {
changeDir('require');
const conf = loadConfig();
t.is(conf.files, 'config-file-cjs-test-value');
t.end();
});
test('throws an error if a config factory returns a promise', t => {
changeDir('factory-no-promise-return');
t.throws(loadConfig);
t.end();
});
test('throws an error if a config exports a promise', t => {
changeDir('no-promise-config');
t.throws(loadConfig);
t.end();
});
test('throws an error if a config factory does not return a plain object', t => {
changeDir('factory-no-plain-return');
t.throws(loadConfig);
t.end();
});
test('throws an error if a config does not export a plain object', t => {
changeDir('no-plain-config');
t.throws(loadConfig);
t.end();
});
test('receives a `projectDir` property', t => {
changeDir('package-only');
const conf = loadConfig();
t.ok(conf.projectDir.startsWith(__dirname));
t.end();
});
test('rethrows wrapped module errors', t => {
t.plan(1);
changeDir('throws');
try {
loadConfig();
} catch (error) {
t.is(error.parent.message, 'foo');
}
});
test('throws an error if a config file has no default export', t => {
changeDir('no-default-export');
t.throws(loadConfig);
t.end();
});
test('throws an error if a config file contains `ava` property', t => {
changeDir('contains-ava-property');
t.throws(loadConfig);
t.end();
});