@ghii/yaml-loader
Version:
A Funny yaml loader for ghii configuration manager
87 lines • 4.35 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const yaml_loader_1 = __importDefault(require("../yaml-loader"));
const fs = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
describe('Ghii Yaml Loader', () => {
it('export a function', () => {
expect(typeof yaml_loader_1.default).toBe('function');
});
describe('to create a loader', () => {
it('create a file loader from yaml file - check if is a function', async () => {
const yamlFileLoader = (0, yaml_loader_1.default)({ throwOnError: false }, __dirname, 'test.yaml');
await expect(typeof yamlFileLoader).toBe('function');
});
it('attempt to read not existent file throw Error', async () => {
expect(() => {
(0, yaml_loader_1.default)({ throwOnError: true }, __dirname, 'test_not_exist.yaml');
}).toThrow();
});
it('attempt to read not existent file with no throw Error options', async () => {
expect(() => {
(0, yaml_loader_1.default)({ throwOnError: false }, __dirname, 'test_not_exist.yaml');
}).not.toThrow();
});
it('attempt to read a folder throw Error', async () => {
expect((0, yaml_loader_1.default)({ throwOnError: true }, __dirname)).rejects.toBeInstanceOf(Error);
});
it('attempt to read a folder with no throw Error', async () => {
expect(await (0, yaml_loader_1.default)({ throwOnError: false }, __dirname)()).toStrictEqual({});
});
it('create a file loader from yaml file', async () => {
const content = await (0, yaml_loader_1.default)({ throwOnError: true }, __dirname, 'test.yaml')();
await expect(content).toStrictEqual({
foo: {
ciao: 'mondo',
},
});
});
it('create a file loader from yaml file removed after init', async () => {
const src = path_1.default.join(__dirname, '../__test__/test.yaml');
const copy = path_1.default.join(__dirname, '../__test__/test-temp-copy.yaml');
if (fs.existsSync(copy))
fs.rmSync(copy);
fs.copyFileSync(src, copy);
const fileLoader = await (0, yaml_loader_1.default)({ throwOnError: true }, __dirname, 'test-temp-copy.yaml');
fs.rmSync(copy);
await expect(fileLoader()).rejects.toBeInstanceOf(Error);
});
it('create a file loader from yaml file removed after init with no throw', async () => {
const src = path_1.default.join(__dirname, '../__test__/test.yaml');
const copy = path_1.default.join(__dirname, '../__test__/test-temp-copy.yaml');
if (fs.existsSync(copy))
fs.rmSync(copy);
fs.copyFileSync(src, copy);
const fileLoader = await (0, yaml_loader_1.default)({ throwOnError: false }, __dirname, 'test-temp-copy.yaml');
fs.rmSync(copy);
await expect(fileLoader()).resolves.toStrictEqual({});
});
});
});
//# sourceMappingURL=yaml-loader.test.js.map