test: add cli precedence test

This commit is contained in:
2025-06-19 01:27:38 +03:00
parent 4b0b4e7380
commit 29f2afe097
3 changed files with 46 additions and 5 deletions

View File

@@ -4,9 +4,9 @@ import { Console } from "console"
import { LogLevel, ScaffoldCmdConfig } from "../src/types"
import * as config from "../src/config"
import { resolve } from "../src/utils"
// @ts-ignore
import * as configFile from "../scaffold.config"
import configFile from "./test-config"
import { findConfigFile } from "../src/config"
import path from "path"
jest.mock("../src/git", () => {
return {
@@ -69,7 +69,7 @@ describe("config", () => {
describe("parseConfigFile", () => {
test("normal config does not change", async () => {
const tmpDir = `/tmp/scaffold-config-${Date.now()}`
const { quiet, tmpDir: _tmpDir, version, ...conf } = blankCliConf
const { quiet: _, tmpDir: _tmpDir, version: __, ...conf } = blankCliConf
expect(
await parseConfigFile({
...blankCliConf,
@@ -98,6 +98,20 @@ describe("config", () => {
})
expect(result?.data?.num).toEqual("1234")
})
test("CLI output overrides config file output", async () => {
const tmpDir = `/tmp/scaffold-config-${Date.now()}`
const result = await parseConfigFile({
...blankCliConf,
config: path.resolve(__dirname, "test-config.js"),
key: "component",
output: "examples/test-output/override",
name: "Component",
tmpDir,
})
expect(result.output).toEqual("examples/test-output/override")
})
})
})
@@ -114,10 +128,10 @@ describe("config", () => {
test("gets local file config", async () => {
const resultFn = await config.getLocalConfig({
config: "scaffold.config.js",
config: path.join(__dirname, "test-config.js"),
logLevel: LogLevel.none,
})
const result = await resolve(resultFn, {} as any)
const result = (await resolve(resultFn, {} as ScaffoldCmdConfig)).default
expect(result).toEqual(configFile)
})
})

3
tests/test-config.d.ts vendored Normal file
View File

@@ -0,0 +1,3 @@
declare const config: import("../dist").ScaffoldConfigFile;
export = config;

24
tests/test-config.js Normal file
View File

@@ -0,0 +1,24 @@
// @ts-check
/** @type {import('../dist').ScaffoldConfigFile} */
// eslint-disable-next-line no-undef
module.exports = (conf) => {
// eslint-disable-next-line no-undef
console.log("Config:", conf)
return {
default: {
templates: ["examples/test-input/Component"],
output: "examples/test-output",
data: { property: "myProp", value: "10" },
},
component: {
templates: ["examples/test-input/Component"],
output: "examples/test-output/component",
data: { property: "myProp", value: "10" },
},
configs: {
templates: ["examples/test-input/**/.*"],
output: "examples/test-output/configs",
name: "---",
},
}
}