Skip to content

Commit 1b20c19

Browse files
authored
feat(compiler): use replace-resources transformer from Angular (#708)
BREAKING CHANGE - `isolatedModules: true` will use `inline-files` and `strip-styles` transformers as default transformers. - `isolatedModules: false` will use `replace-resources` transformer from `@ngtools/webpack` (besides the existing `downlevel-ctor` transformer). This will make `jest-preset-angular` become closer to what Angular CLI does with Karma + Jasmine.
1 parent 68237fe commit 1b20c19

File tree

20 files changed

+453
-69
lines changed

20 files changed

+453
-69
lines changed

e2e/__tests__/calc/calc.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Component, OnInit, Input } from '@angular/core';
2-
import { Observable } from 'rxjs';
3-
4-
const image = require('e2e/test-app-v10/src/assets/its_something.png');
2+
import * as image from 'e2e/test-app-v10/src/assets/its_something.png';
53

64
@Component({
75
selector: 'app-calc',
@@ -23,7 +21,6 @@ export class CalcComponent implements OnInit {
2321
@Input() hasAClass = false;
2422
prop1: number;
2523
image: string;
26-
observable$: Observable<string>;
2724

2825
constructor() {
2926
this.init();

e2e/__tests__/forward-ref/forward-ref.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { forwardRef, Inject, Injector } from '@angular/core';
22

3-
const shouldSkipTest = process.env.NG_VERSION === 'v9' || process.env.SKIP_TEST === 'true';
3+
const shouldSkipTest = process.env.NG_VERSION === 'v9' || process.env.ISOLATED_MODULES === 'true';
44
const skipTest = shouldSkipTest ? test.skip : test
55

66
if (shouldSkipTest) {

e2e/__tests__/simple-with-styles/simple-with-styles.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Component } from '@angular/core';
33
@Component({
44
selector: 'app-simple-with-styles',
55
templateUrl: './simple-with-styles.component.html',
6+
styleUrls: ['./simple-with-styles.scss'],
67
// we have to setup styles this way, since simple styles/styleUrs properties will be removed (jest does not unit test styles)
78
styles: [`
89
.some-class { color: red }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
font-size: 1.6rem;
3+
}

e2e/test-app-v10/src/typings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ declare var module: NodeModule;
33
interface NodeModule {
44
id: string;
55
}
6+
declare module '*.png';

e2e/test-app-v11/src/typings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ declare var module: NodeModule;
33
interface NodeModule {
44
id: string;
55
}
6+
declare module '*.png';

e2e/test-app-v9/src/typings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ declare var module: NodeModule;
33
interface NodeModule {
44
id: string;
55
}
6+
declare module '*.png';

scripts/e2e.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ const executeTest = (projectRealPath) => {
2525
logger.log();
2626

2727
logger.log('setting NG_VERSION environment variable');
28+
logger.log();
2829
const projectName = projectRealPath.match(/([^\\/]*)\/*$/)[1];
2930
process.env.NG_VERSION = projectName.substring(projectName.lastIndexOf('-') + 1);
3031

3132
// then we install it in the repo
3233
logger.log('ensuring all dependencies of target project are installed');
34+
logger.log();
3335

3436
execa.sync('yarn', ['install'], { cwd: projectRealPath });
3537

3638
logger.log('cleaning old assets in target project');
39+
logger.log();
3740

3841
const testCasesDest = join(projectRealPath, 'src', '__tests__');
3942
const presetDir = join(projectRealPath, 'node_modules', 'jest-preset-angular');
@@ -42,6 +45,7 @@ const executeTest = (projectRealPath) => {
4245
mkdirSync(presetDir);
4346

4447
logger.log('copying distributed assets to target project');
48+
logger.log();
4549

4650
copySync(join(cwd, 'jest-preset.js'), `${presetDir}/jest-preset.js`);
4751
copySync(join(cwd, 'ngcc-jest-processor.js'), `${presetDir}/ngcc-jest-processor.js`);
@@ -50,6 +54,7 @@ const executeTest = (projectRealPath) => {
5054
copySync(join(cwd, 'build'), `${presetDir}/build`);
5155

5256
logger.log('copying test cases to target project');
57+
logger.log();
5358

5459
copySync(join(cwd, 'e2e', '__tests__'), testCasesDest);
5560

@@ -66,7 +71,7 @@ const executeTest = (projectRealPath) => {
6671
// cmdESMIso.push(...jestArgs);
6772
}
6873

69-
logger.log('starting non isolatedModules tests');
74+
logger.log('STARTING NONE ISOLATED MODULES TESTS');
7075
logger.log();
7176
logger.log('starting the CJS tests using:', ...cmdCjsUnIso);
7277
logger.log();
@@ -77,11 +82,13 @@ const executeTest = (projectRealPath) => {
7782
env: process.env,
7883
});
7984

80-
logger.log('starting isolatedModules tests');
8185
logger.log();
82-
logger.log('setting SKIP_TEST environment variable for isolatedModules true');
83-
process.env.SKIP_TEST = 'true';
86+
logger.log('STARTING ISOLATED MODULES TESTS');
87+
logger.log();
88+
logger.log('setting ISOLATED_MODULES environment variable for isolatedModules true');
89+
process.env.ISOLATED_MODULES = 'true';
8490

91+
logger.log();
8592
logger.log('starting the CommonJS tests using:', ...cmdCjsIso);
8693
logger.log();
8794

@@ -104,7 +111,7 @@ const executeTest = (projectRealPath) => {
104111

105112
execa.sync('rimraf', [testCasesDest]);
106113
delete process.env.NG_VERSION;
107-
delete process.env.SKIP_TEST;
114+
delete process.env.ISOLATED_MODULES;
108115
};
109116

110117
const cwd = process.cwd();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2+
export const jestCfgStub = {
3+
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
4+
testRegex: ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$'],
5+
globals: {
6+
'ts-jest': {
7+
diagnostics: {
8+
pretty: false,
9+
},
10+
},
11+
},
12+
} as any; // eslint-disable-line @typescript-eslint/no-explicit-any
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>App works</h1>

0 commit comments

Comments
 (0)