-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
20 changed files
with
453 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
h1 { | ||
font-size: 1.6rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ declare var module: NodeModule; | |
interface NodeModule { | ||
id: string; | ||
} | ||
declare module '*.png'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ declare var module: NodeModule; | |
interface NodeModule { | ||
id: string; | ||
} | ||
declare module '*.png'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ declare var module: NodeModule; | |
interface NodeModule { | ||
id: string; | ||
} | ||
declare module '*.png'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
export const jestCfgStub = { | ||
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'], | ||
testRegex: ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$'], | ||
globals: { | ||
'ts-jest': { | ||
diagnostics: { | ||
pretty: false, | ||
}, | ||
}, | ||
}, | ||
} as any; // eslint-disable-line @typescript-eslint/no-explicit-any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>App works</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
h1 { | ||
font-size: 1.6rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/__tests__/__snapshots__/replace-resources.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Replace resources transformer should use inline-files + strip-styles for isolatedModules true 1`] = ` | ||
"\\"use strict\\"; | ||
Object.defineProperty(exports, \\"__esModule\\", { value: true }); | ||
exports.AppComponent = void 0; | ||
const tslib_1 = require(\\"tslib\\"); | ||
const core_1 = require(\\"@angular/core\\"); | ||
let AppComponent = class AppComponent { | ||
constructor() { | ||
this.title = 'test-app-v10'; | ||
} | ||
}; | ||
AppComponent = tslib_1.__decorate([ | ||
core_1.Component({ | ||
selector: 'app-root', | ||
template: require('./app.component.html'), | ||
styleUrls: [], | ||
styles: [], | ||
}) | ||
], AppComponent); | ||
exports.AppComponent = AppComponent; | ||
//# " | ||
`; | ||
|
||
exports[`Replace resources transformer should use replaceResources transformer from @angular/compiler-cli for isolatedModules false 1`] = ` | ||
"\\"use strict\\"; | ||
Object.defineProperty(exports, \\"__esModule\\", { value: true }); | ||
exports.AppComponent = void 0; | ||
const tslib_1 = require(\\"tslib\\"); | ||
const core_1 = require(\\"@angular/core\\"); | ||
let AppComponent = class AppComponent { | ||
constructor() { | ||
this.title = 'test-app-v10'; | ||
} | ||
}; | ||
AppComponent = tslib_1.__decorate([ | ||
core_1.Component({ | ||
selector: 'app-root', | ||
template: require(\\"./app.component.html\\"), | ||
styles: [] | ||
}) | ||
], AppComponent); | ||
exports.AppComponent = AppComponent; | ||
//# " | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { SOURCE_MAPPING_PREFIX } from 'ts-jest/dist/compiler/compiler-utils'; | ||
|
||
import { NgJestConfig } from '../config/ng-jest-config'; | ||
import { jestCfgStub } from './__helpers__/test-helpers'; | ||
import { NgJestCompiler } from '../compiler/ng-jest-compiler'; | ||
|
||
describe('Replace resources transformer', () => { | ||
const fileName = join(__dirname, '__mocks__', 'app.component.ts'); | ||
const fileContent = readFileSync(fileName, 'utf-8'); | ||
|
||
test('should use replaceResources transformer from @angular/compiler-cli for isolatedModules false', () => { | ||
const ngJestConfig = new NgJestConfig({ | ||
...jestCfgStub, | ||
globals: { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
'ts-jest': { | ||
...jestCfgStub.globals['ts-jest'], | ||
isolatedModules: false, | ||
}, | ||
}, | ||
}); | ||
const compiler = new NgJestCompiler(ngJestConfig, new Map()); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const emittedResult = compiler.getCompiledOutput(fileName, fileContent, false)!; | ||
|
||
// Source map is different based on file location which can fail on CI, so we only compare snapshot for js | ||
expect(emittedResult.substring(0, emittedResult.indexOf(SOURCE_MAPPING_PREFIX))).toMatchSnapshot(); | ||
}); | ||
|
||
test('should use inline-files + strip-styles for isolatedModules true', () => { | ||
const ngJestConfig = new NgJestConfig({ | ||
...jestCfgStub, | ||
globals: { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
'ts-jest': { | ||
...jestCfgStub.globals['ts-jest'], | ||
isolatedModules: true, | ||
}, | ||
}, | ||
}); | ||
const compiler = new NgJestCompiler(ngJestConfig, new Map()); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const emittedResult = compiler.getCompiledOutput(fileName, fileContent, false)!; | ||
|
||
// Source map is different based on file location which can fail on CI, so we only compare snapshot for js | ||
expect(emittedResult.substring(0, emittedResult.indexOf(SOURCE_MAPPING_PREFIX))).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** Angular component decorator templateUrl property name */ | ||
export const TEMPLATE_URL = 'templateUrl'; | ||
/** Angular component decorator styleUrls property name */ | ||
export const STYLE_URLS = 'styleUrls'; | ||
/** Angular component decorator styles property name */ | ||
export const STYLES = 'styles'; | ||
/** Angular component decorator template property name */ | ||
export const TEMPLATE = 'template'; | ||
/** Node require function name */ | ||
export const REQUIRE = 'require'; | ||
/** Angular component decorator name */ | ||
export const COMPONENT = 'Component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.