forked from cosmiconfig/cosmiconfig
/
index.test.js
112 lines (97 loc) · 3.31 KB
/
index.test.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
'use strict';
// Mock `createExplorer` because we want to check what it is called with.
jest.mock('../src/createExplorer');
const os = require('os');
const cosmiconfig = require('../src');
const util = require('./util');
const createExplorerMock = require('../src/createExplorer');
const loaders = require('../src/loaders');
const temp = new util.TempDir();
describe('cosmiconfig', () => {
const moduleName = 'foo';
beforeEach(() => {
temp.clean();
});
afterEach(() => {
// Clean up a mock's usage data between tests
jest.clearAllMocks();
jest.restoreAllMocks();
});
afterAll(() => {
// Remove temp.dir created for tests
temp.deleteTempDir();
});
it('creates explorer with default options if not specified', () => {
cosmiconfig(moduleName);
expect(createExplorerMock).toHaveBeenCalledTimes(1);
const explorerOptions = createExplorerMock.mock.calls[0][0];
expect(explorerOptions).toMatchObject({
packageFilenames: ['package.json'],
packageProp: moduleName,
searchPlaces: [
'package.json',
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`${moduleName}.config.js`,
],
ignoreEmptySearchPlaces: true,
stopDir: os.homedir(),
cache: true,
loaders: {
'.js': { sync: loaders.loadJs, async: loaders.loadJs },
'.json': { sync: loaders.loadJson, async: loaders.loadJson },
'.yaml': { sync: loaders.loadYaml, async: loaders.loadYaml },
'.yml': { sync: loaders.loadYaml, async: loaders.loadYaml },
noExt: { sync: loaders.loadYaml, async: loaders.loadYaml },
},
transform: expect.any(Function),
});
});
it('defaults transform to identity function', () => {
cosmiconfig(moduleName);
const explorerOptions = createExplorerMock.mock.calls[0][0];
const x = {};
expect(explorerOptions.transform(x)).toBe(x);
});
it('creates explorer with preference for given options over defaults', () => {
temp.createFile('foo.json', '{ "foo": true }');
const noExtLoader = () => {};
const jsLoader = () => {};
const jsonLoader = () => {};
const yamlLoader = () => {};
cosmiconfig(moduleName, {
stopDir: __dirname,
cache: false,
searchPlaces: ['.foorc.json', 'wildandfree.js'],
packageFilenames: ['manifest.json'],
packageProp: 'wildandfree',
ignoreEmptySearchPlaces: false,
loaders: {
noExt: noExtLoader,
'.js': { async: jsLoader },
'.json': { sync: jsonLoader },
'.yaml': { sync: yamlLoader, async: yamlLoader },
},
});
const explorerOptions = createExplorerMock.mock.calls[0][0];
expect(explorerOptions).toMatchObject({
packageFilenames: ['manifest.json'],
packageProp: 'wildandfree',
searchPlaces: ['.foorc.json', 'wildandfree.js'],
ignoreEmptySearchPlaces: false,
stopDir: __dirname,
cache: false,
loaders: {
'.js': { async: jsLoader },
'.json': { sync: jsonLoader },
'.yaml': { sync: yamlLoader, async: yamlLoader },
'.yml': { sync: loaders.loadYaml, async: loaders.loadYaml },
noExt: { sync: noExtLoader, async: noExtLoader },
},
transform: expect.any(Function),
});
});
});