-
-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathindex.ts
More file actions
169 lines (143 loc) · 5.99 KB
/
Copy pathindex.ts
File metadata and controls
169 lines (143 loc) · 5.99 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { IsPluginEnabled, Plugin, PluginOptions, ResolveConfig } from '../../types/config.ts';
import { arrayify } from '../../util/array.ts';
import { _glob, _dirGlob } from '../../util/glob.ts';
import { type Input, toDeferResolve, toEntry } from '../../util/input.ts';
import { isInternal, join, normalize, toAbsolute } from '../../util/path.ts';
import { hasDependency } from '../../util/plugin.ts';
import { getDependenciesFromConfig } from '../babel/index.ts';
import type { BabelConfigObj } from '../babel/types.ts';
import { getReportersDependencies, resolveExtensibleConfig } from './helpers.ts';
import type { JestConfig, JestInitialOptions } from './types.ts';
// https://jestjs.io/docs/configuration
const title = 'Jest';
const enablers = ['jest'];
const isEnabled: IsPluginEnabled = ({ dependencies, manifest }) =>
hasDependency(dependencies, enablers) || Boolean(manifest.name?.startsWith('jest-presets'));
const config = ['jest.config.{js,ts,mjs,cjs,mts,cts,json}', 'package.json'];
const mocks = ['**/__mocks__/**/*.[jt]s?(x)'];
const entry = ['**/__tests__/**/*.?(c|m)[jt]s?(x)', '**/?(*.)+(spec|test).?(c|m)[jt]s?(x)', ...mocks];
const rootDirRe = /<rootDir>/;
const isBabelJest = (transformer: [string, unknown]): transformer is [string, BabelConfigObj] =>
transformer[0] === 'babel-jest';
const resolveDependencies = async (
config: JestInitialOptions,
rootDir: string,
options: PluginOptions
): Promise<Input[]> => {
const { configFileDir } = options;
if (config?.preset) {
const { preset } = config;
if (isInternal(preset)) {
const presetConfigPath = toAbsolute(preset, configFileDir);
const presetConfig = await resolveExtensibleConfig(presetConfigPath);
config = Object.assign({}, presetConfig, config);
}
}
const presets = (config.preset ? [config.preset] : []).map(preset =>
isInternal(preset) ? preset : join(preset, 'jest-preset')
);
const projects: (string | Input)[] = [];
for (const project of config.projects ?? []) {
if (typeof project === 'string') {
// Special case: Most Jest config settings can resolve <rootDir> later,
// but projects support wildcard expansion so should be expanded now.
//
// Jest projects may be directories or Jest config paths.
//
// Jest uses glob's `{ windowsPathsNoEscape: true }`, which we don't
// currently implement.
const patterns = [project.replace(rootDirRe, rootDir)];
const files = await _glob({ patterns, cwd: options.cwd });
const dirs = await _dirGlob({ patterns, cwd: options.cwd });
projects.push(...files, ...dirs);
} else {
const dependencies = await resolveDependencies(project, rootDir, options);
for (const dependency of dependencies) projects.push(dependency);
}
}
const runner = config.runner ? [typeof config.runner === 'string' ? config.runner : config.runner[0]] : [];
const runtime = config.runtime && config.runtime !== 'jest-circus' ? [config.runtime] : [];
const environments =
config.testEnvironment === 'jsdom'
? ['jest-environment-jsdom']
: config.testEnvironment
? [config.testEnvironment]
: [];
const resolvers = config.resolver ? [config.resolver] : [];
const reporters = getReportersDependencies(config, options);
const watchPlugins =
config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
const transform: (string | Input)[] = [];
for (const transformer of config.transform ? Object.values(config.transform) : []) {
if (typeof transformer === 'string') {
transform.push(transformer);
} else {
transform.push(transformer[0]);
if (isBabelJest(transformer)) transform.push(...getDependenciesFromConfig(transformer[1]));
}
}
const moduleNameMapper = (
config.moduleNameMapper
? Object.values(config.moduleNameMapper).map(mapper => (typeof mapper === 'string' ? mapper : mapper[0]))
: []
).filter(value => !/\$[0-9]/.test(value));
const testResultsProcessor = config.testResultsProcessor ? [config.testResultsProcessor] : [];
const snapshotResolver = config.snapshotResolver ? [config.snapshotResolver] : [];
const snapshotSerializers = config.snapshotSerializers ?? [];
const testSequencer = config.testSequencer ? [config.testSequencer] : [];
const setupFiles = config.setupFiles ?? [];
const setupFilesAfterEnv = config.setupFilesAfterEnv ?? [];
const globalSetup = config.globalSetup ? [config.globalSetup] : [];
const globalTeardown = config.globalTeardown ? [config.globalTeardown] : [];
return [
...presets,
...projects,
...runner,
...runtime,
...environments,
...resolvers,
...reporters,
...watchPlugins,
...setupFiles,
...setupFilesAfterEnv,
...transform,
...moduleNameMapper,
...testResultsProcessor,
...snapshotResolver,
...snapshotSerializers,
...testSequencer,
...globalSetup,
...globalTeardown,
].map(id => (typeof id === 'string' ? toDeferResolve(id) : id));
};
const resolveConfig: ResolveConfig<JestConfig> = async (localConfig, options) => {
const { configFileDir } = options;
if (typeof localConfig === 'function') localConfig = await localConfig();
const rootDir = localConfig.rootDir ?? configFileDir;
const replaceRootDir = (name: string) => name.replace(rootDirRe, rootDir);
const inputs = await resolveDependencies(localConfig, rootDir, options);
const entries = localConfig.testMatch
? arrayify(localConfig.testMatch)
.map(replaceRootDir)
.map(id => toEntry(id))
: entry.map(id => toEntry(id));
if (localConfig.testMatch && !options.config.entry) entries.push(...mocks.map(id => toEntry(id)));
const result = inputs.map(dependency => {
dependency.specifier = normalize(replaceRootDir(dependency.specifier));
return dependency;
});
return entries.concat(result);
};
const args = {
config: true,
};
const plugin: Plugin = {
title,
enablers,
isEnabled,
config,
entry,
resolveConfig,
args,
};
export default plugin;