2018-03-01 13:41:35 -05:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2018-03-01 13:41:35 -05:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-03-02 17:19:01 -05:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2021-04-08 03:26:53 -04:00
|
|
|
|
2018-03-02 17:19:01 -05:00
|
|
|
import * as main from '../lib/main';
|
2021-04-08 03:26:53 -04:00
|
|
|
|
|
|
|
import {assertFileEqual} from './helpers';
|
2018-03-02 17:19:01 -05:00
|
|
|
|
|
|
|
describe('integration test: public api', () => {
|
2018-03-01 13:41:35 -05:00
|
|
|
let _warn: any = null;
|
2018-03-02 17:19:01 -05:00
|
|
|
let warnings: string[] = [];
|
|
|
|
beforeEach(() => {
|
|
|
|
_warn = console.warn;
|
|
|
|
console.warn = (...args: string[]) => warnings.push(args.join(' '));
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
console.warn = _warn;
|
|
|
|
warnings = [];
|
|
|
|
_warn = null;
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should handle empty files', () => {
|
|
|
|
check('test/fixtures/empty.d.ts', 'test/fixtures/empty_expected.d.ts');
|
|
|
|
});
|
2018-03-02 17:19:01 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should include symbols', () => {
|
|
|
|
check('test/fixtures/simple.d.ts', 'test/fixtures/simple_expected.d.ts');
|
|
|
|
});
|
2018-03-02 17:19:01 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should include symbols reexported explicitly', () => {
|
|
|
|
check('test/fixtures/reexported.d.ts', 'test/fixtures/reexported_expected.d.ts');
|
|
|
|
});
|
2018-03-02 17:19:01 -05:00
|
|
|
|
|
|
|
it('should include symbols reexported with *', () => {
|
|
|
|
check('test/fixtures/reexported_star.d.ts', 'test/fixtures/reexported_star_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include members of classes and interfaces', () => {
|
|
|
|
check(
|
|
|
|
'test/fixtures/classes_and_interfaces.d.ts',
|
|
|
|
'test/fixtures/classes_and_interfaces_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
2018-11-26 17:44:45 -05:00
|
|
|
it('should include value and type', () => {
|
|
|
|
check(
|
|
|
|
'test/fixtures/exports_type_and_value.d.ts',
|
|
|
|
'test/fixtures/exports_type_and_value_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
2018-03-02 17:19:01 -05:00
|
|
|
it('should include members reexported classes', () => {
|
|
|
|
check(
|
|
|
|
'test/fixtures/reexported_classes.d.ts', 'test/fixtures/reexported_classes_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove reexported external symbols', () => {
|
|
|
|
check('test/fixtures/reexported_extern.d.ts', 'test/fixtures/reexported_extern_expected.d.ts');
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(warnings).toEqual([
|
2018-03-02 17:19:01 -05:00
|
|
|
'test/fixtures/reexported_extern.d.ts(5,1): error: No export declaration found for symbol "CompilerHost"'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support type literals', () => {
|
|
|
|
check('test/fixtures/type_literals.d.ts', 'test/fixtures/type_literals_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow enums as types', () => {
|
|
|
|
check('test/fixtures/enum_as_type.d.ts', 'test/fixtures/enum_as_type_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw on passing a .ts file as an input', () => {
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(() => main.publicApi('test/fixtures/empty.ts'))
|
|
|
|
.toThrowError('Source file "test/fixtures/empty.ts" is not a declaration file');
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should respect serialization options', () => {
|
|
|
|
check(
|
|
|
|
'test/fixtures/underscored.d.ts', 'test/fixtures/underscored_expected.d.ts',
|
|
|
|
{stripExportPattern: /^__.*/});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('integration test: generateGoldenFile', () => {
|
2018-03-01 13:41:35 -05:00
|
|
|
const outDir = path.join(process.env['TEST_TMPDIR'], 'tmp');
|
2018-03-02 17:19:01 -05:00
|
|
|
const outFile = path.join(outDir, 'out.d.ts');
|
|
|
|
const deepOutFile = path.join(outDir, 'a/b/c/out.d.ts');
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
if (!fs.existsSync(outDir)) {
|
|
|
|
fs.mkdirSync(outDir);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
afterEach(() => {
|
2021-04-08 03:26:53 -04:00
|
|
|
fs.rmdirSync(outDir, {recursive: true});
|
2020-04-13 19:40:21 -04:00
|
|
|
});
|
2018-03-02 17:19:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
it('should generate a golden file', () => {
|
|
|
|
main.generateGoldenFile('test/fixtures/reexported_classes.d.ts', outFile);
|
|
|
|
assertFileEqual(outFile, 'test/fixtures/reexported_classes_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a golden file with any ancestor directory created', () => {
|
|
|
|
main.generateGoldenFile('test/fixtures/reexported_classes.d.ts', deepOutFile);
|
|
|
|
assertFileEqual(deepOutFile, 'test/fixtures/reexported_classes_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should respect serialization options', () => {
|
|
|
|
main.generateGoldenFile(
|
|
|
|
'test/fixtures/underscored.d.ts', outFile, {stripExportPattern: /^__.*/});
|
|
|
|
assertFileEqual(outFile, 'test/fixtures/underscored_expected.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a golden file with keyof', () => {
|
|
|
|
main.generateGoldenFile('test/fixtures/keyof.d.ts', outFile);
|
|
|
|
assertFileEqual(outFile, 'test/fixtures/keyof_expected.d.ts');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('integration test: verifyAgainstGoldenFile', () => {
|
|
|
|
it('should check an entrypoint against a golden file on equal', () => {
|
|
|
|
const diff = main.verifyAgainstGoldenFile(
|
|
|
|
'test/fixtures/reexported_classes.d.ts', 'test/fixtures/reexported_classes_expected.d.ts');
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(diff).toBe('');
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should check an entrypoint against a golden file with proper diff message', () => {
|
|
|
|
const diff = main.verifyAgainstGoldenFile(
|
|
|
|
'test/fixtures/verify_entrypoint.d.ts', 'test/fixtures/verify_expected.d.ts');
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(diff).toBe(fs.readFileSync('test/fixtures/verify.patch').toString());
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should respect serialization options', () => {
|
|
|
|
const diff = main.verifyAgainstGoldenFile(
|
|
|
|
'test/fixtures/underscored.d.ts', 'test/fixtures/underscored_expected.d.ts',
|
|
|
|
{stripExportPattern: /^__.*/});
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(diff).toBe('');
|
2018-03-02 17:19:01 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function check(sourceFile: string, expectedFile: string, options: main.SerializationOptions = {}) {
|
2021-04-08 03:26:53 -04:00
|
|
|
expect(main.publicApi(sourceFile, options)).toBe(fs.readFileSync(expectedFile).toString());
|
2018-03-02 17:19:01 -05:00
|
|
|
}
|