diff --git a/fixtures/test-utils/errors-no-wrapper-classes/errors-no-wrapper-classes.ts b/fixtures/test-utils/errors-no-wrapper-classes/index.ts similarity index 100% rename from fixtures/test-utils/errors-no-wrapper-classes/errors-no-wrapper-classes.ts rename to fixtures/test-utils/errors-no-wrapper-classes/index.ts diff --git a/fixtures/test-utils/errors-no-wrapper-classes/tsconfig.json b/fixtures/test-utils/errors-no-wrapper-classes/tsconfig.json index dccf2f4..3c43903 100644 --- a/fixtures/test-utils/errors-no-wrapper-classes/tsconfig.json +++ b/fixtures/test-utils/errors-no-wrapper-classes/tsconfig.json @@ -1,4 +1,3 @@ { - "extends": "../tsconfig.json", - "include": ["errors-no-wrapper-classes.ts"] + "extends": "../tsconfig.json" } diff --git a/fixtures/test-utils/exports/alert.ts b/fixtures/test-utils/exports/alert.ts new file mode 100644 index 0000000..3766182 --- /dev/null +++ b/fixtures/test-utils/exports/alert.ts @@ -0,0 +1,10 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { ElementWrapper } from './core'; + +export class AlertWrapper extends ElementWrapper { + findContent() { + return new ElementWrapper(); + } +} diff --git a/fixtures/test-utils/exports/button.ts b/fixtures/test-utils/exports/button.ts new file mode 100644 index 0000000..5cdb0b7 --- /dev/null +++ b/fixtures/test-utils/exports/button.ts @@ -0,0 +1,10 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { ElementWrapper } from './core'; + +export class ButtonWrapper extends ElementWrapper { + findText() { + return new ElementWrapper(); + } +} diff --git a/fixtures/test-utils/exports/core.ts b/fixtures/test-utils/exports/core.ts new file mode 100644 index 0000000..e49b19c --- /dev/null +++ b/fixtures/test-utils/exports/core.ts @@ -0,0 +1,3 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +export class ElementWrapper {} diff --git a/fixtures/test-utils/exports/index.ts b/fixtures/test-utils/exports/index.ts new file mode 100644 index 0000000..8d96a57 --- /dev/null +++ b/fixtures/test-utils/exports/index.ts @@ -0,0 +1,4 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +export { AlertWrapper } from './alert'; +export { ButtonWrapper } from './button'; diff --git a/fixtures/test-utils/exports/tsconfig.json b/fixtures/test-utils/exports/tsconfig.json new file mode 100644 index 0000000..3c43903 --- /dev/null +++ b/fixtures/test-utils/exports/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../tsconfig.json" +} diff --git a/src/components/type-utils.ts b/src/components/type-utils.ts index bd3b30d..2315457 100644 --- a/src/components/type-utils.ts +++ b/src/components/type-utils.ts @@ -107,3 +107,10 @@ export function extractDeclaration(symbol: ts.Symbol) { } return declarations[0]; } + +export function printFlags(flags: number, mapping: Record) { + return Object.entries(mapping) + .filter(([key, value]) => Number(key) & flags) + .map(([key, value]) => value) + .join(' | '); +} diff --git a/src/test-utils-new/extractor.ts b/src/test-utils-new/extractor.ts index 2850792..14fcdbd 100644 --- a/src/test-utils-new/extractor.ts +++ b/src/test-utils-new/extractor.ts @@ -43,11 +43,11 @@ export default function extractDocumentation( if (extraExports.includes(className)) { continue; } - if (!(symbol.flags & ts.SymbolFlags.Class)) { + const classType = checker.getDeclaredTypeOfSymbol(symbol); + if (!classType.isClass()) { throw new Error(`Exported symbol is not a class, got ${checker.symbolToString(symbol)}`); } - const classType = checker.getTypeAtLocation(extractDeclaration(symbol)); const classDefinition: TestUtilsDoc = { name: className, methods: [] }; for (const property of classType.getProperties()) { const declaration = property.valueDeclaration; diff --git a/test/components/type-utils.test.ts b/test/components/type-utils.test.ts new file mode 100644 index 0000000..7ae5837 --- /dev/null +++ b/test/components/type-utils.test.ts @@ -0,0 +1,21 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import ts from 'typescript'; +import { test, expect } from 'vitest'; +import { printFlags } from '../../lib/components/type-utils'; + +test('serialises node flags', () => { + expect( + printFlags( + ts.NodeFlags.HasAsyncFunctions + ts.NodeFlags.HasImplicitReturn + ts.NodeFlags.ContainsThis, + ts.NodeFlags + ) + ).toEqual('ContainsThis | HasImplicitReturn | ReachabilityCheckFlags | HasAsyncFunctions | ReachabilityAndEmitFlags'); +}); + +test('serialises type flags', () => { + expect(printFlags(ts.TypeFlags.Number + ts.TypeFlags.Enum, ts.TypeFlags)).toEqual( + 'Number | Enum | NumberLike | EnumLike | PossiblyFalsy | Primitive | NotPrimitiveUnion | Singleton | Intrinsic | IncludesMask | DisjointDomains | DefinitelyNonNullable | Narrowable' + ); +}); diff --git a/test/test-utils/doc-generation.test.ts b/test/test-utils/doc-generation.test.ts index ed9770d..3d37cbe 100644 --- a/test/test-utils/doc-generation.test.ts +++ b/test/test-utils/doc-generation.test.ts @@ -83,6 +83,15 @@ describe('Generate documentation', () => { expect(childClassMethod?.inheritedFrom).toBeUndefined(); }); + test('deal with re-exports', () => { + const results = buildTestUtilsProject('exports'); + expect(results.map(classDoc => classDoc.name)).toEqual(['AlertWrapper', 'ButtonWrapper']); + const alertWrapper = results.find(classDoc => classDoc.name === 'AlertWrapper')!; + expect(alertWrapper.methods.map(method => method.name)).toEqual(['findContent']); + const buttonWrapper = results.find(classDoc => classDoc.name === 'ButtonWrapper')!; + expect(buttonWrapper.methods.map(method => method.name)).toEqual(['findText']); + }); + test('default value rendering', () => { const results = buildTestUtilsProject('default-values'); expect(results.length).toBe(1); diff --git a/test/test-utils/usage.test.ts b/test/test-utils/usage.test.ts index 0df73fb..97fee69 100644 --- a/test/test-utils/usage.test.ts +++ b/test/test-utils/usage.test.ts @@ -16,6 +16,12 @@ describe('documentTestUtils throws error for ', () => { expect(() => buildTestUtilsProject('errors-empty')).toThrow('Failed to parse tsconfig.json'); }); + test('throws error on unknown export', () => { + expect(() => buildTestUtilsProject('errors-no-wrapper-classes')).toThrow( + 'Exported symbol is not a class, got speakTruth' + ); + }); + test('having no input files because of a non-matching glob', () => { expect(() => buildTestUtilsProject('simple', {