application-services
Version:
Out of the box application environment and configuration service.
104 lines • 3.21 kB
JavaScript
import { describe, it, beforeEach, jest, expect } from '@jest/globals';
import { YError } from 'yerror';
import initAppConfig from './APP_CONFIG.js';
import { NodeEnv } from './ENV.js';
describe('initAppConfig', () => {
const log = jest.fn();
const importer = jest.fn();
beforeEach(() => {
log.mockReset();
importer.mockReset();
});
it('should work with existing configs', async () => {
importer.mockResolvedValueOnce({
default: {
BASE_ENV: {
NODE_ENV: NodeEnv.Test,
},
},
});
const APP_CONFIG = await initAppConfig({
APP_ENV: 'local',
MAIN_FILE_URL: 'file:///home/whoami/my-whook-project/src/index.js',
log,
importer,
});
expect({
APP_CONFIG,
logCalls: log.mock.calls.filter((args) => 'debug-stack' !== args[0]),
importerCalls: importer.mock.calls,
}).toMatchInlineSnapshot(`
{
"APP_CONFIG": {
"BASE_ENV": {
"NODE_ENV": "test",
},
},
"importerCalls": [
[
"file:///home/whoami/my-whook-project/src/config/local/config.js",
],
],
"logCalls": [
[
"debug",
"🏭 - Initializing the APP_CONFIG service.",
],
[
"warning",
"⚡ - Loading configurations from "file:///home/whoami/my-whook-project/src/config/local/config.js".",
],
],
}
`);
});
it('should fail with non-existing file', async () => {
importer.mockImplementationOnce(() => {
throw new Error('EEXISTS');
});
try {
await initAppConfig({
APP_ENV: 'local',
MAIN_FILE_URL: 'file:///home/whoami/my-whook-project/src/index.js',
log,
importer,
});
throw new YError('E_UNEXPECTED_SUCCESS');
}
catch (err) {
expect({
errorCode: err.code,
errorDebug: err.debug,
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
importerCalls: importer.mock.calls,
}).toMatchInlineSnapshot(`
{
"errorCode": "E_NO_CONFIG",
"errorDebug": [
"file:///home/whoami/my-whook-project/src/config/local/config.js",
],
"importerCalls": [
[
"file:///home/whoami/my-whook-project/src/config/local/config.js",
],
],
"logCalls": [
[
"debug",
"🏭 - Initializing the APP_CONFIG service.",
],
[
"warning",
"⚡ - Loading configurations from "file:///home/whoami/my-whook-project/src/config/local/config.js".",
],
[
"warning",
"☢ - Could not load configuration file "file:///home/whoami/my-whook-project/src/config/local/config.js".",
],
],
}
`);
}
});
});
//# sourceMappingURL=APP_CONFIG.test.js.map