Skip to content

Commit

Permalink
test (#508): Added basic tests for @kipper/config
Browse files Browse the repository at this point in the history
To be extended!
  • Loading branch information
Luna-Klatzer committed Feb 13, 2024
1 parent b207bc3 commit c6cdca1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
6 changes: 4 additions & 2 deletions test/config-files/kip-config.basic.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"version": "*",
"target": "ts"
},
"files": [
"src/index.kip"
"test/kipper-files/main.kip"
],
"resources": [
"my-image.png"
"img/icon.png"
]
}
33 changes: 33 additions & 0 deletions test/module/config/kipper-config-file-interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
import { getFileName } from "./utils/utils";
import { assert } from "chai";
import { KipperConfigFile, KipperConfigInterpreter, version as kipConfigVersion } from "@kipper/config";
import * as semver from "semver";

const basicKipConfig = getFileName("kip-config.basic.json");

describe("KipperConfigInterpreter", () => {
describe("loadConfig", () => {
describe("no extends", () => {
const interpreter = new KipperConfigInterpreter();

it("should load a basic config", async () => {
const kipperConfigFile = await KipperConfigFile.fromFile(basicKipConfig, "utf8");
const config = await interpreter.loadConfig(kipperConfigFile);

const pwd = process.cwd();
assert.deepEqual(config.raw, {
basePath: pwd,
srcDir: `${pwd}/test/kipper-files`,
outDir: `${pwd}/dist`,
compiler: {
version: semver.parse(semver.clean(kipConfigVersion))!,
target: "ts"
},
files: [
{ src: `${pwd}/test/kipper-files/main.kip`, outDir: `${pwd}/dist` }
],
resources: [
{ src: `${pwd}/img/icon.png`, out: `${pwd}/dist/img/icon.png` }
]
});
});
});
});
});
42 changes: 42 additions & 0 deletions test/module/config/kipper-config-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
import { FileNotFoundError, KipperConfigFile } from "@kipper/config";
import { getFileName } from "./utils/utils";
import { assert } from "chai";
import * as path from "path";
import * as fs from "node:fs/promises";

const basicKipConfig = getFileName("kip-config.basic.json");

describe("KipperConfigFile", () => {
const encoding = "utf8";

describe("fromString", () => {
it("should return a KipperConfigFile instance", () => {
const kipConfig = KipperConfigFile.fromString("{}", encoding);
assert.instanceOf(kipConfig, KipperConfigFile);
assert.strictEqual(kipConfig.content, "{}");
assert.deepEqual(kipConfig.parsedJSON, {});
assert.strictEqual(kipConfig.fileName, "<string>");
assert.strictEqual(kipConfig.encoding, encoding);
});
});

describe("fromFile", () => {
it("should return a KipperConfigFile instance", async () => {
// First get the file content to check what we are expecting
const fileContent = await fs.readFile(basicKipConfig, { encoding });

const kipConfig = await KipperConfigFile.fromFile(basicKipConfig, encoding);
assert.instanceOf(kipConfig, KipperConfigFile);
assert.strictEqual(kipConfig.content, fileContent);
assert.deepEqual(kipConfig.parsedJSON, JSON.parse(fileContent));
assert.strictEqual(kipConfig.fileName, path.basename(basicKipConfig));
assert.strictEqual(kipConfig.encoding, encoding);
});

it("should throw an error if the file does not exist", async () => {
try {
await KipperConfigFile.fromFile(getFileName("does-not-exist.json"), encoding);
assert.fail("Expected an error to be thrown");
} catch (error) {
assert.instanceOf(error, FileNotFoundError);
}
});
});
});
5 changes: 5 additions & 0 deletions test/module/config/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as path from "path";

export function getFileName(pathString: string): string {
return path.resolve(`${__dirname}/../../../config-files/${pathString}`);
}

0 comments on commit c6cdca1

Please sign in to comment.