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

Update test suite for reporter #45

Merged
merged 2 commits into from
Oct 10, 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
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