Skip to content

Instantly share code, notes, and snippets.

@latipun7
Created September 3, 2020 10:02
Show Gist options
  • Save latipun7/c0d1bdc5ed10911a57b7879c9460a24b to your computer and use it in GitHub Desktop.
Save latipun7/c0d1bdc5ed10911a57b7879c9460a24b to your computer and use it in GitHub Desktop.
Jest Config to Resolve Typescript Paths Mapping
/* eslint-disable @typescript-eslint/no-var-requires */
const { readFileSync } = require('fs');
const { resolve } = require('path');
const stripJSON = require('strip-json-comments');
const { pathsToModuleNameMapper } = require('ts-jest/utils');
function readAndStripJSON(...path) {
const rawJSONString = readFileSync(resolve(process.cwd(), ...path), {
encoding: 'utf8',
});
const strippedComments = stripJSON(rawJSONString);
return JSON.parse(strippedComments);
}
/**
* @type {import('./tsconfig.json')}
*/
const { compilerOptions } = readAndStripJSON('tsconfig.json');
module.exports = {
bail: true,
preset: 'ts-jest',
testEnvironment: 'node',
moduleDirectories: ['node_modules', '<rootDir>'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/',
}),
testPathIgnorePatterns: [
'<rootDir>/node_modules',
'<rootDir>/config',
'<rootDir>/dist',
],
globals: {
'ts-jest': {
diagnostics: false,
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment