Skip to content

Commit

Permalink
chore: add test for CJS, MJS, and ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 26, 2021
1 parent 020286c commit e52feef
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/console-out.js
Expand Up @@ -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" +
Expand All @@ -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);
}

Expand Down
8 changes: 5 additions & 3 deletions src/plop.js
Expand Up @@ -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,
});

Expand Down Expand Up @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions 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();
});
7 changes: 7 additions & 0 deletions 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"
}
}
30 changes: 30 additions & 0 deletions 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 },
],
},
],
});
};
7 changes: 7 additions & 0 deletions 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"
}
}
30 changes: 30 additions & 0 deletions 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 },
],
},
],
});
}
7 changes: 7 additions & 0 deletions 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"
}
}
30 changes: 30 additions & 0 deletions 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 },
],
},
],
});
}
7 changes: 7 additions & 0 deletions 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"
}
}
30 changes: 30 additions & 0 deletions 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 },
],
},
],
});
}
6 changes: 3 additions & 3 deletions 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,
};
};

0 comments on commit e52feef

Please sign in to comment.