@jpapini/jest-config
Version:
Jest configuration and utilities for JavaScript and TypeScript projects.
104 lines (101 loc) • 3.44 kB
JavaScript
;
// src/create-jest-config.ts
function createBaseJestConfig(options) {
return {
...options,
rootDir: options.rootDir,
collectCoverageFrom: [
"<rootDir>/src/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}",
...options.collectCoverageFrom ?? []
],
coverageDirectory: options.coverageDirectory ?? "<rootDir>/coverage",
coveragePathIgnorePatterns: [
"index.ts",
"index.tsx",
...options.coveragePathIgnorePatterns ?? []
],
testMatch: [
"<rootDir>/src/**/*.test.{ts,tsx}",
"<rootDir>/src/**/*.spec.{ts,tsx}",
...Array.isArray(options.testMatch) ? options.testMatch : typeof options.testMatch === "string" ? [options.testMatch] : []
]
};
}
function createSwcJestConfig(options, swcConfig = {}) {
return createBaseJestConfig({
...options,
transform: {
"^.+\\.(t|j|mj|cj)sx?$": [
"@swc/jest",
{
...swcConfig,
jsc: {
...swcConfig.jsc,
parser: {
...swcConfig.jsc?.parser,
syntax: "typescript"
},
transform: {
...swcConfig.jsc?.transform,
useDefineForClassFields: true
},
keepClassNames: true,
externalHelpers: false
}
}
],
...options.transform ?? {}
}
});
}
function createTsJestConfig(options, tsJestConfig = {}) {
return createBaseJestConfig({
...options,
transform: {
"^.+\\.(t|j|mj|cj)sx?$": [
"ts-jest",
{
...tsJestConfig
}
],
...options.transform ?? {}
}
});
}
// src/paths-to-module-name-mapper.ts
var escapeRegex = (str) => str.replace(/[-\\^$*+?.()|[\]{}]/gu, "\\$&");
var pathsToModuleNameMapper = (mapping, { prefix = "", useESM = false } = {}) => {
const jestMap = {};
for (const fromPath of Object.keys(mapping)) {
const toPaths = mapping[fromPath];
if (toPaths.length === 0) continue;
const segments = fromPath.split(/\*/gu);
if (segments.length === 1) {
const paths = toPaths.map((target) => {
const enrichedPrefix = prefix !== "" && !prefix.endsWith("/") ? `${prefix}/` : prefix;
return `${enrichedPrefix}${target}`;
});
const cjsPattern = `^${escapeRegex(fromPath)}$`;
jestMap[cjsPattern] = paths.length === 1 ? paths[0] : paths;
} else if (segments.length === 2) {
const paths = toPaths.map((target) => {
const enrichedTarget = target.startsWith("./") && prefix !== "" ? target.substring(target.indexOf("/") + 1) : target;
const enrichedPrefix = prefix !== "" && !prefix.endsWith("/") ? `${prefix}/` : prefix;
return `${enrichedPrefix}${enrichedTarget.replace(/\*/gu, "$1")}`;
});
if (useESM) {
const esmPattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}\\.js$`;
jestMap[esmPattern] = paths.length === 1 ? paths[0] : paths;
}
const cjsPattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`;
jestMap[cjsPattern] = paths.length === 1 ? paths[0] : paths;
}
}
if (useESM) jestMap["^(\\.{1,2}/.*)\\.js$"] = "$1";
return jestMap;
};
exports.createSwcJestConfig = createSwcJestConfig;
exports.createTsJestConfig = createTsJestConfig;
exports.pathsToModuleNameMapper = pathsToModuleNameMapper;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map