Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"extends": "../tsconfig.json",
"include": ["errors-no-wrapper-classes.ts"]
"extends": "../tsconfig.json"
}
10 changes: 10 additions & 0 deletions fixtures/test-utils/exports/alert.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
10 changes: 10 additions & 0 deletions fixtures/test-utils/exports/button.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 3 additions & 0 deletions fixtures/test-utils/exports/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
export class ElementWrapper {}
4 changes: 4 additions & 0 deletions fixtures/test-utils/exports/index.ts
Original file line number Diff line number Diff line change
@@ -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';
3 changes: 3 additions & 0 deletions fixtures/test-utils/exports/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
7 changes: 7 additions & 0 deletions src/components/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ export function extractDeclaration(symbol: ts.Symbol) {
}
return declarations[0];
}

export function printFlags(flags: number, mapping: Record<number, string>) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Not used in the final version of the code, but it was useful in local debugging to convert binary flags into readable strings

Let's keep it in the code so I will not have to remember how it is done every time

return Object.entries(mapping)
.filter(([key, value]) => Number(key) & flags)
.map(([key, value]) => value)
.join(' | ');
}
4 changes: 2 additions & 2 deletions src/test-utils-new/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

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

The actual fix. Replace getTypeAtLocation with getDeclaredTypeOfSymbol to make typescript actually go cross-files to find the real type

const classDefinition: TestUtilsDoc = { name: className, methods: [] };
for (const property of classType.getProperties()) {
const declaration = property.valueDeclaration;
Expand Down
21 changes: 21 additions & 0 deletions test/components/type-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
9 changes: 9 additions & 0 deletions test/test-utils/doc-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions test/test-utils/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member Author

Choose a reason for hiding this comment

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

This fixture existed, but was never used 🤦

'Exported symbol is not a class, got speakTruth'
);
});

test('having no input files because of a non-matching glob', () => {
expect(() =>
buildTestUtilsProject('simple', {
Expand Down
Loading