Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/compiler-cli/test/transformers/program_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ import {TestSupport, expectNoDiagnosticsInProgram, setup} from '../test_support'
describe('ng program', () => {
let testSupport: TestSupport;
let errorSpy: jasmine.Spy&((s: string) => void);
// store ts version before altering it during unit tests
const realTsVersion = ts.version;

afterAll(() => { recoverRealTsVersion(); });

beforeEach(() => {
errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error);
testSupport = setup();
// make sure that whatever added future specs, the typescript version is correct is always
// correct in the beginning
recoverRealTsVersion();
});

function dangerouslySetTsVersion(version: string) { (ts as any).version = version; }

@benbraou benbraou Mar 11, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing the version of TypeScript, another approach is to just export a function from program.ts

export function checkTypeScriptVersion(version: string, options: CompilerOptions){
    if (version < '2.4.2' || (version >= '2.7.0' && !options.disableTypeScriptVersionCheck)) {
      throw new Error(
          `The Angular Compiler requires TypeScript >=2.4.2 and <2.7 but ${version} was found instead.`);
    }
}

And then perform assertions on it.
#22699 is however talking just about adding tests and not modifying the source file. Hence dangerouslySetTsVersion in the test file.
Please let me know if I need to change this


function recoverRealTsVersion() { (ts as any).version = realTsVersion; }

function createModuleAndCompSource(prefix: string, template: string = prefix + 'template') {
const templateEntry =
template.endsWith('.html') ? `templateUrl: '${template}'` : `template: \`${template}\``;
Expand Down Expand Up @@ -1033,4 +1044,44 @@ describe('ng program', () => {
.toContain('Function expressions are not supported');
});
});

describe('TypeScript versions', () => {

afterAll(() => { recoverRealTsVersion(); });

beforeEach(() => { recoverRealTsVersion(); });

const versionError = (version: string) =>
`The Angular Compiler requires TypeScript >=2.4.2 and <2.7 but ${version} was found instead.`;
function createProgramWithOptions(overrideOptions: ng.CompilerOptions = {}) {
testSupport.writeFiles({
'src/index.ts': createModuleAndCompSource('main'),
});
compile(undefined, overrideOptions);
}

it('should not throw when a supported TypeScript version is used', () => {
dangerouslySetTsVersion('2.4.2');
expect(() => createProgramWithOptions()).not.toThrow();
expect(() => createProgramWithOptions({disableTypeScriptVersionCheck: false})).not.toThrow();
expect(() => createProgramWithOptions({disableTypeScriptVersionCheck: true})).not.toThrow();
});

it('should throw when using a TypeScript version < the minimum supported one', () => {
dangerouslySetTsVersion('2.4.1');
expect(() => createProgramWithOptions()).toThrowError(versionError('2.4.1'));
expect(() => createProgramWithOptions({disableTypeScriptVersionCheck: false}))
.toThrowError(versionError('2.4.1'));
expect(() => createProgramWithOptions({disableTypeScriptVersionCheck: true}))
.toThrowError(versionError('2.4.1'));
});

it('should handle a TypeScript version > the maximum supported one', () => {
dangerouslySetTsVersion('2.7.0');
expect(() => createProgramWithOptions()).toThrowError(versionError('2.7.0'));
expect(() => createProgramWithOptions({disableTypeScriptVersionCheck: false}))
.toThrowError(versionError('2.7.0'));
expect(() => createProgramWithOptions({disableTypeScriptVersionCheck: true})).not.toThrow();
});
});
});