ts-jest-resolver
Version:
A resolver for `jest` that uses same strategy as TS when resolving files with JavaScript extension (".js").
66 lines (62 loc) • 1.92 kB
JavaScript
/*!
* ts-jest-resolver v2.0.1
* (c) Vitor Luiz Cavalcanti
* Released under the MIT License.
*/
;
var resolutions = [
{
matcher: /\.js$/i,
extensions: ['.ts', '.tsx'],
},
{
matcher: /\.jsx$/i,
extensions: ['.ts', '.tsx', '.js'],
},
{
matcher: /\.cjs$/i,
extensions: ['.cts'],
},
{
matcher: /\.mjs$/i,
extensions: ['.mts'],
},
];
/**
* A resolver for `jest` that uses same strategy as TS when resolving files with
* JavaScript extension (".js"). Otherwise it just uses default resolver.
*
* When receives a path with JavaScript extension (".js" or ".jsx"):
* 1. It tries to resolve to a path with ".tsx".
* 2. If no file was found, it tries to resolve to a path with ".ts".
* 3. If no file was found, it resolves to original path (with ".js" or ".jsx").
*
* When receives a path with ES modules extension (".mjs"):
* 1. It tries to resolve to a path with ".mts".
* 2. If no file was found, it resolves to original path (with ".mjs").
*
* When receives a path with CommonJS modules extension (".cjs"):
* 1. It tries to resolve to a path with ".cts".
* 2. If no file was found, it resolves to original path (with ".cjs").
*/
function resolverForTSJest(path, options) {
var resolver = options.defaultResolver;
var resolution = resolutions.find(function (_a) {
var matcher = _a.matcher;
return matcher.test(path);
});
if (resolution) {
for (var _i = 0, _a = resolution.extensions; _i < _a.length; _i++) {
var extension = _a[_i];
try {
return resolver(path.replace(resolution.matcher, extension), options);
}
catch (_b) {
continue;
}
}
}
return resolver(path, options);
}
module.exports = resolverForTSJest;
//# sourceMappingURL=index.cjs.map