|
| 1 | +import { vol } from "memfs"; |
| 2 | +import { |
| 3 | + corePkg, |
| 4 | + fsRoot, |
| 5 | + nodecgPackageJson, |
| 6 | + nodecgPackageJsonStr, |
| 7 | + twitchChatPkg, |
| 8 | + validDevInstall, |
| 9 | + validProdInstall, |
| 10 | +} from "../testUtils"; |
| 11 | +import { SemVer } from "semver"; |
| 12 | +import * as path from "path"; |
| 13 | +import * as installation from "../../src/utils/installation"; |
| 14 | +import * as fsUtils from "../../src/utils/fs"; |
| 15 | +import * as npm from "../../src/utils/npm"; |
| 16 | +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" }; |
| 32 | +const nodecgPackageJsonPath = path.join(fsRoot, "package.json"); |
| 33 | +const packageJsonPath = path.join(defaultOpts.bundlePath, "package.json"); |
| 34 | +const tsConfigPath = path.join(defaultOpts.bundlePath, "tsconfig.json"); |
| 35 | + |
| 36 | +jest.spyOn(installation, "readInstallInfo").mockResolvedValue(validProdInstall); |
| 37 | +jest.spyOn(fsUtils, "executeCommand").mockResolvedValue(); |
| 38 | +jest.spyOn(npm, "getLatestPackageVersion").mockResolvedValue(new SemVer("1.2.3")); |
| 39 | + |
| 40 | +jest.mock("fs", () => vol); |
| 41 | +afterEach(() => vol.reset()); |
| 42 | +beforeEach(async () => { |
| 43 | + await vol.promises.mkdir(defaultOpts.bundleDir); |
| 44 | + await vol.promises.mkdir(defaultOpts.bundlePath); |
| 45 | + await vol.promises.writeFile(nodecgPackageJsonPath, nodecgPackageJsonStr); |
| 46 | +}); |
| 47 | + |
| 48 | +describe("ensureValidInstallation", () => { |
| 49 | + test("should not throw when passing install capable of generating bundles", () => { |
| 50 | + expect(ensureValidInstallation(validProdInstall)).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + test("should throw when passing undefined", () => { |
| 54 | + expect(() => ensureValidInstallation(undefined)).toThrow("not installed"); |
| 55 | + }); |
| 56 | + |
| 57 | + test("should throw when passing a development installation", () => { |
| 58 | + expect(() => ensureValidInstallation(validDevInstall)).toThrow("development installation"); |
| 59 | + }); |
| 60 | + |
| 61 | + test("should throw when passing install with no services", () => { |
| 62 | + expect(() => ensureValidInstallation({ ...validProdInstall, packages: [corePkg] })).toThrow( |
| 63 | + "at least one service", |
| 64 | + ); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("generateBundle", () => { |
| 69 | + test("should fail if bundle directory already contains files", async () => { |
| 70 | + // Create some file inside the directory in which the bundle would be generated. |
| 71 | + await vol.promises.writeFile(packageJsonPath, ""); |
| 72 | + await expect(generateBundle(fsRoot, defaultOpts, validProdInstall)).rejects.toThrow( |
| 73 | + "already exists and contains files", |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + test("should install dependencies", async () => { |
| 78 | + const installMock = jest.spyOn(npm, "runNpmInstall").mockResolvedValue(); |
| 79 | + await generateBundle(fsRoot, defaultOpts, validProdInstall); |
| 80 | + |
| 81 | + expect(installMock).toHaveBeenCalled(); |
| 82 | + expect(installMock).toHaveBeenCalledWith(defaultOpts.bundlePath, false); |
| 83 | + }); |
| 84 | + |
| 85 | + test("should run build if typescript", async () => { |
| 86 | + const buildMock = jest.spyOn(npm, "runNpmBuild").mockClear().mockResolvedValue(); |
| 87 | + await generateBundle(fsRoot, defaultOpts, validProdInstall); |
| 88 | + expect(buildMock).toHaveBeenCalledTimes(1); |
| 89 | + expect(buildMock).toHaveBeenCalledWith(defaultOpts.bundlePath); |
| 90 | + }); |
| 91 | + |
| 92 | + test("should not run build if javascript", async () => { |
| 93 | + const buildMock = jest.spyOn(npm, "runNpmBuild").mockClear().mockResolvedValue(); |
| 94 | + await generateBundle(fsRoot, jsOpts, validProdInstall); |
| 95 | + expect(buildMock).toHaveBeenCalledTimes(0); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 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 | + |
| 111 | +describe("genPackageJson", () => { |
| 112 | + // We don't have a good type for a package.json and this is only testing code so this should be fine. |
| 113 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 114 | + async function genPackageJSON(opts: GenerationOptions = defaultOpts): Promise<any> { |
| 115 | + await generateBundle(fsRoot, opts, validProdInstall); |
| 116 | + const packageJsonStr = vol.toJSON()[packageJsonPath]; |
| 117 | + if (!packageJsonStr) throw new Error("package.json does not exist"); |
| 118 | + return JSON.parse(packageJsonStr.toString()); |
| 119 | + } |
| 120 | + |
| 121 | + test("should have correct basic information", async () => { |
| 122 | + const packageJson = await genPackageJSON(); |
| 123 | + |
| 124 | + expect(packageJson["name"]).toBe(defaultOpts.bundleName); |
| 125 | + expect(packageJson["version"]).toBe(defaultOpts.version.toString()); |
| 126 | + expect(packageJson["nodecg"]["compatibleRange"]).toBeDefined(); |
| 127 | + expect(packageJson["nodecg"]["bundleDependencies"][twitchChatPkg.name]).toBe(`^${twitchChatPkg.version}`); |
| 128 | + }); |
| 129 | + |
| 130 | + test("should have only nodecg-io-core dependency if javascript", async () => { |
| 131 | + const deps = (await genPackageJSON(jsOpts))["dependencies"]; |
| 132 | + |
| 133 | + expect(Object.keys(deps).length).toBe(1); |
| 134 | + expect(Object.entries(deps)[0]).toStrictEqual([corePkg.name, `^${corePkg.version}`]); |
| 135 | + }); |
| 136 | + |
| 137 | + test("should have all required typing packages as dependency if typescript", async () => { |
| 138 | + const deps = (await genPackageJSON(defaultOpts))["dependencies"]; |
| 139 | + const e = Object.entries(deps); |
| 140 | + expect(e).toEqual(expect.arrayContaining([["nodecg", `^${nodecgPackageJson.version}`]])); |
| 141 | + expect(e).toEqual(expect.arrayContaining([[twitchChatPkg.name, `^${twitchChatPkg.version}`]])); |
| 142 | + |
| 143 | + // These dependencies should always have the latest version which is fetched by the mocked getLatestPackageVersion |
| 144 | + expect(e).toEqual(expect.arrayContaining([["typescript", `^1.2.3`]])); |
| 145 | + expect(e).toEqual(expect.arrayContaining([["@types/node", `^1.2.3`]])); |
| 146 | + }); |
| 147 | + |
| 148 | + test("should have build scripts if typescript", async () => { |
| 149 | + const packageJson = await genPackageJSON(defaultOpts); |
| 150 | + expect(packageJson["scripts"]).toBeDefined(); |
| 151 | + expect(Object.keys(packageJson["scripts"])).toStrictEqual(["build", "watch", "clean"]); |
| 152 | + }); |
| 153 | + |
| 154 | + test("should have no build scripts if javascript", async () => { |
| 155 | + const packageJson = await genPackageJSON(jsOpts); |
| 156 | + expect(packageJson["scripts"]).toBeUndefined(); |
| 157 | + }); |
| 158 | + |
| 159 | + test("should generate graphic if graphic is enabled", async () => { |
| 160 | + const packageJson = await genPackageJSON({ ...defaultOpts, graphic: true }); |
| 161 | + expect(packageJson["nodecg"]["graphics"]).toBeDefined(); |
| 162 | + expect(packageJson["nodecg"]["graphics"].length).toBe(1); |
| 163 | + }); |
| 164 | + |
| 165 | + test("should generate dashboard if dashboard is enabled", async () => { |
| 166 | + const packageJson = await genPackageJSON({ ...defaultOpts, dashboard: true }); |
| 167 | + expect(packageJson["nodecg"]["dashboardPanels"]).toBeDefined(); |
| 168 | + expect(packageJson["nodecg"]["dashboardPanels"].length).toBe(1); |
| 169 | + }); |
| 170 | +}); |
0 commit comments