Skip to content

Commit

Permalink
test(reporter-factory): update test suite for reporter
Browse files Browse the repository at this point in the history
This commit expands the test coverage for the reporter factory,
ensuring correct behavior across various configurations and edge cases.
  • Loading branch information
adorade committed Oct 10, 2024
1 parent ae7f44e commit 24a007b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/reporter-factory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export default function reporterFactory(config = {}) {
* @returns {Promise<void[]>}
*/
async function reporter(result) {
// console.log('reporter', result.results);

/**
* The formatter to be used for formatting results.
* @type {Formatter}
*/
let formatter = config.formatter || 'string';
let formatter = config.formatter;

if (typeof formatter === 'string') {
if (formatter === 'stylish') {
Expand All @@ -48,7 +50,9 @@ export default function reporterFactory(config = {}) {
} else {
const buildFormatter = 'stylish, compact, github, json, string, tap, unix, verbose';

throw new Error(`Invalid formatter: ${reporter.formatter}. Use one of: "${buildFormatter}"`);
throw new Error(
`Invalid formatter: "${config.formatter}". Use one of: "${buildFormatter}" or a function.`
);
}
}

Expand Down
69 changes: 69 additions & 0 deletions test/reporter-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,75 @@ describe('Reporter Functionality', () => {
expect(typeof reporter({}).then).toBe('function');
});
});
describe('Reporter behavior with formatter', () => {
it('should correctly set formatter to stylishFormatter when "stylish" string is provided', async () => {
const config = { formatter: 'stylish' };
const reporter = reporterFactory(config);
const result = { results: [] };

await reporter(result);

expect(config.formatter).toBe('stylish');
});
it('should throw an error when formatter is set to an invalid string not in the predefined list', () => {
const validFormatter = 'stylish, compact, github, json, string, tap, unix, verbose';
const config = { formatter: 'invalid-formatter' };
const reporter = reporterFactory(config);

expect(() => reporter({})).rejects.toThrow(
`Invalid formatter: "${config.formatter}". Use one of: "${validFormatter}" or a function.`
);
});
it('should correctly set formatter to the appropriate function when a valid formatter string (other than "stylish") is provided', async () => {
const validFormatter = 'json';
const config = { formatter: validFormatter };
const reporter = reporterFactory(config);
const result = { results: [] };

await reporter(result);

expect(config.formatter).toBe(validFormatter);
expect(typeof config.formatter).toBe('string');
});
it('should throw an error for case-sensitive formatter string', () => {
const config = { formatter: 'StYlIsH' };
const reporter = reporterFactory(config);

expect(() => reporter({})).rejects.toThrow(`Invalid formatter: "${config.formatter}".`);
});
it('should maintain the original config object structure after setting the formatter', async () => {
const originalConfig = {
formatter: 'stylish',
console: true,
log: 'output.txt'
};
const reporter = reporterFactory(originalConfig);
const result = { results: [] };

await reporter(result);

expect(originalConfig).toEqual({
formatter: 'stylish',
console: true,
log: 'output.txt'
});
expect(typeof originalConfig.formatter).toBe('string');

await fs.unlink(originalConfig.log);
});
it('should handle a custom formatter function that returns a Promise', async () => {
const customFormatter = () => 'custom formatted result';
const config = { formatter: customFormatter };
const reporter = reporterFactory(config);
const result = { results: [] };

await reporter(result);

expect(config.formatter).toBe(customFormatter);
expect(typeof config.formatter).toBe('function');
expect(config.formatter()).toBe('custom formatted result');
});
});
describe('Reporter behavior with console', () => {
it('reporter should write to console when console param is true', () => {
stub(process.stdout, 'write');
Expand Down

0 comments on commit 24a007b

Please sign in to comment.