Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exhaust unit tests #579

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/__tests__/unit/lib/exhaust.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
jest.mock('fs', () => require('../../../__mocks__/fs'));

import {exhaust} from '../../../lib/exhaust';

import {ERRORS} from '../../../util/errors';

import {STRINGS} from '../../../config';

const {CliInputError, ModuleInitializationError} = ERRORS;
const {INVALID_EXHAUST_PLUGIN} = STRINGS;

describe('lib/exhaust: ', () => {
describe('exhaust(): ', () => {
const spy = jest.spyOn(global.console, 'log');

beforeEach(() => {
spy.mockReset();
});

it('returns void if no exhaust plugin selected, log should be called.', async () => {
const tree = {};
const context = {
initialize: {
outputs: null,
},
};

// @ts-ignore
const result = await exhaust(tree, context);

expect(result).toBeUndefined();
expect(spy).toHaveBeenCalledTimes(1);
spy.mockReset();
});

it('uses log exhaust plugin as export.', async () => {
const tree = {};
const context = {
initialize: {
outputs: ['log'],
},
};

// @ts-ignore
await exhaust(tree, context);
expect(spy).toHaveBeenCalledTimes(1);
});

it('rejects with cli input error if output path is not provided with yaml.', async () => {
const tree = {};
const context = {
initialize: {
outputs: ['yaml'],
},
};
const expectedMessage = 'Output path is required.';

expect.assertions(2);

try {
// @ts-ignore
await exhaust(tree, context);
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);

if (error instanceof CliInputError) {
expect(error.message).toEqual(expectedMessage);
}
}
});

it('rejects with module init error if output module is not supported.', async () => {
const tree = {};
const context = {
initialize: {
outputs: ['mock'],
},
};
const expectedMessage = INVALID_EXHAUST_PLUGIN(
context.initialize.outputs[0]
);

expect.assertions(2);

try {
// @ts-ignore
await exhaust(tree, context);
} catch (error) {
expect(error).toBeInstanceOf(ModuleInitializationError);

if (error instanceof ModuleInitializationError) {
expect(error.message).toEqual(expectedMessage);
}
}
});
});
});
13 changes: 10 additions & 3 deletions src/lib/exhaust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const initializeExhaustPlugins = (plugins: string[]) =>
plugins.map(initializeExhaustPlugin);

/**
* factory method for exhaust plugins
* Factory method for exhaust plugins.
*/
const initializeExhaustPlugin = (name: string): ExhaustPluginInterface => {
switch (name) {
Expand All @@ -44,7 +44,11 @@ const initializeExhaustPlugin = (name: string): ExhaustPluginInterface => {
* Output manager - Exhaust.
* Grabs output plugins from context, executes every.
*/
export const exhaust = (tree: any, context: Context, outputPath?: string) => {
export const exhaust = async (
tree: any,
context: Context,
outputPath?: string
) => {
const outputPlugins = context.initialize.outputs;

if (!outputPlugins) {
Expand All @@ -54,5 +58,8 @@ export const exhaust = (tree: any, context: Context, outputPath?: string) => {
}

const exhaustPlugins = initializeExhaustPlugins(outputPlugins);
exhaustPlugins.forEach(plugin => plugin.execute(tree, context, outputPath));

for await (const plugin of exhaustPlugins) {
await plugin.execute(tree, context, outputPath);
}
};
Loading