Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit b23b563

Browse files
committed
Add tests for genTsConfig and genGraphic/Dashboard and genExtension
1 parent b2ff747 commit b23b563

20 files changed

+121
-42
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module.exports = {
33
testEnvironment: "node",
44
// We don't want to test nodecg, and without including it jest fails because it includes a invalid json
55
modulePathIgnorePatterns: ["/nodecg/"],
6-
testMatch: ["<rootDir>/test/**/**.ts", "!**/testUtils.ts"],
6+
testMatch: ["<rootDir>/test/**/*.ts", "!**/*.util.ts"],
77
};

src/generate/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ export async function generateBundle(
103103
}
104104

105105
async function genGitIgnore(opts: GenerationOptions): Promise<void> {
106+
// When typescript we want to ignore compilation results.
106107
const languageIgnoredFiles = opts.language === "typescript" ? ["/extension/*.js", "/extension/*.js.map"] : [];
108+
// Usual editors and node_modules directory
107109
const ignoreEntries = ["/node_modules/", "/.vscode/", "/.idea/", ...languageIgnoredFiles];
108110
const content = ignoreEntries.join("\n");
109111
await writeBundleFile(content, opts.bundlePath, ".gitignore");

test/generate/extension.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { vol } from "memfs";
2+
import * as path from "path";
3+
import { defaultOpts, jsOpts } from "./opts.util";
4+
import { genExtension } from "../../src/generate/extension";
5+
import { validProdInstall } from "../test.util";
6+
7+
jest.mock("fs", () => vol);
8+
beforeEach(() => vol.promises.mkdir(defaultOpts.bundlePath, { recursive: true }));
9+
afterEach(() => vol.reset());
10+
11+
describe("genExtension", () => {
12+
const extensionDirPath = path.join(defaultOpts.bundlePath, "extension");
13+
function checkExtFile(name: string, existing: boolean) {
14+
expect(vol.existsSync(path.join(extensionDirPath, name))).toBe(existing);
15+
}
16+
17+
test("should generate a .ts file if typescript", async () => {
18+
await genExtension(defaultOpts, validProdInstall);
19+
checkExtFile("index.ts", true);
20+
checkExtFile("index.js", false);
21+
});
22+
23+
test("should generate a .js file if javascript", async () => {
24+
await genExtension(jsOpts, validProdInstall);
25+
checkExtFile("index.ts", false);
26+
checkExtFile("index.js", true);
27+
});
28+
});

test/generate/index.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,18 @@ import {
77
twitchChatPkg,
88
validDevInstall,
99
validProdInstall,
10-
} from "../testUtils";
10+
} from "../test.util";
1111
import { SemVer } from "semver";
1212
import * as path from "path";
1313
import * as installation from "../../src/utils/installation";
1414
import * as fsUtils from "../../src/utils/fs";
1515
import * as npm from "../../src/utils/npm";
1616
import { ensureValidInstallation, generateBundle } from "../../src/generate";
17-
import { computeGenOptsFields, GenerationOptions, PromptedGenerationOptions } from "../../src/generate/prompt";
18-
19-
const defaultOptsPrompt: PromptedGenerationOptions = {
20-
bundleName: "test-bundle",
21-
bundleDir: path.join(fsRoot, "bundles"),
22-
description: "Hello, this is a description for a test bundle.",
23-
version: new SemVer("0.1.0"),
24-
services: [twitchChatPkg.path.replace("nodecg-io-", "")],
25-
language: "typescript",
26-
graphic: false,
27-
dashboard: false,
28-
};
29-
30-
const defaultOpts = computeGenOptsFields(defaultOptsPrompt, validProdInstall);
31-
const jsOpts: GenerationOptions = { ...defaultOpts, language: "javascript" };
17+
import { GenerationOptions } from "../../src/generate/prompt";
18+
import { defaultOpts, jsOpts } from "./opts.util";
19+
3220
const nodecgPackageJsonPath = path.join(fsRoot, "package.json");
3321
const packageJsonPath = path.join(defaultOpts.bundlePath, "package.json");
34-
const tsConfigPath = path.join(defaultOpts.bundlePath, "tsconfig.json");
3522

3623
jest.spyOn(installation, "readInstallInfo").mockResolvedValue(validProdInstall);
3724
jest.spyOn(fsUtils, "executeCommand").mockResolvedValue();
@@ -96,18 +83,6 @@ describe("generateBundle", () => {
9683
});
9784
});
9885

