UNPKG

schema2typebox

Version:

Creates typebox code from JSON schemas

117 lines 5.61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const globals_1 = require("@jest/globals"); const fs = __importStar(require("node:fs")); const cli = __importStar(require("../src/cli")); jest.mock("node:fs", () => { const actualModule = jest.requireActual("node:fs"); return { ...actualModule, writeFileSync: jest.fn(), readFileSync: jest.fn(), }; }); jest.mock("../src/programmatic-usage", () => { const actualModule = jest.requireActual("../src/programmatic-usage"); return { ...actualModule, schema2typebox: async () => { return "DUMMY_RESULT"; }, }; }); (0, globals_1.describe)("when running the cli", () => { (0, globals_1.it)("returns help text with 'h' argument", async () => { process.argv = ["node", "dist/src/cli/index.js", "-h"]; process.stdout.write = jest.fn(); await cli.runCli(); expect(process.stdout.write).toHaveBeenCalledTimes(1); expect(process.stdout.write).toHaveBeenCalledWith(cli.HELP_TEXT); }); (0, globals_1.it)("returns help text with 'help' argument", async () => { process.argv = ["node", "dist/src/cli/index.js", "--help"]; process.stdout.write = jest.fn(); await cli.runCli(); expect(process.stdout.write).toHaveBeenCalledTimes(1); expect(process.stdout.write).toHaveBeenCalledWith(cli.HELP_TEXT); }); (0, globals_1.it)("returns output to stdout with 'output-stdout' argument", async () => { process.argv = ["node", "dist/src/cli/index.js", "--output-stdout"]; process.stdout.write = jest.fn(); await cli.runCli(); expect(process.stdout.write).toHaveBeenCalledTimes(1); }); (0, globals_1.it)("'--output-stdout' has precedence over '--output'", async () => { process.argv = ["node", "dist/src/cli/index.js", "--output-stdout"]; process.stdout.write = jest.fn(); await cli.runCli(); expect(process.stdout.write).toHaveBeenCalledTimes(1); expect(fs.writeFileSync).not.toHaveBeenCalled(); }); (0, globals_1.it)("writes to file without 'output-stdout' argument", async () => { process.argv = ["node", "dist/src/cli/index.js"]; process.stdout.write = jest.fn(); await cli.runCli(); expect(fs.writeFileSync).toHaveBeenCalledTimes(1); expect(process.stdout.write).not.toHaveBeenCalled(); }); (0, globals_1.it)(`writes to file '${cli.DEFAULT_OUTPUT_FILE_NAME}' without an 'output' argument`, async () => { process.argv = ["node", "dist/src/cli/index.js"]; process.stdout.write = jest.fn(); await cli.runCli(); expect(fs.writeFileSync).toHaveBeenCalledTimes(1); expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining(cli.DEFAULT_OUTPUT_FILE_NAME), expect.any(String), expect.any(String)); expect(process.stdout.write).not.toHaveBeenCalled(); }); (0, globals_1.it)("writes to file based on 'output' argument", async () => { const output = "newfile.ts"; process.argv = ["node", "dist/src/cli/index.js", "--output", output]; process.stdout.write = jest.fn(); await cli.runCli(); expect(fs.writeFileSync).toHaveBeenCalledTimes(1); expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining(output), expect.any(String), expect.any(String)); expect(process.stdout.write).not.toHaveBeenCalled(); }); (0, globals_1.it)(`reads from file '${cli.DEFAULT_INPUT_FILE_NAME}' without an 'input' argument`, async () => { const input = "dummyschema.json"; process.argv = ["node", "dist/src/cli/index.js", "--input", input]; process.stdout.write = jest.fn(); await cli.runCli(); expect(fs.readFileSync).toHaveBeenCalledTimes(1); expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining(input), expect.any(String)); expect(process.stdout.write).not.toHaveBeenCalled(); }); (0, globals_1.it)(`reads from file based on 'input' argument`, async () => { const input = "dummyschema.json"; process.argv = ["node", "dist/src/cli/index.js", "--input", input]; process.stdout.write = jest.fn(); await cli.runCli(); expect(fs.readFileSync).toHaveBeenCalledTimes(1); expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining(input), expect.any(String)); expect(process.stdout.write).not.toHaveBeenCalled(); }); }); //# sourceMappingURL=cli.spec.js.map