-
Notifications
You must be signed in to change notification settings - Fork 4
chore: Update new test-utils documenter #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,26 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| export class TestUtilWrapper { | ||
| /** | ||
| * Generic return value | ||
| */ | ||
| findAll(): Array<HTMLElement> { | ||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * Generic arguments | ||
| */ | ||
| setAll(all: Array<HTMLElement>) {} | ||
|
|
||
| /** | ||
| * Method overload example | ||
| */ | ||
| keydown(keyCode: number): void; | ||
| keydown(keyboardEventProps: KeyboardEventInit): void; | ||
| keydown(args: KeyboardEventInit | number) {} | ||
| } | ||
|
|
||
| export default function createWrapper() { | ||
| return new TestUtilWrapper(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,11 @@ function getDefaultValue(declaration: ts.Declaration) { | |
| return declaration.initializer.getText(); | ||
| } | ||
|
|
||
| export default function extractDocumentation(sourceFile: ts.SourceFile, checker: ts.TypeChecker): Array<TestUtilsDoc> { | ||
| export default function extractDocumentation( | ||
| sourceFile: ts.SourceFile, | ||
| checker: ts.TypeChecker, | ||
| extraExports: Array<string> | ||
| ): Array<TestUtilsDoc> { | ||
| const moduleSymbol = checker.getSymbolAtLocation(sourceFile); | ||
| if (!moduleSymbol) { | ||
| throw new Error(`Unable to resolve module: ${sourceFile.fileName}`); | ||
|
|
@@ -35,14 +39,21 @@ export default function extractDocumentation(sourceFile: ts.SourceFile, checker: | |
| const definitions: Array<TestUtilsDoc> = []; | ||
|
|
||
| for (const symbol of exportSymbols) { | ||
| const className = symbol.getName(); | ||
| if (extraExports.includes(className)) { | ||
| continue; | ||
| } | ||
| if (!(symbol.flags & ts.SymbolFlags.Class)) { | ||
| throw new Error(`Exported symbol is not a class, got ${checker.symbolToString(symbol)}`); | ||
| } | ||
| const className = symbol.getName(); | ||
|
|
||
| const classType = checker.getTypeAtLocation(extractDeclaration(symbol)); | ||
| const classDefinition: TestUtilsDoc = { name: className, methods: [] }; | ||
| for (const property of classType.getProperties()) { | ||
| const declaration = extractDeclaration(property); | ||
| const declaration = property.valueDeclaration; | ||
| if (!declaration) { | ||
| throw new Error(`Unexpected member on ${className} – ${property.getName()}`); | ||
| } | ||
| const modifiers = (ts.canHaveModifiers(declaration) && ts.getModifiers(declaration)) || []; | ||
| if ( | ||
| modifiers.find( | ||
|
|
@@ -52,26 +63,25 @@ export default function extractDocumentation(sourceFile: ts.SourceFile, checker: | |
| continue; | ||
| } | ||
| const type = checker.getTypeAtLocation(declaration); | ||
| if (type.getCallSignatures().length !== 1) { | ||
| throw new Error(`Unexpected member on ${className} – ${property.getName()}: ${stringifyType(type, checker)}`); | ||
| for (const signature of type.getCallSignatures()) { | ||
| // report each function signature as a separate method | ||
| classDefinition.methods.push({ | ||
| name: property.getName(), | ||
| description: getDescription(property.getDocumentationComment(checker), declaration).text, | ||
| returnType: { name: stringifyType(signature.getReturnType(), checker) }, | ||
| parameters: signature.parameters.map(parameter => { | ||
| const paramType = checker.getTypeAtLocation(extractDeclaration(parameter)); | ||
| return { | ||
| name: parameter.name, | ||
| typeName: stringifyType(paramType, checker), | ||
| description: getDescription(parameter.getDocumentationComment(checker), declaration).text, | ||
| flags: { isOptional: isOptional(paramType) }, | ||
| defaultValue: getDefaultValue(extractDeclaration(parameter)), | ||
| }; | ||
| }), | ||
| inheritedFrom: getInheritedFrom(declaration, className), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed fields order to maintain compatibility with previous snapshot tests |
||
| }); | ||
| } | ||
| const returnType = type.getCallSignatures()[0].getReturnType(); | ||
| classDefinition.methods.push({ | ||
| name: property.getName(), | ||
| description: getDescription(property.getDocumentationComment(checker), declaration).text, | ||
| inheritedFrom: getInheritedFrom(declaration, className), | ||
| parameters: type.getCallSignatures()[0].parameters.map(parameter => { | ||
| const paramType = checker.getTypeAtLocation(extractDeclaration(parameter)); | ||
| return { | ||
| name: parameter.name, | ||
| typeName: stringifyType(paramType, checker), | ||
| description: getDescription(parameter.getDocumentationComment(checker), declaration).text, | ||
| flags: { isOptional: isOptional(paramType) }, | ||
| defaultValue: getDefaultValue(extractDeclaration(parameter)), | ||
| }; | ||
| }), | ||
| returnType: { name: stringifyType(returnType, checker) }, | ||
| }); | ||
| } | ||
| classDefinition.methods.sort((a, b) => a.name.localeCompare(b.name)); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,15 @@ import { bootstrapTypescriptProject } from '../bootstrap/typescript'; | |
| import extractDocumentation from './extractor'; | ||
| import { TestUtilsDoc } from '../test-utils/interfaces'; | ||
|
|
||
| export interface TestUtilsVariantOptions { | ||
| root: string; | ||
| extraExports?: Array<string>; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra exports could be different for DOM and selectors code, this is why it is a separate config for each |
||
| } | ||
|
|
||
| export interface TestUtilsDocumenterOptions { | ||
| tsconfigPath: string; | ||
| domUtilsRoot: string; | ||
| selectorsUtilsRoot: string; | ||
| domUtils: TestUtilsVariantOptions; | ||
| selectorsUtils: TestUtilsVariantOptions; | ||
| } | ||
|
|
||
| interface TestUtilsDefinitions { | ||
|
|
@@ -18,8 +23,8 @@ interface TestUtilsDefinitions { | |
| } | ||
|
|
||
| export function documentTestUtilsNew(options: TestUtilsDocumenterOptions): TestUtilsDefinitions { | ||
| const domUtilsRoot = pathe.resolve(options.domUtilsRoot); | ||
| const selectorsUtilsRoot = pathe.resolve(options.selectorsUtilsRoot); | ||
| const domUtilsRoot = pathe.resolve(options.domUtils.root); | ||
| const selectorsUtilsRoot = pathe.resolve(options.selectorsUtils.root); | ||
| const program = bootstrapTypescriptProject(options.tsconfigPath); | ||
| const checker = program.getTypeChecker(); | ||
|
|
||
|
|
@@ -33,8 +38,8 @@ export function documentTestUtilsNew(options: TestUtilsDocumenterOptions): TestU | |
| throw new Error(`File '${selectorsUtilsFile}' not found`); | ||
| } | ||
| return { | ||
| domDefinitions: extractDocumentation(domUtilsFile, checker), | ||
| selectorsDefinitions: extractDocumentation(selectorsUtilsFile, checker), | ||
| domDefinitions: extractDocumentation(domUtilsFile, checker, options.domUtils.extraExports ?? []), | ||
| selectorsDefinitions: extractDocumentation(selectorsUtilsFile, checker, options.selectorsUtils.extraExports ?? []), | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,15 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { documentTestUtilsNew, TestUtilsDocumenterOptions } from '../../src/test-utils-new'; | ||
| import { documentTestUtilsNew, TestUtilsVariantOptions } from '../../src/test-utils-new'; | ||
| import { TestUtilsDoc } from '../../src/test-utils/interfaces'; | ||
|
|
||
| export function buildTestUtilsProject( | ||
| name: string, | ||
| configOverrides?: Partial<TestUtilsDocumenterOptions> | ||
| configOverrides?: Partial<TestUtilsVariantOptions> | ||
| ): TestUtilsDoc[] { | ||
| return documentTestUtilsNew({ | ||
| tsconfigPath: require.resolve(`../../fixtures/test-utils/${name}/tsconfig.json`), | ||
| domUtilsRoot: `fixtures/test-utils/${name}/index.ts`, | ||
| selectorsUtilsRoot: `fixtures/test-utils/${name}/index.ts`, | ||
| ...configOverrides, | ||
| domUtils: { root: `fixtures/test-utils/${name}/index.ts`, ...configOverrides }, | ||
| selectorsUtils: { root: `fixtures/test-utils/${name}/index.ts`, ...configOverrides }, | ||
| }).domDefinitions; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is not covered in tests. Because typescript says
valueDeclarationcould be undefined, but it always exists in our cases