Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(presets): remove moduleNameMapper config #910

Merged
merged 1 commit into from
Apr 28, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 0 additions & 7 deletions presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ const basePreset = {
'^.+\\.(ts|js|html|css|sass|scss|less|styl)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
'^app/(.*)$': '<rootDir>/src/app/$1',
'^assets/(.*)$': '<rootDir>/src/assets/$1',
'^environments/(.*)$': '<rootDir>/src/environments/$1',
},
snapshotSerializers,
};

Expand All @@ -33,7 +27,6 @@ module.exports = {
},
},
moduleNameMapper: {
...basePreset.moduleNameMapper,
tslib: '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
Expand Down
10 changes: 0 additions & 10 deletions src/__tests__/__snapshots__/jest-preset.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ Object {
"js",
"json",
],
"moduleNameMapper": Object {
"^app/(.*)$": "<rootDir>/src/app/$1",
"^assets/(.*)$": "<rootDir>/src/assets/$1",
"^environments/(.*)$": "<rootDir>/src/environments/$1",
"^src/(.*)$": "<rootDir>/src/$1",
},
"snapshotSerializers": Array [
"jest-preset-angular/build/serializers/html-comment",
"jest-preset-angular/build/serializers/ng-snapshot",
Expand Down Expand Up @@ -51,10 +45,6 @@ Object {
"json",
],
"moduleNameMapper": Object {
"^app/(.*)$": "<rootDir>/src/app/$1",
"^assets/(.*)$": "<rootDir>/src/assets/$1",
"^environments/(.*)$": "<rootDir>/src/environments/$1",
"^src/(.*)$": "<rootDir>/src/$1",
"tslib": "<rootDir>/node_modules/tslib/tslib.es6.js",
},
"snapshotSerializers": Array [
Expand Down
6 changes: 0 additions & 6 deletions website/docs/getting-started/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ module.exports = {
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
'^app/(.*)$': '<rootDir>/src/app/$1',
'^assets/(.*)$': '<rootDir>/src/assets/$1',
'^environments/(.*)$': '<rootDir>/src/environments/$1',
},
snapshotSerializers,
};
```
Expand Down
35 changes: 3 additions & 32 deletions website/docs/guides/absolute-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,7 @@ id: absolute-imports
title: Absolute Imports
---

TypeScript supports absolute imports. The preset (starting from v3.0.0) by default understands absolute imports referring to `src`, `app`, `assets` and `environments` directory, so instead:
If you wish to use TypeScript path mappings which are defined in `paths` of your tsconfig, make sure that you create the
similar mapping for `moduleNameMapper` in Jest config.

```ts
import MyComponent from '../../src/app/my.component';
import MyStuff from '../../src/testing/my.stuff';
```

you can use:

```ts
import MyComponent from 'app/my.component';
import MyStuff from 'src/testing/my.stuff';
```

However, if your directory structure differ from that provided by `angular-cli` you can adjust `moduleNameMapper` in Jest config:

```json5
{
jest: {
moduleNameMapper: {
'app/(.*)': '<rootDir>/src/to/app/$1', // override default, why not
'testing/(.*)': '<rootDir>/app/testing/$1', // add new mapping
},
},
}
```

:::important

If you wish to use any absolute import paths which are defined in `paths` of your tsconfig, make sure that you create the
similar mapping for `moduleNameMapper` in Jest config

:::
More information see `ts-jest` [paths mapping](https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping) configuration documentation