Skip to content

Commit

Permalink
refactor(presets): mark inline-files and strip-styles as internal…
Browse files Browse the repository at this point in the history
… transformersl (#709)

BREAKING CHANGE
- `jest-preset-angular` now uses internally the 2 transformers `inline-files` and `strip-styles` for code compilation. These 2 transformers are no longer exposed as public transformers. One currently uses in jest config should remove `astTransformers` option in`ts-jest` config.

- For users who migrate from **v8.3.2** to **9.0.0**, any references to `'jest-preset-angular/build/InlineFilesTransformer'` and `'jest-preset-angular/build/StripStylesTransformer'` should be removed from jest config.

- For users who migrate from **9.0.0-next.4**, any references to `jest-preset-angular/build/transformers/inline-files` and `jest-preset-angular/build/transformers/inline-files` should be removed from jest config.
  • Loading branch information
ahnpnl committed Jan 5, 2021
1 parent ee5f1fa commit 68237fe
Show file tree
Hide file tree
Showing 21 changed files with 213 additions and 199 deletions.
3 changes: 0 additions & 3 deletions e2e/test-app-v10/jest-cjs-iso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
isolatedModules: true,
}
},
Expand Down
3 changes: 0 additions & 3 deletions e2e/test-app-v10/jest-cjs-uniso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
}
},
};
3 changes: 0 additions & 3 deletions e2e/test-app-v10/jest-esm-iso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
useESM: true,
isolatedModules: true,
}
Expand Down
3 changes: 0 additions & 3 deletions e2e/test-app-v11/jest-cjs-iso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
isolatedModules: true,
}
},
Expand Down
3 changes: 0 additions & 3 deletions e2e/test-app-v11/jest-cjs-uniso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
}
},
};
3 changes: 0 additions & 3 deletions e2e/test-app-v11/jest-esm-iso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
useESM: true,
isolatedModules: true,
}
Expand Down
3 changes: 0 additions & 3 deletions e2e/test-app-v9/jest-cjs-iso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
isolatedModules: true,
}
},
Expand Down
3 changes: 0 additions & 3 deletions e2e/test-app-v9/jest-cjs-uniso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
}
},
};
3 changes: 0 additions & 3 deletions e2e/test-app-v9/jest-esm-iso.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module.exports = {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: require('jest-preset-angular/build/transformers'),
},
useESM: true,
isolatedModules: true,
}
Expand Down
4 changes: 0 additions & 4 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const customTransformers = require('./build/transformers');
const snapshotSerializers = require('./build/serializers');

module.exports = {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: {
before: customTransformers,
},
},
},
testEnvironment: 'jsdom',
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ module.exports = {
tsconfig: 'tsconfig.spec.json',
},
},
testPathIgnorePatterns: ['/e2e/', '/src/__tests__/__mocks__/'],
testPathIgnorePatterns: ['/e2e/', '/src/__tests__/__mocks__/', '/src/__tests__/__helpers__/'],
};
12 changes: 12 additions & 0 deletions src/__tests__/__helpers__/test-constants.ts
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
14 changes: 14 additions & 0 deletions src/__tests__/__mocks__/forward-ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { forwardRef, Inject } from '@angular/core';

export class Door {
lock: Lock;

// Door attempts to inject Lock, despite it not being defined yet.
// forwardRef makes this possible.
constructor(@Inject(forwardRef(() => Lock)) lock: Lock) {
this.lock = lock;
}
}

// Only at this point Lock is defined.
export class Lock {}
48 changes: 48 additions & 0 deletions src/__tests__/__snapshots__/downlevel-ctor.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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`] = `
"\\"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 {
// 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;
// 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`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Lock = exports.Door = void 0;
const core_1 = require(\\"@angular/core\\");
class Door {
// Door attempts to inject Lock, despite it not being defined yet.
// forwardRef makes this possible.
constructor(lock) {
this.lock = lock;
}
}
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;
//# "
`;
10 changes: 10 additions & 0 deletions src/__tests__/__snapshots__/hoisting.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Hoisting should hoist correctly 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
jest.mock('./foo');
const foo_1 = require(\\"./foo\\");
console.log(foo_1.getFoo());
//# "
`;
65 changes: 2 additions & 63 deletions src/__tests__/__snapshots__/ng-jest-compiler.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,66 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`NgJestCompiler with isolatedModule false should hoist correctly 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
jest.mock('./foo');
const foo_1 = require(\\"./foo\\");
console.log(foo_1.getFoo());
//# "
`;
exports[`NgJestCompiler with isolatedModule false should throw diagnostics error for new file which is: known by Program 1`] = `"src/__tests__/__mocks__/foo.component.ts(8,3): error TS2322: Type '\\"test-app-v10\\"' is not assignable to type 'number'."`;

exports[`NgJestCompiler with isolatedModule false should return compiled result for existing file which is known by Program 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',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
], AppComponent);
exports.AppComponent = AppComponent;
//# "
`;

exports[`NgJestCompiler with isolatedModule false should return compiled result for new file which is not known by Program 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',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
], AppComponent);
exports.AppComponent = AppComponent;
//# "
`;

exports[`NgJestCompiler with isolatedModule false should throw diagnostics error for existing file which is known by Program 1`] = `"src/__tests__/__mocks__/foo.component.ts(8,3): error TS2322: Type '\\"test-app-v10\\"' is not assignable to type 'number'."`;

exports[`NgJestCompiler with isolatedModule false should throw diagnostics error for new file which is not known by Program 1`] = `"foo.ts(8,3): error TS2322: Type '\\"test-app-v10\\"' is not assignable to type 'number'."`;

exports[`NgJestCompiler with isolatedModules true should use hoisting transformer from ts-jest 1`] = `
Object {
"before": Array [
[Function],
],
}
`;
exports[`NgJestCompiler with isolatedModule false should throw diagnostics error for new file which is: not known by Program 1`] = `"src/__tests__/__mocks__/foo.component.ts(8,3): error TS2322: Type '\\"test-app-v10\\"' is not assignable to type 'number'."`;
35 changes: 35 additions & 0 deletions src/__tests__/downlevel-ctor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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-constants';
import { NgJestCompiler } from '../compiler/ng-jest-compiler';

describe('Downlevel ctor transformer', () => {
const fileName = join(__dirname, '__mocks__', 'forward-ref.ts');
const fileContent = readFileSync(fileName, 'utf-8');

test.each([true, false])(
'should/should not use downlevel ctor transformer from Angular for isolatedModules false/true',
(isolatedModules) => {
const ngJestConfig = new NgJestConfig({
...jestCfgStub,
globals: {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
'ts-jest': {
...jestCfgStub.globals['ts-jest'],
isolatedModules,
},
},
});
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();
},
);
});
22 changes: 22 additions & 0 deletions src/__tests__/hoisting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { SOURCE_MAPPING_PREFIX } from 'ts-jest/dist/compiler/compiler-utils';

import { NgJestCompiler } from '../compiler/ng-jest-compiler';
import { NgJestConfig } from '../config/ng-jest-config';
import { jestCfgStub } from './__helpers__/test-constants';

describe('Hoisting', () => {
// Verify if we use `ts-jest` hoisting transformer
test('should hoist correctly', () => {
const ngJestConfig = new NgJestConfig(jestCfgStub);
const fileName = join(__dirname, '__mocks__', 'foo.spec.ts');
const compiler = new NgJestCompiler(ngJestConfig, new Map());

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const emittedResult = compiler.getCompiledOutput(fileName, readFileSync(fileName, 'utf-8'), 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();
});
});

0 comments on commit 68237fe

Please sign in to comment.