Skip to content

Commit

Permalink
Fix various timing and impl bugs with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 23, 2021
1 parent 80aa8ec commit cb0bd7e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"prepare": "husky install"
},
"devDependencies": {
"cli-testing-library": "1.0.0-alpha.6",
"cli-testing-library": "1.0.0-alpha.7",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
Expand Down
8 changes: 7 additions & 1 deletion src/plop.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ const Plop = new Liftoff({
v8flags: v8flags,
});

const progressSpinner = ora();
const isInJest = process.env.NODE_ENV === "test";

const progressSpinner = ora({
// Default is stderr
stream: isInJest ? process.stdout : process.stderr,
isEnabled: !isInJest,
});

/**
* The function to pass as the second argument to `Plop.launch`
Expand Down
3 changes: 2 additions & 1 deletion tests/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { renderPlop } = require("./render");
const { resolve } = require("path");
const { waitFor } = require("cli-testing-library");
const fs = require("fs");
const { getFilePath } = require("./file-helper");
const { getFileHelper } = require("./file-helper");
const { getFilePath } = getFileHelper();

test("Plop to add and rename files", async () => {
const expectedFilePath = await getFilePath(
Expand Down
38 changes: 20 additions & 18 deletions tests/file-helper.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
const fs = require("fs");
const { resolve } = require("path");

let cleanupFile = null;
exports.getFileHelper = () => {
let cleanupFile = null;

afterEach(() => {
if (!cleanupFile) return;
try {
fs.unlinkSync(cleanupFile);
} catch (e) {}
cleanupFile = null;
});
afterEach(() => {
if (!cleanupFile) return;
try {
fs.unlinkSync(cleanupFile);
} catch (e) {}
cleanupFile = null;
});

const getFilePath = async (path) => {
const expectedFilePath = resolve(__dirname, path);
const getFilePath = async (path) => {
const expectedFilePath = resolve(__dirname, path);

cleanupFile = expectedFilePath;
try {
await fs.promises.unlink(cleanupFile);
} catch (e) {}
cleanupFile = expectedFilePath;
try {
await fs.promises.unlink(cleanupFile);
} catch (e) {}

return expectedFilePath;
};
return expectedFilePath;
};

module.exports = {
getFilePath,
return {
getFilePath,
};
};
13 changes: 10 additions & 3 deletions tests/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ const { resolve } = require("path");
function renderScript(script, args = [], opts = {}) {
const { cwd = __dirname } = opts;

const rendered = render("npx", ["nyc", "--silent", "node", script, ...args], {
cwd,
});
const rendered = render(
resolve(__dirname, "../node_modules/.bin/nyc"),
["--silent", "node", script, ...args],
{
cwd,
spawnOpts: {
env: { ...process.env, NODE_ENV: "test" },
},
}
);

return rendered;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { renderScript, renderPlop } = require("./render");
const { resolve } = require("path");
const { waitFor } = require("cli-testing-library");
const fs = require("fs");
const { getFilePath } = require("./file-helper");
const { getFileHelper } = require("./file-helper");
const { getFilePath } = getFileHelper();

const renderWrapper = (...props) => {
return renderScript(
Expand Down

0 comments on commit cb0bd7e

Please sign in to comment.