mirror of
https://github.com/chenasraf/simple-scaffold.git
synced 2026-05-17 17:28:09 +00:00
build: migrate to vite+vitest
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { describe, test, expect, beforeEach, afterEach, beforeAll, vi } from "vitest"
|
||||
import mockFs from "mock-fs"
|
||||
import FileSystem from "mock-fs/lib/filesystem"
|
||||
import { Console } from "console"
|
||||
@@ -9,10 +10,10 @@ import { findConfigFile, getOptionValueForFile } from "../src/config"
|
||||
import { registerHelpers } from "../src/parser"
|
||||
import path from "path"
|
||||
|
||||
jest.mock("../src/git", () => {
|
||||
vi.mock("../src/git", async () => {
|
||||
const actual = await vi.importActual<typeof import("../src/git")>("../src/git")
|
||||
return {
|
||||
__esModule: true,
|
||||
...jest.requireActual("../src/git"),
|
||||
...actual,
|
||||
getGitConfig: () => {
|
||||
return Promise.resolve(blankCliConf)
|
||||
},
|
||||
@@ -321,7 +322,7 @@ describe("config", () => {
|
||||
})
|
||||
|
||||
test("calls function with file path info", () => {
|
||||
const fn = jest.fn().mockReturnValue("custom-output")
|
||||
const fn = vi.fn().mockReturnValue("custom-output")
|
||||
const result = getOptionValueForFile(conf, "/home/user/file.txt", fn)
|
||||
expect(result).toEqual("custom-output")
|
||||
expect(fn).toHaveBeenCalledWith(
|
||||
@@ -356,7 +357,7 @@ describe("config", () => {
|
||||
"scaffold.json": JSON.stringify(blankConfig),
|
||||
}
|
||||
|
||||
function withMock(fileStruct: FileSystem.DirectoryItems, testFn: jest.EmptyFunction): jest.EmptyFunction {
|
||||
function withMock(fileStruct: FileSystem.DirectoryItems, testFn: () => void): () => void {
|
||||
return () => {
|
||||
beforeEach(() => {
|
||||
// console.log("Mocking:", fileStruct)
|
||||
@@ -378,12 +379,15 @@ describe("config", () => {
|
||||
for (const struct of [struct1, struct2, struct3, struct4]) {
|
||||
const [k] = Object.keys(struct)
|
||||
|
||||
describe(`finds config file ${k}`, () => {
|
||||
withMock(struct, async () => {
|
||||
const result = await findConfigFile(process.cwd())
|
||||
expect(result).toEqual(k)
|
||||
})
|
||||
})
|
||||
describe(
|
||||
`finds config file ${k}`,
|
||||
withMock(struct, () => {
|
||||
test(`finds ${k}`, async () => {
|
||||
const result = await findConfigFile(process.cwd())
|
||||
expect(result).toEqual(k)
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
describe(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { describe, test, expect, beforeEach, afterEach } from "vitest"
|
||||
import mockFs from "mock-fs"
|
||||
import FileSystem from "mock-fs/lib/filesystem"
|
||||
import { Console } from "console"
|
||||
@@ -20,7 +21,7 @@ import { ScaffoldConfig, LogLevel } from "../src/types"
|
||||
import { registerHelpers } from "../src/parser"
|
||||
import { readFileSync } from "fs"
|
||||
|
||||
function withMock(fileStruct: FileSystem.DirectoryItems, testFn: jest.EmptyFunction): jest.EmptyFunction {
|
||||
function withMock(fileStruct: FileSystem.DirectoryItems, testFn: () => void): () => void {
|
||||
return () => {
|
||||
beforeEach(() => {
|
||||
console = new Console(process.stdout, process.stderr)
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
import { describe, test, expect, beforeEach, afterEach, vi, type MockInstance } from "vitest"
|
||||
import { log, logInitStep, logInputFile } from "../src/logger"
|
||||
import { LogLevel, ScaffoldConfig } from "../src/types"
|
||||
|
||||
describe("logger", () => {
|
||||
let consoleSpy: {
|
||||
log: jest.SpyInstance
|
||||
warn: jest.SpyInstance
|
||||
error: jest.SpyInstance
|
||||
log: MockInstance
|
||||
warn: MockInstance
|
||||
error: MockInstance
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
consoleSpy = {
|
||||
log: jest.spyOn(console, "log").mockImplementation(() => void 0),
|
||||
warn: jest.spyOn(console, "warn").mockImplementation(() => void 0),
|
||||
error: jest.spyOn(console, "error").mockImplementation(() => void 0),
|
||||
log: vi.spyOn(console, "log").mockImplementation(() => void 0),
|
||||
warn: vi.spyOn(console, "warn").mockImplementation(() => void 0),
|
||||
error: vi.spyOn(console, "error").mockImplementation(() => void 0),
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { describe, test, expect, beforeAll, afterAll } from "vitest"
|
||||
import { ScaffoldConfig } from "../src/types"
|
||||
import path from "node:path"
|
||||
import * as dateFns from "date-fns"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { describe, test, expect, beforeEach, afterEach, beforeAll, afterAll, vi, type MockInstance } from "vitest"
|
||||
import mockFs from "mock-fs"
|
||||
import FileSystem from "mock-fs/lib/filesystem"
|
||||
import Scaffold from "../src/scaffold"
|
||||
@@ -79,14 +80,14 @@ const fileStructExcludes = {
|
||||
output: {},
|
||||
}
|
||||
|
||||
function withMock(fileStruct: FileSystem.DirectoryItems, testFn: jest.EmptyFunction): jest.EmptyFunction {
|
||||
function withMock(fileStruct: FileSystem.DirectoryItems, testFn: () => void): () => void {
|
||||
return () => {
|
||||
beforeEach(() => {
|
||||
// console.log("Mocking:", fileStruct)
|
||||
console = new Console(process.stdout, process.stderr)
|
||||
|
||||
mockFs(fileStruct)
|
||||
// logMock = jest.spyOn(console, 'log').mockImplementation((...args) => {
|
||||
// logMock = vi.spyOn(console, 'log').mockImplementation((...args) => {
|
||||
// logsTemp.push(args)
|
||||
// })
|
||||
})
|
||||
@@ -200,9 +201,9 @@ describe("Scaffold", () => {
|
||||
describe(
|
||||
"errors",
|
||||
withMock(fileStructNormal, () => {
|
||||
let consoleMock1: jest.SpyInstance
|
||||
let consoleMock1: MockInstance
|
||||
beforeAll(() => {
|
||||
consoleMock1 = jest.spyOn(console, "error").mockImplementation(() => void 0)
|
||||
consoleMock1 = vi.spyOn(console, "error").mockImplementation(() => void 0)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
@@ -238,9 +239,9 @@ describe("Scaffold", () => {
|
||||
describe(
|
||||
"dry run",
|
||||
withMock(fileStructNormal, () => {
|
||||
let consoleMock1: jest.SpyInstance
|
||||
let consoleMock1: MockInstance
|
||||
beforeAll(() => {
|
||||
consoleMock1 = jest.spyOn(console, "error").mockImplementation(() => void 0)
|
||||
consoleMock1 = vi.spyOn(console, "error").mockImplementation(() => void 0)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
@@ -279,7 +280,8 @@ describe("Scaffold", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
describe("output structure", () => {
|
||||
describe(
|
||||
"output structure",
|
||||
withMock(fileStructNested, () => {
|
||||
test("should maintain input structure on output", async () => {
|
||||
await Scaffold({
|
||||
@@ -304,23 +306,26 @@ describe("Scaffold", () => {
|
||||
expect(oneDeepFile.toString()).toEqual("Hello, my value is 1")
|
||||
expect(twoDeepFile.toString()).toEqual("Hi! My value is actually NOT 1!")
|
||||
})
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
describe(
|
||||
"file exclusion via glob pattern",
|
||||
withMock(fileStructExcludes, () => {
|
||||
test("should exclude files", async () => {
|
||||
test("should only include matching files", async () => {
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input", "!exclude.txt"],
|
||||
templates: ["input/include.*"],
|
||||
data: { value: "1" },
|
||||
logLevel: "none",
|
||||
})
|
||||
const includeFile = readFileSync(join(process.cwd(), "output", "app_name.txt"))
|
||||
expect(includeFile.toString()).toEqual("This file should be included")
|
||||
expect(() => readFileSync(join(process.cwd(), "output", "exclude.txt"))).toThrow()
|
||||
const outputFiles = readdirSync(join(process.cwd(), "output"))
|
||||
expect(outputFiles).toContain("include.txt")
|
||||
expect(outputFiles).not.toContain("exclude.txt")
|
||||
})
|
||||
})
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
describe(
|
||||
"capitalization helpers",
|
||||
@@ -893,7 +898,7 @@ describe("Scaffold", () => {
|
||||
},
|
||||
() => {
|
||||
test("beforeWrite gets content, rawContent, and outputPath", async () => {
|
||||
const beforeWriteSpy = jest.fn().mockReturnValue(undefined)
|
||||
const beforeWriteSpy = vi.fn().mockReturnValue(undefined)
|
||||
await Scaffold({
|
||||
name: "app",
|
||||
output: "output",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { describe, test, expect } from "vitest"
|
||||
import { handleErr, resolve, wrapNoopResolver, colorize, TermColor } from "../src/utils"
|
||||
describe("utils", () => {
|
||||
describe("resolve", () => {
|
||||
|
||||
Reference in New Issue
Block a user