/
config.spec.js
98 lines (81 loc) · 3 KB
/
config.spec.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
'use strict';
// This is not a "functional" test; we aren't invoking the mocha executable.
// Instead we just avoid test doubles.
var fs = require('fs');
var path = require('path');
var loadConfig = require('../../lib/cli/config').loadConfig;
describe('config', function() {
it('should return the same values for all supported config types', function() {
var configDir = path.join(__dirname, 'fixtures', 'config');
var js = loadConfig(path.join(configDir, 'mocharc.js'));
var json = loadConfig(path.join(configDir, 'mocharc.json'));
var yaml = loadConfig(path.join(configDir, 'mocharc.yaml'));
expect(js, 'to equal', json);
expect(json, 'to equal', yaml);
});
describe('when configuring Mocha via a ".js" file', function() {
var projRootDir = path.join(__dirname, '..', '..');
var configDir = path.join(__dirname, 'fixtures', 'config');
var json = loadConfig(path.join(configDir, 'mocharc.json'));
it('should load configuration given absolute path', function() {
var js;
function _loadConfig() {
js = loadConfig(path.join(configDir, 'mocharc.js'));
}
expect(_loadConfig, 'not to throw');
expect(js, 'to equal', json);
});
it('should load configuration given cwd-relative path', function() {
var relConfigDir = configDir.substring(projRootDir.length + 1);
var js;
function _loadConfig() {
js = loadConfig(path.join('.', relConfigDir, 'mocharc.js'));
}
expect(_loadConfig, 'not to throw');
expect(js, 'to equal', json);
});
// In other words, path does not begin with '/', './', or '../'
describe('when path is neither absolute or relative', function() {
var nodeModulesDir = path.join(projRootDir, 'node_modules');
var pkgName = 'mocha-config';
var installedLocally = false;
var symlinkedPkg = false;
before(function() {
try {
var srcPath = path.join(configDir, pkgName);
var targetPath = path.join(nodeModulesDir, pkgName);
fs.symlinkSync(srcPath, targetPath, 'dir');
symlinkedPkg = true;
installedLocally = true;
} catch (err) {
if (err.code === 'EEXIST') {
console.log('setup:', 'package already exists in "node_modules"');
installedLocally = true;
} else {
console.error('setup failed:', err);
}
}
});
it('should load configuration given module-relative path', function() {
var js;
if (!installedLocally) {
return this.skip();
}
function _loadConfig() {
js = loadConfig(path.join(pkgName, 'index.js'));
}
expect(_loadConfig, 'not to throw');
expect(js, 'to equal', json);
});
after(function() {
if (symlinkedPkg) {
try {
fs.unlinkSync(path.join(nodeModulesDir, pkgName));
} catch (err) {
console.error('teardown failed:', err);
}
}
});
});
});
});