From e52feef9119d6ed56744d9a5f6e8a4c20c201c9e Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Thu, 25 Nov 2021 21:19:12 -0800 Subject: [PATCH] chore: add test for CJS, MJS, and ESM support --- src/console-out.js | 6 +++- src/plop.js | 8 +++-- tests/esm.spec.js | 50 ++++++++++++++++++++++++++++++ tests/examples/cjs-js/package.json | 7 +++++ tests/examples/cjs-js/plopfile.js | 30 ++++++++++++++++++ tests/examples/cjs/package.json | 7 +++++ tests/examples/cjs/plopfile.cjs | 30 ++++++++++++++++++ tests/examples/esm/package.json | 7 +++++ tests/examples/esm/plopfile.js | 30 ++++++++++++++++++ tests/examples/mjs/package.json | 7 +++++ tests/examples/mjs/plopfile.mjs | 30 ++++++++++++++++++ wallaby.js | 6 ++-- 12 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 tests/esm.spec.js create mode 100644 tests/examples/cjs-js/package.json create mode 100644 tests/examples/cjs-js/plopfile.js create mode 100644 tests/examples/cjs/package.json create mode 100644 tests/examples/cjs/plopfile.cjs create mode 100644 tests/examples/esm/package.json create mode 100644 tests/examples/esm/plopfile.js create mode 100644 tests/examples/mjs/package.json create mode 100644 tests/examples/mjs/plopfile.mjs diff --git a/src/console-out.js b/src/console-out.js index 8d77f089..bda7b30e 100644 --- a/src/console-out.js +++ b/src/console-out.js @@ -90,7 +90,7 @@ function displayHelpScreen() { function createInitPlopfile(force = false) { var initString = - "module.exports = function (plop) {\n\n" + + "export default function (plop) {\n\n" + "\tplop.setGenerator('basics', {\n" + "\t\tdescription: 'this is a skeleton plopfile',\n" + "\t\tprompts: [],\n" + @@ -102,6 +102,10 @@ function createInitPlopfile(force = false) { throw Error('"plopfile.js" already exists at this location.'); } + if (fs.existsSync(process.cwd() + "/plopfile.cjs") && force === false) { + throw Error('"plopfile.cjs" already exists at this location.'); + } + fs.writeFileSync(process.cwd() + "/plopfile.js", initString); } diff --git a/src/plop.js b/src/plop.js index 3b1012e6..8deeec2f 100755 --- a/src/plop.js +++ b/src/plop.js @@ -17,7 +17,9 @@ import { getBypassAndGenerator, handleArgFlags } from "./input-processing.js"; const Plop = new Liftoff({ name: "plop", - extensions: interpret.jsVariants, + // Remove this when this PR is merged: + // https://github.com/gulpjs/interpret/pull/75 + extensions: { ...interpret.jsVariants, [".cjs"]: null }, v8flags: v8flags, }); @@ -103,11 +105,11 @@ async function run(env, _, passArgsBeforeDashes) { runGeneratorByName(generatorName); } else { // we just can't make sense of your input... sorry :-( - const fuzyGenName = (generatorName + " " + args.join(" ")).trim(); + const fuzzyGenName = (generatorName + " " + args.join(" ")).trim(); console.error( chalk.red("[PLOP] ") + 'Could not find a generator for "' + - fuzyGenName + + fuzzyGenName + '"' ); process.exit(1); diff --git a/tests/esm.spec.js b/tests/esm.spec.js new file mode 100644 index 00000000..9b642499 --- /dev/null +++ b/tests/esm.spec.js @@ -0,0 +1,50 @@ +import { resolve, dirname } from "node:path"; +import { renderPlop } from "./render.js"; + +import { fileURLToPath } from "node:url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +test("should load ESM file", async () => { + const { findByText, fireEvent } = await renderPlop([], { + cwd: resolve(__dirname, "./examples/esm"), + }); + expect(await findByText("What is your name?")).toBeTruthy(); + fireEvent.type("Joe"); + expect(await findByText("Joe")).toBeTruthy(); + fireEvent.enter(); + fireEvent.sigterm(); +}); + +test("should load MJS file", async () => { + const { findByText, fireEvent } = await renderPlop([], { + cwd: resolve(__dirname, "./examples/mjs"), + }); + expect(await findByText("What is your name?")).toBeTruthy(); + fireEvent.type("Joe"); + expect(await findByText("Joe")).toBeTruthy(); + fireEvent.enter(); + fireEvent.sigterm(); +}); + +test.only("should load CJS file", async () => { + const { findByText, fireEvent } = await renderPlop([], { + cwd: resolve(__dirname, "./examples/cjs"), + }); + expect(await findByText("What is your name?")).toBeTruthy(); + fireEvent.type("Joe"); + expect(await findByText("Joe")).toBeTruthy(); + fireEvent.enter(); + fireEvent.sigterm(); +}); + +test("should load JS module='commonjs' file", async () => { + const { findByText, fireEvent } = await renderPlop([], { + cwd: resolve(__dirname, "./examples/cjs-js"), + }); + expect(await findByText("What is your name?")).toBeTruthy(); + fireEvent.type("Joe"); + expect(await findByText("Joe")).toBeTruthy(); + fireEvent.enter(); + fireEvent.sigterm(); +}); diff --git a/tests/examples/cjs-js/package.json b/tests/examples/cjs-js/package.json new file mode 100644 index 00000000..72e11e03 --- /dev/null +++ b/tests/examples/cjs-js/package.json @@ -0,0 +1,7 @@ +{ + "name": "plop-example-prompts-only", + "type": "commonjs", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + } +} diff --git a/tests/examples/cjs-js/plopfile.js b/tests/examples/cjs-js/plopfile.js new file mode 100644 index 00000000..cfce952e --- /dev/null +++ b/tests/examples/cjs-js/plopfile.js @@ -0,0 +1,30 @@ +module.exports = function (plop) { + plop.setGenerator("test", { + description: "this is a test", + prompts: [ + { + type: "input", + name: "name", + message: "What is your name?", + validate: function (value) { + if (/.+/.test(value)) { + return true; + } + return "name is required"; + }, + }, + { + type: "checkbox", + name: "toppings", + message: "What pizza toppings do you like?", + choices: [ + { name: "Cheese", value: "cheese", checked: true }, + { name: "Pepperoni", value: "pepperoni" }, + { name: "Pineapple", value: "pineapple" }, + { name: "Mushroom", value: "mushroom" }, + { name: "Bacon", value: "bacon", checked: true }, + ], + }, + ], + }); +}; diff --git a/tests/examples/cjs/package.json b/tests/examples/cjs/package.json new file mode 100644 index 00000000..d1a07383 --- /dev/null +++ b/tests/examples/cjs/package.json @@ -0,0 +1,7 @@ +{ + "name": "plop-example-prompts-only", + "type": "module", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + } +} diff --git a/tests/examples/cjs/plopfile.cjs b/tests/examples/cjs/plopfile.cjs new file mode 100644 index 00000000..8c307faa --- /dev/null +++ b/tests/examples/cjs/plopfile.cjs @@ -0,0 +1,30 @@ +module.exports = function (plop) { + plop.setGenerator("test", { + description: "this is a test", + prompts: [ + { + type: "input", + name: "name", + message: "What is your name?", + validate: function (value) { + if (/.+/.test(value)) { + return true; + } + return "name is required"; + }, + }, + { + type: "checkbox", + name: "toppings", + message: "What pizza toppings do you like?", + choices: [ + { name: "Cheese", value: "cheese", checked: true }, + { name: "Pepperoni", value: "pepperoni" }, + { name: "Pineapple", value: "pineapple" }, + { name: "Mushroom", value: "mushroom" }, + { name: "Bacon", value: "bacon", checked: true }, + ], + }, + ], + }); +} diff --git a/tests/examples/esm/package.json b/tests/examples/esm/package.json new file mode 100644 index 00000000..d1a07383 --- /dev/null +++ b/tests/examples/esm/package.json @@ -0,0 +1,7 @@ +{ + "name": "plop-example-prompts-only", + "type": "module", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + } +} diff --git a/tests/examples/esm/plopfile.js b/tests/examples/esm/plopfile.js new file mode 100644 index 00000000..bac43fe9 --- /dev/null +++ b/tests/examples/esm/plopfile.js @@ -0,0 +1,30 @@ +export default function (plop) { + plop.setGenerator("test", { + description: "this is a test", + prompts: [ + { + type: "input", + name: "name", + message: "What is your name?", + validate: function (value) { + if (/.+/.test(value)) { + return true; + } + return "name is required"; + }, + }, + { + type: "checkbox", + name: "toppings", + message: "What pizza toppings do you like?", + choices: [ + { name: "Cheese", value: "cheese", checked: true }, + { name: "Pepperoni", value: "pepperoni" }, + { name: "Pineapple", value: "pineapple" }, + { name: "Mushroom", value: "mushroom" }, + { name: "Bacon", value: "bacon", checked: true }, + ], + }, + ], + }); +} diff --git a/tests/examples/mjs/package.json b/tests/examples/mjs/package.json new file mode 100644 index 00000000..d1a07383 --- /dev/null +++ b/tests/examples/mjs/package.json @@ -0,0 +1,7 @@ +{ + "name": "plop-example-prompts-only", + "type": "module", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + } +} diff --git a/tests/examples/mjs/plopfile.mjs b/tests/examples/mjs/plopfile.mjs new file mode 100644 index 00000000..bac43fe9 --- /dev/null +++ b/tests/examples/mjs/plopfile.mjs @@ -0,0 +1,30 @@ +export default function (plop) { + plop.setGenerator("test", { + description: "this is a test", + prompts: [ + { + type: "input", + name: "name", + message: "What is your name?", + validate: function (value) { + if (/.+/.test(value)) { + return true; + } + return "name is required"; + }, + }, + { + type: "checkbox", + name: "toppings", + message: "What pizza toppings do you like?", + choices: [ + { name: "Cheese", value: "cheese", checked: true }, + { name: "Pepperoni", value: "pepperoni" }, + { name: "Pineapple", value: "pineapple" }, + { name: "Mushroom", value: "mushroom" }, + { name: "Bacon", value: "bacon", checked: true }, + ], + }, + ], + }); +} diff --git a/wallaby.js b/wallaby.js index 0fa7b34a..b8cc93d4 100644 --- a/wallaby.js +++ b/wallaby.js @@ -1,12 +1,12 @@ module.exports = function (wallaby) { return { - files: ["bin/**/*.js", "src/**/*.js", "!tests/**/*.ava.js"], - tests: ["tests/**/*.ava.js"], + files: ["bin/**/*.js", "src/**/*.js", "!tests/**/*.spec.js"], + tests: ["tests/**/*.spec.js"], env: { type: "node", runner: "node", }, - testFramework: "ava", + testFramework: "jest", debug: true, }; };