Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/** @type {import('@jest/types').Config.InitialOptions} */
/** @typedef {import('ts-jest')} */

const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig-base');

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
// Ignore the TS project `outDir`
// https://github.com/kulshekhar/ts-jest/issues/765
testPathIgnorePatterns: ['<rootDir>/target/'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
globals: {
'ts-jest': {
isolatedModules: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { divide } from '~'
import { divide as _divide } from '~/divide'

test('imports same divide function', () => {
expect(_divide).toBe(divide)
})

test('divides 3 / 2 to equal 1.5', () => {
expect(divide(3, 2)).toBe(1.5)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { multiply } from '@/multiply'

test('multiplies 2 * 2 to equal 4', () => {
expect(multiply(2, 2)).toBe(4)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { subtract } from '@/subtract'

test('subtracts 3 - 1 to equal 2', () => {
expect(subtract(3, 1)).toBe(2)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import divide from 'lodash/divide'

export { divide }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import multiply from 'lodash/multiply'

export { multiply }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import subtract from 'lodash/subtract'

export { subtract }
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
"rootDir": "./src/",
"types": [],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
"esModuleInterop": true,
"baseUrl": ".",
"paths": {
"@/*": [
"src/nested/*",
"src/nested/src/*"
],
"~": [
"src/math/divide.ts"
],
"~/*": [
"src/math/*"
]
}
}
}
25 changes: 18 additions & 7 deletions src/config/paths-to-module-name-mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ import { pathsToModuleNameMapper } from './paths-to-module-name-mapper'
const tsconfigMap = {
log: ['src/util/log'],
server: ['src/server'],
client: ['src/client', 'src/client/index'],
'util/*': ['src/util/*'],
'api/*': ['src/api/*'],
'test/*': ['test/*'],
'mocks/*': ['test/mocks/*'],
'test/*/mock': ['test/mocks/*'],
'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'],
}

describe('pathsToModuleNameMapper', () => {
it('should convert tsconfig mapping', () => {
expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(`
Object {
"^api/(.*)$": "src/api/$1",
"^client$": Array [
"src/client",
"src/client/index",
],
"^log$": "src/util/log",
"^mocks/(.*)$": "test/mocks/$1",
"^server$": "src/server",
"^test/(.*)$": "test/$1",
"^test/(.*)/mock$": "test/mocks/$1",
"^test/(.*)/mock$": Array [
"test/mocks/$1",
"test/__mocks__/$1",
],
"^util/(.*)$": "src/util/$1",
}
`)
Expand All @@ -31,11 +39,18 @@ Object {
expect(pathsToModuleNameMapper(tsconfigMap, { prefix: '<rootDir>/' })).toMatchInlineSnapshot(`
Object {
"^api/(.*)$": "<rootDir>/src/api/$1",
"^client$": Array [
"<rootDir>/src/client",
"<rootDir>/src/client/index",
],
"^log$": "<rootDir>/src/util/log",
"^mocks/(.*)$": "<rootDir>/test/mocks/$1",
"^server$": "<rootDir>/src/server",
"^test/(.*)$": "<rootDir>/test/$1",
"^test/(.*)/mock$": "<rootDir>/test/mocks/$1",
"^test/(.*)/mock$": Array [
"<rootDir>/test/mocks/$1",
"<rootDir>/test/__mocks__/$1",
],
"^util/(.*)$": "<rootDir>/src/util/$1",
}
`)
Expand All @@ -48,20 +63,16 @@ Object {
pathsToModuleNameMapper({
kept: ['src/kept'],
'no-target': [],
'too-many-target': ['one', 'two'],
'too/*/many/*/stars': ['to/*/many/*/stars'],
}),
).toMatchInlineSnapshot(`
Object {
"^kept$": "src/kept",
"^too\\\\-many\\\\-target$": "one",
}
`)
expect(log.lines.warn).toMatchInlineSnapshot(`
Array [
"[level:40] Not mapping \\"no-target\\" because it has no target.
",
"[level:40] Mapping only to first target of \\"too-many-target\\" because it has more than one (2).
",
"[level:40] Not mapping \\"too/*/many/*/stars\\" because it has more than one star (\`*\`).
",
Expand Down
14 changes: 4 additions & 10 deletions src/config/paths-to-module-name-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,18 @@ export const pathsToModuleNameMapper = (
if (toPaths.length === 0) {
logger.warn(interpolate(Errors.NotMappingPathWithEmptyMap, { path: fromPath }))
continue
} else if (toPaths.length > 1) {
logger.warn(
interpolate(Errors.MappingOnlyFirstTargetOfPath, {
path: fromPath,
count: toPaths.length,
}),
)
}
const target = toPaths[0]

// split with '*'
const segments = fromPath.split(/\*/g)
if (segments.length === 1) {
const paths = toPaths.map((target) => `${prefix}${target}`)
pattern = `^${escapeRegex(fromPath)}$`
jestMap[pattern] = `${prefix}${target}`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else if (segments.length === 2) {
const paths = toPaths.map((target) => `${prefix}${target.replace(/\*/g, '$1')}`)
pattern = `^${escapeRegex(segments[0])}(.*)${escapeRegex(segments[1])}$`
jestMap[pattern] = `${prefix}${target.replace(/\*/g, '$1')}`
jestMap[pattern] = paths.length === 1 ? paths[0] : paths
} else {
logger.warn(interpolate(Errors.NotMappingMultiStarPath, { path: fromPath }))
continue
Expand Down
1 change: 0 additions & 1 deletion src/util/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const enum Errors {
UnableToCompileTypeScript = '{{diagnostics}}',
NotMappingMultiStarPath = 'Not mapping "{{path}}" because it has more than one star (`*`).',
NotMappingPathWithEmptyMap = 'Not mapping "{{path}}" because it has no target.',
MappingOnlyFirstTargetOfPath = 'Mapping only to first target of "{{path}}" because it has more than one ({{count}}).',
GotJsFileButAllowJsFalse = 'Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore',
GotUnknownFileTypeWithoutBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore.',
GotUnknownFileTypeWithBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files.',
Expand Down