forked from webpro-nl/knip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
66 lines (54 loc) · 2.47 KB
/
Copy pathindex.ts
File metadata and controls
66 lines (54 loc) · 2.47 KB
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
import { join, isInternal, toAbsolute, dirname } from '../../util/path.js';
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
import type { Config } from '@jest/types';
// https://jestjs.io/docs/configuration
export const NAME = 'Jest';
/** @public */
export const ENABLERS = ['jest'];
export const isEnabled: IsPluginEnabledCallback = ({ dependencies, manifest }) =>
hasDependency(dependencies, ENABLERS) || Boolean(manifest.name?.startsWith('jest-presets'));
export const CONFIG_FILE_PATTERNS = ['jest.config.{js,ts,mjs,cjs,json}', 'package.json'];
const resolveExtensibleConfig = async (configFilePath: string) => {
const config: Config.InitialOptions = await load(configFilePath);
if (config?.preset) {
const { preset } = config;
if (isInternal(preset)) {
const presetConfigPath = toAbsolute(preset, dirname(configFilePath));
const presetConfig = await resolveExtensibleConfig(presetConfigPath);
Object.assign(config, presetConfig);
}
}
return config;
};
const findJestDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
const config = configFilePath.endsWith('package.json')
? (manifest.jest as Config.InitialOptions)
: await resolveExtensibleConfig(configFilePath);
if (!config) return [];
const replaceRootDir = (name: string) =>
name.includes('<rootDir>') ? join(cwd, name.replace(/^.*<rootDir>/, '')) : name;
const presets = (config.preset ? [config.preset] : []).map(preset =>
isInternal(preset) ? preset : join(preset, 'jest-preset')
);
const environments = config.testEnvironment === 'jsdom' ? ['jest-environment-jsdom'] : [];
const resolvers = config.resolver ? [config.resolver] : [];
const watchPlugins =
config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
const setupFiles = config.setupFiles ?? [];
const setupFilesAfterEnv = config.setupFilesAfterEnv ?? [];
const transform = config.transform
? Object.values(config.transform).map(transform => (typeof transform === 'string' ? transform : transform[0]))
: [];
return [
...presets,
...environments,
...resolvers,
...watchPlugins,
...setupFiles,
...setupFilesAfterEnv,
...transform,
].map(replaceRootDir);
};
export const findDependencies = timerify(findJestDependencies);