Skip to content

Commit

Permalink
Merge pull request #45 from adorade/develop
Browse files Browse the repository at this point in the history
Update test suite for reporter
  • Loading branch information
adorade authored Oct 10, 2024
2 parents 35998ad + c4dfdf6 commit 1c67145
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/reporter-factory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function reporterFactory(config = {}) {
* 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 +48,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 1c67145

Please sign in to comment.