From 858b2313cd312d33f8e005d605fe33da426333ae Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sun, 13 Jun 2021 10:49:42 -0600 Subject: [PATCH] chore: Refactor tests to reduce runtime --- src/lib/utils/package-manifest.ts | 6 +-- src/test/packages.test.ts | 64 ++++++++++++++++++------------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/lib/utils/package-manifest.ts b/src/lib/utils/package-manifest.ts index c838e9dfe..25da433b2 100644 --- a/src/lib/utils/package-manifest.ts +++ b/src/lib/utils/package-manifest.ts @@ -1,4 +1,4 @@ -// Utilities to support the inspection of node package "manifests" (package.json's) +// Utilities to support the inspection of node package "manifests" import glob = require("glob"); import { dirname, join, resolve } from "path"; @@ -81,7 +81,7 @@ export function expandPackages( packageJsonDir: string, workspaces: string[] ): string[] { - // Technnically npm and Yarn workspaces don't support recursive nesting, + // Technically npm and Yarn workspaces don't support recursive nesting, // however we support the passing of paths to either packages or // to the root of a workspace tree in our params and so we could here // be dealing with either a root or a leaf. So let's do this recursively, @@ -99,7 +99,7 @@ export function expandPackages( // Assume this is a single package repo return [dirname(packageJsonPath)]; } - // This is a workpace root package, recurse + // This is a workspace root package, recurse return expandPackages( logger, dirname(packageJsonPath), diff --git a/src/test/packages.test.ts b/src/test/packages.test.ts index cdede97a2..b1fc784ad 100644 --- a/src/test/packages.test.ts +++ b/src/test/packages.test.ts @@ -1,38 +1,50 @@ import { deepStrictEqual as equal, ok } from "assert"; -import * as Path from "path"; -import { Application } from "../lib/application"; +import { readFileSync } from "fs"; +import { join } from "path"; +import { Logger, normalizePath } from "../lib/utils"; +import { + expandPackages, + getTsEntryPointForPackage, +} from "../lib/utils/package-manifest"; -// TODO: These tests don't really need to go through the whole process. They could -// instead just test the package expansion feature. Making this change should cut down on test time by ~50%. describe("Packages support", () => { it("handles monorepos", () => { - const base = Path.join(__dirname, "packages", "multi-package"); - const app = new Application(); - app.bootstrap({ - packages: [base], - }); - const project = app.convert(); + const base = join(__dirname, "packages", "multi-package"); + const logger = new Logger(); + const packages = expandPackages(logger, ".", [base]); + equal( - project?.children?.map((r) => r.name), + packages, [ - "typedoc-multi-package-bar", - "typedoc-multi-package-baz", - "typedoc-multi-package-foo", - ] + join(base, "packages/bar"), + join(base, "packages/baz"), + join(base, "packages/foo"), + ].map(normalizePath) ); + + const entries = packages.map((p) => { + const packageJson = join(p, "package.json"); + return getTsEntryPointForPackage( + logger, + packageJson, + JSON.parse(readFileSync(packageJson, "utf-8")) + ); + }); + + equal(entries, [ + join(base, "packages/bar/index.d.ts"), + join(base, "packages/baz/index.ts"), + join(base, "packages/foo/index.ts"), + ]); + + ok(!logger.hasErrors() && !logger.hasWarnings()); }); it("handles single packages", () => { - const base = Path.join(__dirname, "packages", "single-package"); - const app = new Application(); - app.bootstrap({ - packages: [base], - }); - const project = app.convert(); - ok(project, "Failed to convert project"); - equal( - project.children?.map((r) => r.name), - ["helloWorld"] - ); + const base = join(__dirname, "packages", "single-package"); + const logger = new Logger(); + const packages = expandPackages(logger, ".", [base]); + + equal(packages, [normalizePath(base)]); }); });