Skip to content
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

add Typedoc integration tests #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 3 additions & 0 deletions assets/sample/excluded/excluded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function excludedHello(name: string): void {
console.log(`hello ${ name }`);
}
3 changes: 3 additions & 0 deletions assets/sample/excluded/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Type definitions for excluded

declare function excludedHello(name: string): void;
3 changes: 3 additions & 0 deletions assets/sample/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Type definitions for index

declare function hello(name: string): void;
6 changes: 4 additions & 2 deletions assets/sample/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default function (name: string): void {
console.log(`hello ${ name }`);
import excludedHello from './excluded/excluded';

export default function hello(name: string): void {
excludedHello(name);
}
11 changes: 9 additions & 2 deletions tests/_support/tmpFiles.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { mkdirSync, mkdtempSync, existsSync } from 'fs';
import { join } from 'path';

export function tmpDirectory(): string {
export function tmpDirectory(ext?: string): string {
if (!existsSync('.test')) {
mkdirSync('.test');
}
return mkdtempSync('.test/dir-');

const tmpDir = mkdtempSync('.test/dir-');

if (ext) {
return join(tmpDir, `index${ext}`);
Copy link
Member

Choose a reason for hiding this comment

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

I understand why this was done because sometimes we want a filename. But it doesn't really follow that by passing an extension that it should make a temp directory and join it with a filename index${ext}. I would just move the join outside of this function to explicitly call out what's going on.

}

return tmpDir;
}

export function tmpFile(name: string = String(Math.floor(Math.random() * 10000))): string {
Expand Down
127 changes: 123 additions & 4 deletions tests/integration/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,136 @@ import * as registerSuite from 'intern!object';
import * as assert from 'intern/chai!assert';
import typedoc from 'src/commands/typedoc';
import { tmpDirectory } from '../_support/tmpFiles';
import { readFileSync } from 'fs';
import { readdirSync, readFileSync } from 'fs';
import { join } from 'path';

let sampleRequire: any;
let outputTarget: any;

function getTreeContents(obj: any, path: string) {
const itemPath = join(obj.currentPath, path);
console.log(arguments[1], arguments[3]);
if (path.split('.').length > 1) {
console.log(path, ' is a file');
obj[path] = String(readFileSync(itemPath));
} else if (['assets'].some((p) => p === path)) {
console.log(path, ' should not be dealt with');
} else {
console.log(path, ' is a dir');
obj[path] = readdirSync(itemPath).reduce(getTreeContents, {
currentPath: join(obj.currentPath, path)
});
}

return obj;
}

registerSuite({
name: 'typedoc',

before() {
sampleRequire = (<any> require).toUrl('assets/sample');
},

beforeEach() {
outputTarget = tmpDirectory();
},

async build() {
const out = tmpDirectory();
await typedoc(sampleRequire, outputTarget);

const indexFile = readFileSync(join(outputTarget, 'index.html'));

await typedoc((<any> require).toUrl('assets/sample'), out);
const indexFile = readFileSync(join(out, 'index.html'));
assert.include(String(indexFile), 'This is a README!');
},

async 'build JSON'() {
let json;

outputTarget = tmpDirectory('.json');

await typedoc(sampleRequire, outputTarget);

const indexFile = readFileSync(outputTarget);

try {
json = JSON.parse(String(indexFile));
assert.isOk(json.children);
} catch (e) {
console.log(e.message);
assert.fail('typedoc output should be a JSON object');
}
},

async 'test externalPattern option'() {
const opts = {
externalPattern: '**/examples/**/*.ts'
};

outputTarget = tmpDirectory('.json');

await typedoc(sampleRequire, outputTarget, opts);

const output = JSON.parse(String(readFileSync(outputTarget)));

// we should have two children, index and excluded/excluded
assert.strictEqual(output.children.length, 2);

const outputChildNames = output.children.map((child: any) => child.name);

assert.include(outputChildNames, '"index"');
assert.include(outputChildNames, '"excluded/excluded"');

const excludedChild = output.children.filter((child: any) => child.flags.isExternal);

assert.strictEqual(excludedChild[0].name, '"excluded/excluded"');
},

async 'test excludeExternals option'() {
const opts = {
excludeExternals: true,
externalPattern: '**/examples/**/*.ts'
};

outputTarget = tmpDirectory('.json');

await typedoc(sampleRequire, outputTarget, opts);

const output = JSON.parse(String(readFileSync(outputTarget)));

// index.ts should be the only child
assert.strictEqual(output.children.length, 1);
assert.strictEqual(output.children[0].name, '"index"');
assert.isUndefined(output.children[0].flags.isExternal);
},

async excludeOption() {
const opts = {
exclude: '**/index.ts'
};

outputTarget = tmpDirectory('.json');

await typedoc(sampleRequire, outputTarget, opts);

const output = JSON.parse(String(readFileSync(outputTarget)));

// we should have no children
assert.isUndefined(output.children);
},

async 'test includeDeclarations option'() {
const opts = {
includeDeclarations: true
};

outputTarget = tmpDirectory('.json');

await typedoc(sampleRequire, outputTarget, opts);

const output = JSON.parse(String(readFileSync(outputTarget)));

// if we includeDeclarations, it should pull in .d.ts files from the whole project
assert.isTrue(output.children.length > 2);
}
});
2 changes: 1 addition & 1 deletion tests/intern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const loaderOptions = {

export const suites = [ 'tests/unit/all' ];

export const excludeInstrumentation = /^(?:_build\/tests|node_modules)\//;
export const excludeInstrumentation = true; // /^(?:_build\/tests|node_modules)\//;
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why we excluded instrumentation here


export const loaders = {
'host-node': '@dojo/loader'
Expand Down