UNPKG

@practica/create-node-app

Version:

Create Node.js app that is packed with best practices AND strive for simplicity

161 lines (160 loc) 7.86 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const execa_1 = __importDefault(require("execa")); const path_1 = __importDefault(require("path")); const testHelpers = __importStar(require("./test-helpers")); let emptyFolderForATest; beforeEach(async () => { emptyFolderForATest = await testHelpers.createUniqueFolder(); }); afterEach(async () => { await fs_extra_1.default.remove(emptyFolderForATest); }); describe("Non-interactive CLI component tests", () => { describe("Web framework flag", () => { test("When framework type is express, then the created entry points folder has only express folder and dependencies", async () => { // Arrange // Act await (0, execa_1.default)("ts-node", [ "./bin/cli.ts", "immediate", `--target-directory=${emptyFolderForATest}`, `--app-name=test`, `--web-framework=express`, ]); // Assert const rootPath = path_1.default.join(emptyFolderForATest, "test", "services", "order-service"); const isFastifyInPackageJSON = await testHelpers.doesFileContainPhrase(path_1.default.join(rootPath, "package.json"), "fastify"); const doesEntryPointFolderExists = await testHelpers.doesFolderExistInPath(path_1.default.join(rootPath, "entry-points")); expect({ isFastifyInPackageJSON, doesEntryPointFolderExists, }).toStrictEqual({ isFastifyInPackageJSON: false, doesEntryPointFolderExists: true, }); }); test("When framework type is fastify, then the created entry points folder has only fastify folder and dependencies", async () => { // Arrange // Act await (0, execa_1.default)("ts-node", [ "./bin/cli.ts", "immediate", `--target-directory=${emptyFolderForATest}`, `--app-name=test`, `--web-framework=fastify`, ]); // Assert const rootPath = path_1.default.join(emptyFolderForATest, "test", "services", "order-service"); const isExpressInPackageJSON = await testHelpers.doesFileContainPhrase(path_1.default.join(rootPath, "package.json"), "express"); const doesEntryPointFolderExists = await testHelpers.doesFolderExistInPath(path_1.default.join(rootPath, "entry-points")); expect({ isFastifyInPackageJSON: isExpressInPackageJSON, doesEntryPointFolderExists, }).toStrictEqual({ isFastifyInPackageJSON: false, doesEntryPointFolderExists: true, }); }); }); describe("ORM type", () => { test("When ORM type is Prisma, then the created DAL folder has prisma dependency and files", async () => { // Arrange // Act await (0, execa_1.default)("ts-node", [ "./bin/cli.ts", "immediate", `--target-directory=${emptyFolderForATest}`, `--app-name=test`, "--orm=prisma", ]); // Assert const rootPath = path_1.default.join(emptyFolderForATest, "test", "services", "order-service"); const isPrismaInPackageJSON = await testHelpers.doesFileContainPhrase(path_1.default.join(rootPath, "package.json"), "prisma"); const isSequelizeInPackageJSON = await testHelpers.doesFileContainPhrase(path_1.default.join(rootPath, "package.json"), "sequelize"); const isPrismaFolderInDALLayer = await testHelpers.doesFolderExistInPath(path_1.default.join(rootPath, "data-access", "prisma")); expect({ isSequelizeInPackageJSON, isPrismaInPackageJSON, isPrismaFolderInDALLayer, }).toStrictEqual({ isSequelizeInPackageJSON: false, isPrismaFolderInDALLayer: true, isPrismaInPackageJSON: true, }); }); test("When ORM type is sequelize, then the created DAL folder has only sequelize dependency and files", async () => { // Arrange // Act await (0, execa_1.default)("ts-node", [ "./bin/cli.ts", "immediate", `--target-directory=${emptyFolderForATest}`, `--app-name=test`, "--orm=sequelize", ]); // Assert const rootPath = path_1.default.join(emptyFolderForATest, "test", "services", "order-service"); const isPrismaInPackageJSON = await testHelpers.doesFileContainPhrase(path_1.default.join(rootPath, "package.json"), "prisma"); const isSequelizeInPackageJSON = await testHelpers.doesFileContainPhrase(path_1.default.join(rootPath, "package.json"), "sequelize"); const isSequelizeFolderInDALLayer = await testHelpers.doesFolderExistInPath(path_1.default.join(rootPath, "data-access", "config")); const isPrismaFolderThere = await testHelpers.doesFolderExistInPath(path_1.default.join(rootPath, "data-access-prisma")); expect({ isSequelizeInPackageJSON, isPrismaInPackageJSON, isSequelizeFolderInDALLayer, isPrismaFolderThere, }).toStrictEqual({ isSequelizeInPackageJSON: true, isSequelizeFolderInDALLayer: true, isPrismaInPackageJSON: false, isPrismaFolderThere: false, }); }); }); describe("Flag app name", () => { test("When installing without app name, then it's created with the default name", async () => { // Arrange // Act await (0, execa_1.default)("ts-node", [ "./bin/cli.ts", "immediate", `--target-directory=${emptyFolderForATest}`, // 👉 No name provided ]); // Assert const generatedSolutionFolder = path_1.default.join(emptyFolderForATest, "default-app-name"); const pathExists = await fs_extra_1.default.pathExists(generatedSolutionFolder); expect({ pathExists }).toStrictEqual({ pathExists: true }); const destinationFolderContent = await fs_extra_1.default.readdir(generatedSolutionFolder); expect(destinationFolderContent.length).toBeGreaterThan(0); }); }); });