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
3 changes: 1 addition & 2 deletions fixtures/test-utils/advanced-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"extends": "../tsconfig.json",
"include": ["advanced-types.ts"]
"extends": "../tsconfig.json"
}
9 changes: 9 additions & 0 deletions fixtures/test-utils/default-values/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
export class DefaultValueWrapper {
selectOption(index = 1): void {}

openDropdown(expandToViewport = false): void {}

getColumns(order: 'first' | 'last' = 'first'): void {}
}
3 changes: 3 additions & 0 deletions fixtures/test-utils/default-values/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.json"
}
3 changes: 1 addition & 2 deletions fixtures/test-utils/inheritance/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"extends": "../tsconfig.json",
"include": ["inheritance.ts"]
"extends": "../tsconfig.json"
}
1 change: 0 additions & 1 deletion fixtures/test-utils/simple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"extends": "../tsconfig.json",
"include": ["simple.ts"]
}
1 change: 1 addition & 0 deletions src/bootstrap/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function loadTSConfig(tsconfigPath: string): ts.ParsedCommandLine {
}
const config = ts.parseJsonConfigFileContent(configFile.config, ts.sys, pathe.dirname(tsconfigPath));
if (config.errors.length > 0) {
console.error(...config.errors);
throw new Error('Failed to parse tsconfig.json');
}
// this prints a warning that incremental mode is not supported in programmatic API
Expand Down
5 changes: 2 additions & 3 deletions src/test-utils/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export interface Parameter {
name: string;
typeName?: string;
Expand All @@ -19,8 +18,8 @@ export interface TestUtilMethod {
description?: string;
returnType?: {
name: string;
type: string;
typeArguments: Array<TypeArgument>;
type?: string;
typeArguments?: Array<TypeArgument>;
Copy link
Member Author

Choose a reason for hiding this comment

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

These can be undefined, but because the current code (ab)uses as any too much, typecheck does not see it

};
parameters: Array<Parameter>;
inheritedFrom?: {
Expand Down
11 changes: 0 additions & 11 deletions test/components/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import pathe from 'pathe';
import ts from 'typescript';
import { ProjectReflection } from 'typedoc';
import { documentComponents } from '../../src/components';
import { documentTestUtils } from '../../src/test-utils';
import { bootstrapProject } from '../../src/bootstrap';
import { TestUtilsDoc } from '../../src/test-utils/interfaces';
import { DocumenterOptions } from '../../src/components';

export function buildProject(name: string, options?: Partial<DocumenterOptions>) {
Expand All @@ -23,15 +21,6 @@ export function getTemporaryDir() {
return fs.mkdtempSync(pathe.join(os.tmpdir(), 'documenter-'));
}

export function buildTestUtilsProject(name: string, testGlob?: string): TestUtilsDoc[] {
return documentTestUtils(
{
tsconfig: require.resolve(`../../fixtures/test-utils/${name}/tsconfig.json`),
},
testGlob || `fixtures/test-utils/${name}/**/*`
);
}

export function buildCustomProject(tsConfig: string, testGlob: string): ProjectReflection {
const project = bootstrapProject(
{
Expand Down
64 changes: 54 additions & 10 deletions test/test-utils/doc-generation.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { describe, expect, test } from 'vitest';
import { buildTestUtilsProject } from '../components/test-helpers';
import { buildTestUtilsProject } from './test-helpers';

describe('Generate documentation', () => {
test('For simple cases', () => {
Expand All @@ -25,7 +25,7 @@ describe('Generate documentation', () => {
expect(noOpMethod?.inheritedFrom).toBeUndefined();

const findStringMethod = methods.find(method => method.name === 'findString');
expect(findStringMethod).toBeDefined;
expect(findStringMethod).toBeDefined();
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 assertion did not work

expect(findStringMethod?.returnType).toEqual({ name: 'string', type: 'intrinsic' });
expect(findStringMethod?.parameters).toEqual([]);
expect(findStringMethod?.description).toBe(
Expand All @@ -34,22 +34,20 @@ describe('Generate documentation', () => {
expect(findStringMethod?.inheritedFrom).toBeUndefined();

const setStringMethod = methods.find(method => method.name === 'setString');
expect(setStringMethod).toBeDefined;
expect(setStringMethod).toBeDefined();
expect(setStringMethod?.returnType).toEqual({ name: 'void', type: 'intrinsic' });
expect(setStringMethod?.parameters).toMatchSnapshot();
expect(setStringMethod?.description).toBe('Short Text');
expect(setStringMethod?.inheritedFrom).toBeUndefined();

const findObjectMethod = methods.find(method => method.name === 'findObject');
expect(findObjectMethod).toBeDefined;
expect(findObjectMethod).toBeDefined();
expect(findObjectMethod?.returnType).toEqual({ name: 'TestReturnType', type: 'reference' });
expect(findObjectMethod?.parameters).toEqual([]);
expect(findObjectMethod?.description).toBe('Short Text.\nLong Text.\n');
expect(findObjectMethod?.inheritedFrom).toBeUndefined();
});

// TODO: Testing here could me more exhaustive, but we do testing for types in different tests already.
// Try to reuse existing type extraction utils in another step. Adjust testing accordingly.
test('deal with more complex types', () => {
const results = buildTestUtilsProject('advanced-types');

Expand All @@ -62,7 +60,7 @@ describe('Generate documentation', () => {
expect(methods.length).toBe(2);

const findAllMethod = methods.find(method => method.name === 'findAll');
expect(findAllMethod).toBeDefined;
expect(findAllMethod).toBeDefined();
expect(findAllMethod?.returnType).toEqual({
name: 'Array',
type: 'reference',
Expand All @@ -78,7 +76,7 @@ describe('Generate documentation', () => {
expect(findAllMethod?.inheritedFrom).toBeUndefined();

const setAllMethod = methods.find(method => method.name === 'setAll');
expect(setAllMethod).toBeDefined;
expect(setAllMethod).toBeDefined();
expect(setAllMethod?.returnType).toEqual({
name: 'void',
type: 'intrinsic',
Expand All @@ -100,13 +98,59 @@ describe('Generate documentation', () => {
expect(methods.length).toBe(2);

const inheritedMethod = methods.find(method => method.name === 'inheritedMethod');
expect(inheritedMethod).toBeDefined;
expect(inheritedMethod).toBeDefined();
expect(inheritedMethod?.inheritedFrom).toEqual({
name: 'AbstractWrapper.inheritedMethod',
});

const childClassMethod = methods.find(method => method.name === 'childClassMethod');
expect(childClassMethod).toBeDefined;
expect(childClassMethod).toBeDefined();
expect(childClassMethod?.inheritedFrom).toBeUndefined();
});

test('default value rendering', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Add missing test

const results = buildTestUtilsProject('default-values');
expect(results.length).toBe(1);

const classDoc = results.find(classDoc => classDoc.name === 'DefaultValueWrapper');
expect(classDoc).toBeDefined();

expect(classDoc!.methods).toEqual([
{
name: 'getColumns',
parameters: [
{
name: 'order',
flags: { isOptional: false },
defaultValue: '"first"',
},
],
returnType: { name: 'void', type: 'intrinsic' },
},
{
name: 'openDropdown',
parameters: [
{
name: 'expandToViewport',
typeName: 'boolean',
flags: { isOptional: false },
defaultValue: 'false',
},
],
returnType: { name: 'void', type: 'intrinsic' },
},
{
name: 'selectOption',
parameters: [
{
name: 'index',
typeName: 'number',
flags: { isOptional: false },
defaultValue: '1',
},
],
returnType: { name: 'void', type: 'intrinsic' },
},
]);
});
});
13 changes: 13 additions & 0 deletions test/test-utils/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { documentTestUtils } from '../../src/test-utils';
import { TestUtilsDoc } from '../../src/test-utils/interfaces';

export function buildTestUtilsProject(name: string, testGlob?: string): TestUtilsDoc[] {
return documentTestUtils(
{
tsconfig: require.resolve(`../../fixtures/test-utils/${name}/tsconfig.json`),
},
testGlob || `fixtures/test-utils/${name}/**/*`
);
}
2 changes: 1 addition & 1 deletion test/test-utils/usage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { describe, expect, test } from 'vitest';
import { buildTestUtilsProject } from '../components/test-helpers';
import { buildTestUtilsProject } from './test-helpers';

describe('documentTestUtils throws error for ', () => {
test('failing project generation because of invalid config', () => {
Expand Down
Loading