Skip to content

Commit

Permalink
feat(compiler): use constructor downlevel ctor transformer for `isola…
Browse files Browse the repository at this point in the history
…tedModules: true` (#792)
  • Loading branch information
ahnpnl authored Feb 9, 2021
1 parent 7b79f47 commit 00c71ce
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 26 deletions.
9 changes: 1 addition & 8 deletions e2e/__tests__/forward-ref/forward-ref.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { forwardRef, Inject, Injector } from '@angular/core';

const shouldSkipTest = process.env.ISOLATED_MODULES === 'true';
const skipTest = shouldSkipTest ? test.skip : test;

if (shouldSkipTest) {
process.stdout.write('\nforwardRef only works in non-isolatedModules mode\n');
}

skipTest('forwardRef should work', () => {
test('forwardRef should work', () => {
class Door {
lock: Lock;

Expand Down
15 changes: 7 additions & 8 deletions src/__tests__/__snapshots__/downlevel-ctor.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Downlevel ctor transformer should/should not use downlevel ctor transformer from Angular for isolatedModules false/true 1`] = `
exports[`Downlevel ctor transformer should use downlevel ctor transformer from Angular for isolatedModules false/true 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Lock = exports.Door = void 0;
const tslib_1 = require(\\"tslib\\");
const core_1 = require(\\"@angular/core\\");
let Door = class Door {
class Door {
// Door attempts to inject Lock, despite it not being defined yet.
// forwardRef makes this possible.
constructor(lock) {
this.lock = lock;
}
};
Door = tslib_1.__decorate([
tslib_1.__param(0, core_1.Inject(core_1.forwardRef(() => Lock)))
], Door);
}
exports.Door = Door;
Door.ctorParameters = () => [
{ type: Lock, decorators: [{ type: core_1.Inject, args: [core_1.forwardRef(() => Lock),] }] }
];
// Only at this point Lock is defined.
class Lock {
}
exports.Lock = Lock;
//# "
`;

exports[`Downlevel ctor transformer should/should not use downlevel ctor transformer from Angular for isolatedModules false/true 2`] = `
exports[`Downlevel ctor transformer should use downlevel ctor transformer from Angular for isolatedModules false/true 2`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Lock = exports.Door = void 0;
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/downlevel-ctor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Downlevel ctor transformer', () => {
const fileContent = readFileSync(fileName, 'utf-8');

test.each([true, false])(
'should/should not use downlevel ctor transformer from Angular for isolatedModules false/true',
'should use downlevel ctor transformer from Angular for isolatedModules false/true',
(isolatedModules) => {
const ngJestConfig = new NgJestConfig({
...jestCfgStub,
Expand Down
11 changes: 2 additions & 9 deletions src/compiler/ng-jest-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,13 @@ export class NgJestCompiler implements CompilerInstance {
}

private makeTransformers(customTransformers: TsJestAstTransformer, program: ts.Program): ts.CustomTransformers {
let baseTransformers = {
return {
before: [
...customTransformers.before.map((beforeTransformer) =>
beforeTransformer.factory({ configSet: this.ngJestConfig, program }, beforeTransformer.options),
),
replaceResources({ program } as TsCompilerInstance),
constructorDownlevelCtor({ program } as TsCompilerInstance),
] as Array<ts.TransformerFactory<ts.SourceFile> | ts.CustomTransformerFactory>,
after: customTransformers.after.map((afterTransformer) =>
afterTransformer.factory({ configSet: this.ngJestConfig, program }, afterTransformer.options),
Expand All @@ -302,13 +303,5 @@ export class NgJestCompiler implements CompilerInstance {
afterDeclarations.factory({ configSet: this.ngJestConfig, program }, afterDeclarations.options),
) as Array<ts.TransformerFactory<ts.SourceFile | ts.Bundle>>,
};
if (!this.ngJestConfig.isolatedModules) {
baseTransformers = {
...baseTransformers,
before: [...baseTransformers.before, constructorDownlevelCtor({ program } as TsCompilerInstance)],
};
}

return baseTransformers;
}
}

0 comments on commit 00c71ce

Please sign in to comment.