cli-loader: use mock-fs for tests

This commit is contained in:
Patrik Oldsberg
2020-09-01 23:04:02 +02:00
parent 8da37131da
commit 905a924c18
4 changed files with 75 additions and 73 deletions
+3 -1
View File
@@ -37,8 +37,10 @@
},
"devDependencies": {
"@types/jest": "^26.0.7",
"@types/mock-fs": "^4.10.0",
"@types/node": "^12.0.0",
"@types/yup": "^0.28.2"
"@types/yup": "^0.28.2",
"mock-fs": "^4.13.0"
},
"files": [
"dist"
+38 -35
View File
@@ -14,46 +14,48 @@
* limitations under the License.
*/
const pathExists = jest.fn();
jest.mock('fs-extra', () => ({ pathExists }));
import mockFs from 'mock-fs';
import { resolveStaticConfig } from './resolver';
function normalizePaths(paths: string[]) {
return paths.map(p =>
p
.replace(/^[a-z]:/i, '')
.split('\\')
.join('/'),
);
}
describe('resolveStaticConfig', () => {
afterEach(() => {
jest.resetAllMocks();
mockFs.restore();
});
it('should resolve no files for empty roots', async () => {
mockFs({});
const resolved = await resolveStaticConfig({
env: 'development',
rootPaths: [],
});
expect(resolved).toEqual([]);
expect(pathExists).not.toHaveBeenCalled();
expect(normalizePaths(resolved)).toEqual([]);
});
it('should resolve a single app-config', async () => {
pathExists.mockImplementation(async (path: string) =>
['/repo/app-config.yaml'].includes(path),
);
mockFs({ '/repo/app-config.yaml': '' });
const resolved = await resolveStaticConfig({
env: 'development',
rootPaths: ['/repo'],
});
expect(resolved).toEqual(['/repo/app-config.yaml']);
expect(pathExists).toHaveBeenCalledTimes(4);
expect(normalizePaths(resolved)).toEqual(['/repo/app-config.yaml']);
});
it('should resolve a app-configs in different directories', async () => {
pathExists.mockImplementation(async (path: string) =>
['/repo/app-config.yaml', '/repo/packages/a/app-config.yaml'].includes(
path,
),
);
mockFs({
'/repo/app-config.yaml': '',
'/repo/packages/a/app-config.yaml': '',
});
const resolved = await resolveStaticConfig({
env: 'development',
rootPaths: [
@@ -64,53 +66,54 @@ describe('resolveStaticConfig', () => {
],
});
expect(resolved).toEqual([
expect(normalizePaths(resolved)).toEqual([
'/repo/app-config.yaml',
'/repo/packages/a/app-config.yaml',
]);
expect(pathExists).toHaveBeenCalledTimes(16);
});
it('should resolve env and local configs', async () => {
pathExists.mockImplementation(async (path: string) =>
[
'/repo/app-config.yaml',
'/repo/app-config.local.yaml',
'/repo/app-config.production.yaml',
'/repo/app-config.production.local.yaml',
'/repo/app-config.development.local.yaml',
'/repo/packages/a/app-config.development.yaml',
'/repo/packages/a/app-config.local.yaml',
].includes(path),
);
mockFs({
'/repo/app-config.yaml': '',
'/repo/app-config.local.yaml': '',
'/repo/app-config.production.yaml': '',
'/repo/app-config.production.local.yaml': '',
'/repo/app-config.development.local.yaml': '',
'/repo/packages/a/app-config.development.yaml': '',
'/repo/packages/a/app-config.local.yaml': '',
});
const resolved = await resolveStaticConfig({
env: 'development',
rootPaths: ['/repo', '/repo/packages/a'],
});
expect(resolved).toEqual([
expect(normalizePaths(resolved)).toEqual([
'/repo/app-config.yaml',
'/repo/app-config.local.yaml',
'/repo/app-config.development.local.yaml',
'/repo/packages/a/app-config.local.yaml',
'/repo/packages/a/app-config.development.yaml',
]);
expect(pathExists).toHaveBeenCalledTimes(8);
});
it('resolves suffixed configs in the correct order', async () => {
pathExists.mockImplementation(async () => true);
mockFs({
'/repo/app-config.yaml': '',
'/repo/app-config.local.yaml': '',
'/repo/app-config.production.yaml': '',
'/repo/app-config.production.local.yaml': '',
});
const resolved = await resolveStaticConfig({
env: 'production',
rootPaths: ['/repo'],
});
expect(resolved).toEqual([
expect(normalizePaths(resolved)).toEqual([
'/repo/app-config.yaml',
'/repo/app-config.local.yaml',
'/repo/app-config.production.yaml',
'/repo/app-config.production.local.yaml',
]);
expect(pathExists).toHaveBeenCalledTimes(4);
});
});
+22 -37
View File
@@ -15,45 +15,30 @@
*/
import { loadConfig } from './loader';
jest.mock('fs-extra', () => {
const mockFiles: { [path in string]: string } = {
'/root/app-config.yaml': `
app:
title: Example App
sessionKey:
$secret:
file: secrets/session-key.txt
`,
'/root/app-config.development.yaml': `
app:
sessionKey: development-key
`,
'/root/secrets/session-key.txt': 'abc123',
'/secret-port/app-config.yaml': `
backend:
listen:
port:
$secret:
file: secrets/port.txt
`,
'/secret-port/secrets/port.txt': '12345',
};
return {
async readFile(path: string) {
if (path in mockFiles) {
return mockFiles[path];
}
throw new Error(`File not found, ${path}`);
},
async pathExists(path: string) {
return path in mockFiles;
},
};
});
import mockFs from 'mock-fs';
describe('loadConfig', () => {
beforeAll(() => {
mockFs({
'/root/app-config.yaml': `
app:
title: Example App
sessionKey:
$secret:
file: secrets/session-key.txt
`,
'/root/app-config.development.yaml': `
app:
sessionKey: development-key
`,
'/root/secrets/session-key.txt': 'abc123',
});
});
afterAll(() => {
mockFs.restore();
});
it('loads config without secrets', async () => {
await expect(
loadConfig({
+12
View File
@@ -4460,6 +4460,13 @@
dependencies:
"@types/node" "*"
"@types/mock-fs@^4.10.0":
version "4.10.0"
resolved "https://registry.npmjs.org/@types/mock-fs/-/mock-fs-4.10.0.tgz#460061b186993d76856f669d5317cda8a007c24b"
integrity sha512-FQ5alSzmHMmliqcL36JqIA4Yyn9jyJKvRSGV3mvPh108VFatX7naJDzSG4fnFQNZFq9dIx0Dzoe6ddflMB2Xkg==
dependencies:
"@types/node" "*"
"@types/morgan@^1.9.0":
version "1.9.1"
resolved "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.1.tgz#6457872df95647c1dbc6b3741e8146b71ece74bf"
@@ -14682,6 +14689,11 @@ mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@^0.5.3, mkdirp@^0.5.4, mkdirp@^0.5.5, mkdir
dependencies:
minimist "^1.2.5"
mock-fs@^4.13.0:
version "4.13.0"
resolved "https://registry.npmjs.org/mock-fs/-/mock-fs-4.13.0.tgz#31c02263673ec3789f90eb7b6963676aa407a598"
integrity sha512-DD0vOdofJdoaRNtnWcrXe6RQbpHkPPmtqGq14uRX0F8ZKJ5nv89CVTYl/BZdppDxBDaV0hl75htg3abpEWlPZA==
modify-values@^1.0.0:
version "1.0.1"
resolved "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"