Skip to content

Instantly share code, notes, and snippets.

@victorcrbt
Created April 8, 2020 17:03
Show Gist options
  • Save victorcrbt/2d3491acb3abce28e6de0a2dd90d01cf to your computer and use it in GitHub Desktop.
Save victorcrbt/2d3491acb3abce28e6de0a2dd90d01cf to your computer and use it in GitHub Desktop.
Jest with TypeScript aliases
module.exports = {
globals: {
'ts-jest': {
compiler: 'ttypescript',
},
},
bail: 1,
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ['./src/app/**/*.(ts|js)', './src/lib/Mail.ts'],
coverageDirectory: './__tests__/coverage',
coverageReporters: ['json', 'lcov'],
moduleFileExtensions: ['js', 'json', 'ts', 'node'],
// The pattern of the aliases on the Jest config are different.
moduleNameMapper: {
'src/(.*)': '<rootDir>/src/$1',
},
preset: 'ts-jest',
setupFilesAfterEnv: ['jest-extended'],
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.test.{js,ts}'],
transform: {
'^.+\\.(ts|js)$': 'ts-jest',
},
};
{
"name": "fintech",
"version": "1.0.0",
"main": "index.js",
"author": "Victor Batalha <[email protected]>",
"license": "MIT",
"private": true,
"scripts": {
"dev": "ts-node-dev -r tsconfig-paths/register --respawn --transpileOnly ./src/server",
// We use ttypescript to compile, so the aliases can be transformed with to relative paths.
// Also, specify the tsconfig file so it won't build the tests.
"build": "yarn ttsc -p tsconfig.build.json",
// Workaround to put the views folder inside the build folder. Might have a better way to do this.
"postbuild": "cp -r ./src/app/views ./build/app/views",
"test": "jest --runInBand --forceExit",
},
"dependencies": {
},
"devDependencies": {
"@types/node": "^13.9.8",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.10.1",
"eslint-import-resolver-typescript": "^2.0.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-prettier": "^3.1.2",
"jest": "^25.2.4",
"jest-extended": "^0.11.5",
"prettier": "^2.0.2",
"supertest": "^4.0.2",
"ts-jest": "^25.3.0",
"ts-node": "^8.8.1",
"ts-node-dev": "^1.0.0-pre.44",
"ttypescript": "^1.5.10",
// Allow custom transforms when compiling TypeScript files.
"typescript": "^3.8.3",
"typescript-eslint-parser": "^22.0.0",
// Used to transform the absolute paths to relative paths together with ttypescript.
"typescript-transform-paths": "^1.1.14"
}
}
// Used for build the project without the tests.
{
"extends": "./tsconfig.json",
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
{
"compilerOptions": {
"target": "es6",
"esModuleInterop": true,
"moduleResolution": "node",
"module": "commonjs",
"lib": ["ES6"],
"baseUrl": "src",
"outDir": "./build",
"paths": {
"src/*": ["*"],
},
// Transform the absolute imports into relative imports on compile.
"plugins": [
{ "transform": "typescript-transform-paths" },
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
],
"typeRoots": ["./node_modules/@types", "./typings"],
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"types": ["jest"]
},
// Needed to put the tests folder so the TypeScript can recognize the path aliases within the tests.
"include": ["src/**/*", "__tests__/**/*"],
"exclude": ["node_modules"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment