Skip to content

Commit

Permalink
chore(test-utils): cast error to object for proper assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Aug 23, 2022
1 parent 70ab1d5 commit b032723
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
8 changes: 7 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
const fs = require('fs');
const path = require('path');

const scopes = ['repo', 'test-harness', 'deps-dev', ...fs.readdirSync(path.join(__dirname, './packages'))];
const scopes = [
'repo',
'test-harness',
'test-utils',
'deps-dev',
...fs.readdirSync(path.join(__dirname, './packages')),
];

module.exports = {
extends: ['@commitlint/config-conventional'],
Expand Down
22 changes: 15 additions & 7 deletions packages/core/src/ruleset/validation/__tests__/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ describe('JS Ruleset Validation', () => {
expect(assertValidRuleset.bind(null, invalidRuleset)).toThrowAggregateError(
new AggregateError([
new RulesetValidationError('the rule must have at least "given" and "then" properties', [
'/rules/no-given-no-then',
'rules',
'no-given-no-then',
]),
new RulesetValidationError('allowed types are "style" and "validation"', [
'#/rules/rule-with-invalid-enum/type',
'rules',
'rule-with-invalid-enum',
'type',
]),
new RulesetValidationError('the value has to be one of: 0, 1, 2, 3 or "error", "warn", "info", "hint", "off"', [
'#/rules/rule-with-invalid-enum/severity',
'rules',
'rule-with-invalid-enum',
'severity',
]),
]),
);
Expand Down Expand Up @@ -427,7 +432,7 @@ describe('JS Ruleset Validation', () => {
[
new RulesetValidationError(
'must be a valid JSON Path expression or a reference to the existing Alias optionally paired with a JSON Path expression subset',
['aliases', 'PathItem'],
['aliases', 'PathItem', '0'],
),
],
],
Expand All @@ -436,7 +441,7 @@ describe('JS Ruleset Validation', () => {
[
new RulesetValidationError(
'must be a valid JSON Path expression or a reference to the existing Alias optionally paired with a JSON Path expression subset',
['aliases', 'PathItem'],
['aliases', 'PathItem', '0'],
),
],
],
Expand All @@ -446,7 +451,7 @@ describe('JS Ruleset Validation', () => {
[
new RulesetValidationError(
'must be a valid JSON Path expression or a reference to the existing Alias optionally paired with a JSON Path expression subset',
['aliases', 'PathItem'],
['aliases', 'PathItem', '0'],
),
],
],
Expand All @@ -472,7 +477,10 @@ describe('JS Ruleset Validation', () => {
}),
).toThrowAggregateError(
new AggregateError([
new RulesetValidationError('targets must be present and have at least a single alias definition', []),
new RulesetValidationError('targets must be present and have at least a single alias definition', [
'aliases',
'alias',
]),
]),
);
});
Expand Down
17 changes: 15 additions & 2 deletions test-utils/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ declare global {
}
}

function toPlainObject(ex: unknown): Record<string, unknown> {
if (ex instanceof Error) {
return {
...ex,
message: ex.message,
name: ex.name,
};
}

return Object(ex);
}

expect.extend({
toThrowAggregateError(received, expected) {
let error: unknown;
Expand All @@ -23,11 +35,12 @@ expect.extend({
error = received;
}

expect(error).toBeInstanceOf(Error);
expect(error).toEqual(expected);
expect((error as AggregateError).errors).toEqual(expected.errors);
expect((error as AggregateError).errors.map(toPlainObject)).toEqual(expected.errors.map(toPlainObject));

return {
message: () => 'All errors matched!',
message: (): string => 'All errors matched!',
pass: true,
};
},
Expand Down

0 comments on commit b032723

Please sign in to comment.