-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.test.js
More file actions
70 lines (60 loc) · 2.25 KB
/
cli.test.js
File metadata and controls
70 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Assertion
import { beforeEach, afterEach, describe, it } from "node:test";
import assert from "node:assert/strict";
// Built-in modules
import { readFile, rm } from "node:fs/promises";
import { resolve } from "node:path";
import { promisify } from "node:util";
import { exec } from "node:child_process";
// Local modules
import { ERR_MISSING_TEMPLATE } from "./src/errors.js";
const execAsync = promisify(exec);
const CLI_PATH = resolve("./src/cli.js");
function clearOutputDir() {
const outputDir = resolve("output");
// Clean up the output directory before each test
rm(outputDir, { recursive: true, force: true }).catch(() => {});
}
describe("YNDAP CLI", () => {
beforeEach(clearOutputDir);
afterEach(clearOutputDir);
// ============
// Successful test case
// ============
it("Create a file passing correct params, creating .js by default", async () => {
const templateName = "sum";
const outputDir = resolve("output/sum");
const targetFile = resolve(outputDir, "sum.js");
await execAsync(`node ${CLI_PATH} -t ${templateName} -o ${outputDir}`);
const content = await readFile(targetFile, "utf8");
assert.ok(content.includes("export default function sum"));
});
it("Create a file passing correct params, creating .ts by change extension", async () => {
const templateName = "sum";
const outputDir = resolve("output/sum");
const targetFile = resolve(outputDir, "sum.ts");
await execAsync(
`node ${CLI_PATH} -t ${templateName} -o ${outputDir} -e ts`,
);
const content = await readFile(targetFile, "utf8");
assert.ok(content.includes("export default function sum"));
});
it("Create a file passing an another github user", async () => {
const targetFile = resolve("output", "is-even.js");
await execAsync(
`node ${CLI_PATH} -t even -o ${targetFile} -r alexcastrodev/ydnap-example`,
);
const content = await readFile(targetFile, "utf8");
assert.ok(content.includes("export default function isEven"));
});
// ============
// Fail test case
// ============
it("Exit if missing template name", async () => {
const command = `node ${CLI_PATH}`;
await assert.rejects(execAsync(command), {
code: 1,
stderr: new RegExp(ERR_MISSING_TEMPLATE),
});
});
});