Skip to content

Commit

Permalink
Tighten up module mappers; bump jest (#53)
Browse files Browse the repository at this point in the history
In #6 we introduced very loose moduleNameMapper which resulted in couple of weird bugs and until next Jest release it's impossible to override it.

This PR reverts it and adds some reasonable defaults.

Also, because of this "(.*)"mapping, while debugging #41 on master version of Jest, I encountered an issue where one of the internal Jest modules was calling path matching the regexp, but couldn't find it under src dir. This surfaced only because Jest version on master supports checking if modules in moduleNameMapper exist and throws otherwise.
  • Loading branch information
thymikee authored Sep 6, 2017
1 parent 4123477 commit 0f6ed89
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 79 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![CircleCI Build Status](https://circleci.com/gh/thymikee/jest-preset-angular.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/thymikee/jest-preset-angular)
[![NPM version](https://img.shields.io/npm/v/jest-preset-angular.svg)](https://www.npmjs.com/package/jest-preset-angular)

A preset of [Jest](http://facebook.github.io/jest) configuration for [Angular](https://angular.io/) projects.
A preset of [Jest](http://facebook.github.io/jest) configuration for [Angular](https://angular.io/) projects.

This is a part of the article: [Testing Angular faster with Jest](https://www.xfive.co/blog/testing-angular-faster-jest/).

Expand Down Expand Up @@ -44,7 +44,9 @@ import './jestGlobalMocks'; // browser mocks globally available for every test
"html"
],
"moduleNameMapper": {
"(.*)": "<rootDir>/src/$1"
"app/(.*)": "<rootDir>/src/app/$1",
"assets/(.*)": "<rootDir>/src/assets/$1",
"environments/(.*)": "<rootDir>/src/environments/$1"
},
"transformIgnorePatterns": [
"node_modules/(?!@ngrx)"
Expand Down Expand Up @@ -105,31 +107,29 @@ exports[`CalcComponent should snap 1`] = `
`;
```



## Troubleshooting

Problems may arise if you're using custom builds (this preset is tailored for `angular-cli` as firsty priority). Please be adivsed that every entry in default configuration may be overriden to best suite your app's needs.

### Absolute imports

TypeScript supports absolute imports. The preset by default understands all absolute imports referring to `src` directory, so instead:
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:
```js
import MyComponent from '../../src/app/my.component';
import MyStuff from '../../src/testing/my.stuff';
```
you can use:
```js
import MyComponent from 'app/my.component';
import MyStuff from 'testing/my.stuff';
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:
```js
{
"jest": {
"moduleNameMapper": {
"(.*)": "$1", // override default if it causes problems
"testing/(.*)": "<rootDir>/app/testing/$1"
"app/(.*)": "<rootDir>/src/to/app/$1", // override default, why not
"testing/(.*)": "<rootDir>/app/testing/$1" // add new mapping
}
}
}
Expand Down
1 change: 1 addition & 0 deletions example/__mocks__/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'test-image-stub';
7 changes: 5 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"jest": "^20.0.0",
"jest": "^21.0.1",
"jest-preset-angular": "^2.0.1",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
Expand All @@ -51,6 +51,9 @@
},
"jest": {
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts"
"setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts",
"moduleNameMapper": {
"\\.(jpg|jpeg|png)$": "<rootDir>/__mocks__/image.js"
}
}
}
10 changes: 10 additions & 0 deletions example/src/app/__snapshots__/app.component.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ exports[`AppComponent snaps 1`] = `
variableWithPrecedingDolar={[Function Number]}
>
<h1>
app works!
<app-calc />
<span>
aaa $1234
</span>
<span>
ddd
</span>
\`test'chars""
</h1>
</app-root>
`;
12 changes: 10 additions & 2 deletions example/src/app/calc/__snapshots__/calc.component.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
exports[`CalcComponent should snap 1`] = `
<app-calc
hasAClass="false"
image={[Function String]}
prop1={[Function Number]}
>
<p
<p
class="a-default-class"
ng-reflect-klass="a-default-class"
ng-reflect-ng-class="[object Object]"
>
calc works! 1337 another text node
calc works!
1337
another text node
test-image-stub
</p>
</app-calc>
`;
4 changes: 4 additions & 0 deletions example/src/app/calc/calc.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit, Input } from '@angular/core';
import { Observable } from 'rxjs/Observable';
const image = require('assets/its_something.png');

@Component({
selector: 'app-calc',
Expand All @@ -13,18 +14,21 @@ import { Observable } from 'rxjs/Observable';
calc works!
{{prop1}}
another text node
{{image}}
</p>
`,
styleUrls: ['./calc.component.css']
})
export class CalcComponent implements OnInit {
@Input() hasAClass = false;
prop1: number;
image: string;
observable$: Observable<string>;

constructor() {
this.init();
this.prop1 = 1337;
this.image = image;
}

ngOnInit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
exports[`SimpleComponent snapshots 1`] = `
<app-simple>
<p>
simple works!
</p>
</app-simple>
`;
Binary file added example/src/assets/its_something.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0f6ed89

Please sign in to comment.