Skip to content

Commit

Permalink
test: simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherPHolder committed Mar 11, 2024
1 parent a1ab2dc commit a716aec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { handleOpenFlowReports, openFlowReports } from './open-report';
import { logVerbose } from '../../../../core/loggin';
import { CollectCommandOptions } from '../../options';

jest.mock('open');
jest.mock('../../../../core/loggin');

describe('handleOpenFlowReport', () => {

beforeEach(() => {
jest.clearAllMocks();
})
Expand All @@ -14,7 +18,7 @@ describe('handleOpenFlowReport', () => {
interactive: true,
dryRun: false,
} as CollectCommandOptions);
expect(typeof openReportsProcess).toEqual("function");
expect(openReportsProcess).toEqual(expect.any(Function));
});

it('should return undefined if openReport is false', () => {
Expand All @@ -23,7 +27,7 @@ describe('handleOpenFlowReport', () => {
interactive: true,
dryRun: false,
} as CollectCommandOptions);
expect(openReportsProcess).toEqual(undefined);
expect(openReportsProcess).toBeUndefined();
});

it('should return undefined if dryRun is true', () => {
Expand All @@ -32,7 +36,7 @@ describe('handleOpenFlowReport', () => {
interactive: true,
dryRun: true,
} as CollectCommandOptions);
expect(openReportsProcess).toEqual(undefined);
expect(openReportsProcess).toBeUndefined();
});

it('should return undefined if interactive is false', () => {
Expand All @@ -41,15 +45,10 @@ describe('handleOpenFlowReport', () => {
interactive: false,
dryRun: false,
} as CollectCommandOptions);
expect(openReportsProcess).toEqual(undefined);
expect(openReportsProcess).toBeUndefined();
});


});

jest.mock('open');
jest.mock('../../../../core/loggin');

describe('openReports', () => {

beforeEach(() => {
Expand All @@ -61,48 +60,18 @@ describe('openReports', () => {
expect(openReport).not.toHaveBeenCalled();
});

it('should not logVerbose if no file name is passed', async () => {
await openFlowReports([]);
expect(logVerbose).not.toHaveBeenCalled();
});

it('should open the report if filenames includes an html report', async () => {
await openFlowReports(['example.html']);
expect(openReport).toHaveBeenCalled();
});

it('should logVerbose if filenames includes an html report', async () => {
await openFlowReports(['example.html']);
expect(logVerbose).toHaveBeenCalledWith(expect.stringContaining('HTML'));
});

it('should open the report if filenames includes a json report', async () => {
await openFlowReports(['example.json']);
it.each(['html', 'json', 'md'])('should open the %s report', async (format) => {
await openFlowReports([`example.${format}`]);
expect(openReport).toHaveBeenCalled();
});

it('should logVerbose if filenames includes an json report', async () => {
await openFlowReports(['example.json']);
expect(logVerbose).toHaveBeenCalledWith(expect.stringContaining('JSON'));
});

it('should open the report if filenames includes a md report', async () => {
await openFlowReports(['example.md']);
expect(openReport).toHaveBeenCalled();
});

it('should logVerbose if filenames includes an md report', async () => {
await openFlowReports(['example.md']);
expect(logVerbose).toHaveBeenCalledWith(expect.stringContaining('Markdown'));
it('should not logVerbose if no file name is passed', async () => {
await openFlowReports([]);
expect(logVerbose).not.toHaveBeenCalled();
});

it('should only open 1 time report if multiple report formats are passed', async () => {
await openFlowReports(['example.html', 'example.json', 'example.md']);
expect(openReport).toHaveBeenCalledTimes(1);
});

it('should only logVerbose 1 time report if multiple report formats are passed', async () => {
await openFlowReports(['example.html', 'example.json', 'example.md']);
expect(logVerbose).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion packages/cli/src/lib/commands/init/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { REPORT_FORMAT_VALUES } from '../collect/constants';

const isValidFormat = (value: any): value is ReportFormat => REPORT_FORMAT_VALUES.includes(value);

function sanitizedFormats(formats: string[]): ReportFormat[] {
function sanitizedFormats(formats: string[]) {
const validatedFormats: ReportFormat[] = formats.filter(isValidFormat);
if (validatedFormats.length !== formats.length) {
throw new Error(`${formats} contains invalid format options`);
Expand Down

0 comments on commit a716aec

Please sign in to comment.