99-
describe("genTSConfig", () => {
100-
test("should generate tsconfig if typescript", async () => {
101-
await generateBundle(fsRoot, defaultOpts, validProdInstall);
102-
expect(vol.existsSync(tsConfigPath)).toBe(true);
103-
});
104-
105-
test("should not generate tsconfig if javascript", async () => {
106-
await generateBundle(fsRoot, jsOpts, validProdInstall);
107-
expect(vol.existsSync(tsConfigPath)).toBe(false);
108-
});
109-
});
110-
11186
describe("genPackageJson", () => {
11287
// We don't have a good type for a package.json and this is only testing code so this should be fine.
11388
// eslint-disable-next-line @typescript-eslint/no-explicit-any

test/generate/opts.util.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { computeGenOptsFields, GenerationOptions, PromptedGenerationOptions } from "../../src/generate/prompt";
2+
import * as path from "path";
3+
import { fsRoot, twitchChatPkg, validProdInstall } from "../test.util";
4+
import { SemVer } from "semver";
5+
6+
export const defaultOptsPrompt: PromptedGenerationOptions = {
7+
bundleName: "test-bundle",
8+
bundleDir: path.join(fsRoot, "bundles"),
9+
description: "Hello, this is a description for a test bundle.",
10+
version: new SemVer("0.1.0"),
11+
services: [twitchChatPkg.path.replace("nodecg-io-", "")],
12+
language: "typescript",
13+
graphic: false,
14+
dashboard: false,
15+
};
16+
17+
export const defaultOpts = computeGenOptsFields(defaultOptsPrompt, validProdInstall);
18+
export const jsOpts: GenerationOptions = { ...defaultOpts, language: "javascript" };

test/generate/panel.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { vol } from "memfs";
2+
import * as path from "path";
3+
import { defaultOpts } from "./opts.util";
4+
import { genDashboard, genGraphic } from "../../src/generate/panel";
5+
6+
jest.mock("fs", () => vol);
7+
beforeEach(() => vol.promises.mkdir(defaultOpts.bundlePath, { recursive: true }));
8+
afterEach(() => vol.reset());
9+
10+
describe("genGraphic", () => {
11+
const graphicPath = path.join(defaultOpts.bundlePath, "graphics", "index.html");
12+
test("should generate a graphic html file if wanted", async () => {
13+
await genGraphic({ ...defaultOpts, graphic: true });
14+
expect(vol.existsSync(graphicPath)).toBe(true);
15+
});
16+
17+
test("should generate no file if not wanted", async () => {
18+
await genGraphic(defaultOpts);
19+
expect(vol.existsSync(graphicPath)).toBe(false);
20+
});
21+
});
22+
23+
describe("genDashboard", () => {
24+
const dashboardPath = path.join(defaultOpts.bundlePath, "dashboard", "panel.html");
25+
test("should generate a dashboard html file if wanted", async () => {
26+
await genDashboard({ ...defaultOpts, dashboard: true });
27+
expect(vol.existsSync(dashboardPath)).toBe(true);
28+
});
29+
30+
test("should generate no file if not wanted", async () => {
31+
await genDashboard(defaultOpts);
32+
expect(vol.existsSync(dashboardPath)).toBe(false);
33+
});
34+
});

test/generate/tsConfig.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { vol } from "memfs";
2+
import { genTsConfig } from "../../src/generate/tsConfig";
3+
import { defaultOpts, jsOpts } from "./opts.util";
4+
import * as path from "path";
5+
6+
jest.mock("fs", () => vol);
7+
beforeEach(() => vol.promises.mkdir(defaultOpts.bundlePath, { recursive: true }));
8+
afterEach(() => vol.reset());
9+
10+
const tsConfigPath = path.join(defaultOpts.bundlePath, "tsconfig.json");
11+
12+
describe("genTsConfig", () => {
13+
test("should generate tsconfig if typescript", async () => {
14+
await genTsConfig(defaultOpts);
15+
expect(vol.existsSync(tsConfigPath)).toBe(true);
16+
});
17+
18+
test("should not generate tsconfig if javascript", async () => {
19+
await genTsConfig(jsOpts);
20+
expect(vol.existsSync(tsConfigPath)).toBe(false);
21+
});
22+
});

test/install/development.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vol } from "memfs";
22
import * as git from "isomorphic-git";
33
import * as fsUtils from "../../src/utils/fs";
4-
import { fsRoot, validDevInstall, nodecgIODir } from "../testUtils";
4+
import { fsRoot, validDevInstall, nodecgIODir } from "../test.util";
55
import * as dev from "../../src/install/development";
66
import { removeDirectory } from "../../src/utils/fs";
77

test/install/production.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { vol } from "memfs";
22
import * as path from "path";
3-
import { corePkg, dashboardPkg, nodecgIODir, twitchChatPkg, validProdInstall } from "../testUtils";
3+
import { corePkg, dashboardPkg, nodecgIODir, twitchChatPkg, validProdInstall } from "../test.util";
44
import { diffPackages, installPackages, removePackages, validateInstall } from "../../src/install/production";
55
import * as installation from "../../src/utils/installation";
66
import * as fsUtils from "../../src/utils/fs";

test/install/prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getCompatibleVersions, buildPackageList, getServicesFromInstall } from
44
import { logger } from "../../src/utils/log";
55
import { corePackages, dashboardPackage, dashboardPath } from "../../src/nodecgIOVersions";
66
import { ProductionInstallation } from "../../src/utils/installation";
7-
import { corePkg, twitchChatPkg } from "../testUtils";
7+
import { corePkg, twitchChatPkg } from "../test.util";
88
import { SemVer } from "semver";
99

1010
describe("getCompatibleVersions", () => {

0 commit comments

Comments
 (0)