Skip to content

Commit

Permalink
wip tests for cspell-json-reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-gooze committed Sep 9, 2021
1 parent 265d8b4 commit 9607d70
Showing 6 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/cspell-json-reporter/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../jest.config');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`setToJSONReplacer converts Set to Array 1`] = `"{\\"foo\\":[\\"foo\\",\\"bar\\",123]}"`;

exports[`setToJSONReplacer ignores other values 1`] = `"{\\"123\\":\\"1\\",\\"foo\\":\\"bar\\",\\"obj\\":{\\"key\\":\\"value\\"},\\"array\\":[1,2,3]}"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validateSettings throws for invalid settings 1`] = `"cspell-json-reporter settings.outFile must be a string"`;

exports[`validateSettings throws for invalid settings 2`] = `"cspell-json-reporter settings.outFile must be a string"`;

exports[`validateSettings throws for invalid settings 3`] = `"cspell-json-reporter settings.verbose must be a boolean"`;

exports[`validateSettings throws for invalid settings 4`] = `"cspell-json-reporter settings.debug must be a boolean"`;
20 changes: 20 additions & 0 deletions packages/cspell-json-reporter/src/utils/setToJSONReplacer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { setToJSONReplacer } from "./setToJSONReplacer";

describe('setToJSONReplacer', () => {
it('converts Set to Array', () => {
const input = {
foo: new Set(['foo', 'bar', 123]),
};
expect(JSON.stringify(input, setToJSONReplacer)).toMatchSnapshot();
});

it('ignores other values', () => {
const input = {
foo: 'bar',
'123': '1',
obj: { key: 'value' },
array: [1, 2, 3],
};
expect(JSON.stringify(input, setToJSONReplacer)).toMatchSnapshot();
});
});
29 changes: 29 additions & 0 deletions packages/cspell-json-reporter/src/utils/validateSettings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CSpellJSONReporterSettings } from "../CSpellJSONReporterSettings";
import { validateSettings } from "./validateSettings";

describe('validateSettings', () => {
it('passes valid settings', () => {
const valid: CSpellJSONReporterSettings[] = [
{ outFile: 'foobar' },
{ outFile: 'foobar', verbose: true },
{ outFile: 'foobar', debug: false },
{ outFile: 'foobar', progress: true },
{ outFile: 'foobar', verbose: undefined },
];
valid.forEach((settings) => {
expect(validateSettings(settings)).toEqual(undefined);
});
});

it('throws for invalid settings', () => {
const invalid = [
{ },
{ outFile: 1 },
{ outFile: 'foobar', verbose: 123 },
{ outFile: 'foobar', debug: [] },
];
invalid.forEach((settings) => {
expect(() => validateSettings(settings)).toThrowErrorMatchingSnapshot();
});
});
});
19 changes: 16 additions & 3 deletions packages/cspell-json-reporter/src/utils/validateSettings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { AssertionError } from 'assert';
import { CSpellJSONReporterSettings } from '../CSpellJSONReporterSettings';

function assertBooleanOrUndefined(key: string, value: unknown): asserts value is boolean | undefined {
if (typeof value !== 'boolean' && value !== undefined) {
throw new TypeError(`cspell-json-reporter settings.${key} must be a boolean`);
throw new AssertionError({
message: `cspell-json-reporter settings.${key} must be a boolean`,
actual: typeof value,
expected: 'boolean',
});
}
}

@@ -11,13 +16,21 @@ function assertBooleanOrUndefined(key: string, value: unknown): asserts value is
*/
export function validateSettings(settings: unknown): asserts settings is CSpellJSONReporterSettings {
if (!settings || typeof settings !== 'object') {
throw new TypeError('cspell-json-reporter settings must be an object');
throw new AssertionError({
message: 'cspell-json-reporter settings must be an object',
actual: typeof settings,
expected: 'object',
});
}

const { outFile, debug, verbose, progress } = settings as CSpellJSONReporterSettings;

if (typeof outFile !== 'string') {
throw new TypeError('cspell-json-reporter settings.outFile must be a string');
throw new AssertionError({
message: 'cspell-json-reporter settings.outFile must be a string',
actual: typeof outFile,
expected: 'string',
});
}

assertBooleanOrUndefined('verbose', verbose);

0 comments on commit 9607d70

Please sign in to comment.