Skip to content

Commit

Permalink
feat(compiler): use replace-resources for isolatedModules: true (#…
Browse files Browse the repository at this point in the history
…717)

Copy source code of TypeScript `transpileModule` so that we can get `Program` which is created in that function which then allows us to use `replace-resources` transformer from Angular.

This also provides fully compatibility to ESM for `isolatedModules: true`, related to #710 

BREAKING CHANGE
- `inline-files` and `strip-styles` are removed from `jest-preset-angular` and now `jest-preset-angular` always uses Angular `replace-resources` instead for both `isolatedModules: false` and `isolatedModules: true`.
- The transformers in your jest config
```
'jest-preset-angular/build/InlineFilesTransformer'
'jest-preset-angular/build/StripStylesTransformer'
```
or
```
'jest-preset-angular/build/transformers/inline-files'
'jest-preset-angular/build/transformers/strip-styles'
```
must be removed
  • Loading branch information
ahnpnl authored Jan 7, 2021
1 parent 9d25b93 commit 76c25d2
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 935 deletions.
12 changes: 11 additions & 1 deletion src/__tests__/__snapshots__/hoisting.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Hoisting should hoist correctly 1`] = `
exports[`Hoisting should hoist correctly with isolatedModules false 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
const globals_1 = require(\\"@jest/globals\\");
globals_1.jest.mock('./foo');
const foo_1 = require(\\"./foo\\");
console.log(foo_1.getFoo());
//# "
`;

exports[`Hoisting should hoist correctly with isolatedModules true 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
const globals_1 = require(\\"@jest/globals\\");
Expand Down
207 changes: 0 additions & 207 deletions src/__tests__/__snapshots__/inline-files.spec.ts.snap

This file was deleted.

51 changes: 46 additions & 5 deletions src/__tests__/__snapshots__/replace-resources.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Replace resources transformer should use inline-files + strip-styles for isolatedModules true 1`] = `
exports[`Replace resources transformer with isolatedModules false should use replaceResources transformer from @angular/compiler-cli with useESM false 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.AppComponent = void 0;
Expand All @@ -14,16 +14,15 @@ let AppComponent = class AppComponent {
AppComponent = tslib_1.__decorate([
core_1.Component({
selector: 'app-root',
template: require('./app.component.html'),
styleUrls: [],
styles: [],
template: require(\\"./app.component.html\\"),
styles: []
})
], AppComponent);
exports.AppComponent = AppComponent;
//# "
`;

exports[`Replace resources transformer should use replaceResources transformer from @angular/compiler-cli for isolatedModules false 1`] = `
exports[`Replace resources transformer with isolatedModules false should use replaceResources transformer from @angular/compiler-cli with useESM true 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.AppComponent = void 0;
Expand All @@ -44,3 +43,45 @@ AppComponent = tslib_1.__decorate([
exports.AppComponent = AppComponent;
//# "
`;

exports[`Replace resources transformer with isolatedModules true should use replaceResources transformer from @angular/compiler-cli with useESM 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;
//# "
`;

exports[`Replace resources transformer with isolatedModules true should use replaceResources transformer from @angular/compiler-cli with useESM true 1`] = `
"import { __decorate } from \\"tslib\\";
import __NG_CLI_RESOURCE__0 from \\"./app.component.html\\";
import { Component } from '@angular/core';
let AppComponent = class AppComponent {
constructor() {
this.title = 'test-app-v10';
}
};
AppComponent = __decorate([
Component({
selector: 'app-root',
template: __NG_CLI_RESOURCE__0,
styles: []
})
], AppComponent);
export { AppComponent };
//# "
`;
47 changes: 0 additions & 47 deletions src/__tests__/__snapshots__/strip-styles.spec.ts.snap

This file was deleted.

12 changes: 10 additions & 2 deletions src/__tests__/hoisting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ import { mockFolder } from './__helpers__/test-helpers';

describe('Hoisting', () => {
// Verify if we use `ts-jest` hoisting transformer
test('should hoist correctly', () => {
const ngJestConfig = new NgJestConfig(jestCfgStub);
test.each([true, false])('should hoist correctly with isolatedModules %p', (isolatedModules) => {
const ngJestConfig = new NgJestConfig({
...jestCfgStub,
globals: {
'ts-jest': {
...jestCfgStub.globals['ts-jest'],
isolatedModules,
},
},
});
const fileName = join(mockFolder, 'foo.spec.ts');
const compiler = new NgJestCompiler(ngJestConfig, new Map());

Expand Down
Loading

0 comments on commit 76c25d2

Please sign in to comment.