forked from jestjs/jest
/
resolveConfigPath.test.ts
104 lines (84 loc) · 3.17 KB
/
resolveConfigPath.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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {tmpdir} from 'os';
import * as path from 'path';
import resolveConfigPath from '../resolveConfigPath';
import {JEST_CONFIG_EXT_ORDER} from '../constants';
const {cleanup, writeFiles} = require('../../../../e2e/Utils');
const DIR = path.resolve(tmpdir(), 'resolve_config_path_test');
const ERROR_PATTERN = /Could not find a config file based on provided values/;
const NO_ROOT_DIR_ERROR_PATTERN = /Can't find a root directory/;
beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));
describe.each(JEST_CONFIG_EXT_ORDER.slice(0))(
'Resolve config path %s',
extension => {
test(`file path with "${extension}"`, () => {
const relativeConfigPath = `a/b/c/my_config${extension}`;
const absoluteConfigPath = path.resolve(DIR, relativeConfigPath);
writeFiles(DIR, {[relativeConfigPath]: ''});
// absolute
expect(resolveConfigPath(absoluteConfigPath, DIR)).toBe(
absoluteConfigPath,
);
expect(() => resolveConfigPath('/does_not_exist', DIR)).toThrowError(
NO_ROOT_DIR_ERROR_PATTERN,
);
// relative
expect(resolveConfigPath(relativeConfigPath, DIR)).toBe(
absoluteConfigPath,
);
expect(() => resolveConfigPath('does_not_exist', DIR)).toThrowError(
NO_ROOT_DIR_ERROR_PATTERN,
);
});
test(`directory path with "${extension}"`, () => {
const relativePackageJsonPath = 'a/b/c/package.json';
const absolutePackageJsonPath = path.resolve(
DIR,
relativePackageJsonPath,
);
const relativeJestConfigPath = `a/b/c/jest.config${extension}`;
const absoluteJestConfigPath = path.resolve(DIR, relativeJestConfigPath);
writeFiles(DIR, {[`a/b/c/some_random_file${extension}`]: ''});
// no configs yet. should throw
expect(() =>
// absolute
resolveConfigPath(path.dirname(absoluteJestConfigPath), DIR),
).toThrowError(ERROR_PATTERN);
expect(() =>
// relative
resolveConfigPath(path.dirname(relativeJestConfigPath), DIR),
).toThrowError(ERROR_PATTERN);
writeFiles(DIR, {[relativePackageJsonPath]: ''});
// absolute
expect(
resolveConfigPath(path.dirname(absolutePackageJsonPath), DIR),
).toBe(absolutePackageJsonPath);
// relative
expect(
resolveConfigPath(path.dirname(relativePackageJsonPath), DIR),
).toBe(absolutePackageJsonPath);
writeFiles(DIR, {[relativeJestConfigPath]: ''});
// jest.config.js takes precedence
// absolute
expect(
resolveConfigPath(path.dirname(absolutePackageJsonPath), DIR),
).toBe(absoluteJestConfigPath);
// relative
expect(
resolveConfigPath(path.dirname(relativePackageJsonPath), DIR),
).toBe(absoluteJestConfigPath);
expect(() => {
resolveConfigPath(
path.join(path.dirname(relativePackageJsonPath), 'j/x/b/m/'),
DIR,
);
}).toThrowError(NO_ROOT_DIR_ERROR_PATTERN);
});
},
);