Skip to content

Commit efc9dd3

Browse files
committed
test: refactoring
1 parent a5f71d7 commit efc9dd3

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

test/helpers/matchers.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616

1717
import {
18+
BuildResult,
1819
ChangeMessage,
1920
ChangeSummary,
2021
DeprecateItem,
2122
EMPTY_CHANGE_SUMMARY,
23+
MESSAGE_SEVERITY,
24+
NotificationMessage,
2225
OperationChanges,
2326
type OperationsApiType,
2427
OperationType,
@@ -32,6 +35,8 @@ import { ArrayContaining, ObjectContaining, RecursiveMatcher } from '../../.jest
3235
export type ApihubComparisonMatcher = ObjectContaining<VersionsComparison> & VersionsComparison
3336
export type ApihubOperationChangesMatcher = ObjectContaining<OperationChanges> & OperationChanges
3437
export type ApihubChangesSummaryMatcher = ObjectContaining<ChangeSummary> & ChangeSummary
38+
export type ApihubNotificationsMatcher = ObjectContaining<BuildResult> & BuildResult
39+
export type ApihubErrorNotificationMatcher = ObjectContaining<NotificationMessage> & NotificationMessage
3540
export type ApihubChangeMessagesMatcher = ArrayContaining<ChangeMessage> & ChangeMessage[]
3641

3742
export function apihubComparisonMatcher(
@@ -130,3 +135,21 @@ export function deprecatedItemDescriptionMatcher(
130135
}
131136

132137
type Matcher = ObjectContaining<DeprecateItem>
138+
139+
export function notificationsMatcher(
140+
expected: Array<RecursiveMatcher<NotificationMessage>>,
141+
): ApihubNotificationsMatcher {
142+
return expect.objectContaining({
143+
notifications: expect.toIncludeSameMembers(expected),
144+
},
145+
)
146+
}
147+
148+
export function errorNotificationMatcher(
149+
message: string | RegExp,
150+
): ApihubErrorNotificationMatcher {
151+
return expect.objectContaining({
152+
message: expect.stringMatching(message),
153+
severity: MESSAGE_SEVERITY.Error,
154+
})
155+
}

test/reference-bundling.test.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { LocalRegistry } from './helpers'
2-
import { MESSAGE_SEVERITY, VALIDATION_RULES_SEVERITY_LEVEL_ERROR } from '../src'
1+
import { errorNotificationMatcher, LocalRegistry, notificationsMatcher } from './helpers'
2+
import { VALIDATION_RULES_SEVERITY_LEVEL_ERROR } from '../src'
33

44
describe('Reference bundling test', () => {
55
test('external references should be bundled', async () => {
@@ -9,19 +9,22 @@ describe('Reference bundling test', () => {
99
expect(result.documents.get('openapi.yaml')?.dependencies).toEqual(['reference.yaml'])
1010
})
1111

12-
test('should throw on missing external reference', async () => {
12+
test('should throw on missing external reference if error severity level configured', async () => {
1313
const pkg = LocalRegistry.openPackage('reference-bundling/case2')
1414

1515
await expect(
1616
pkg.publish(pkg.packageId, { validationRulesSeverity: { brokenRefs: VALIDATION_RULES_SEVERITY_LEVEL_ERROR } }),
1717
).rejects.toThrow(/does not exist/)
1818
})
1919

20-
test('should not throw on missing external reference', async () => {
20+
test('should collect missing external reference notifications if severity level is not configured', async () => {
2121
const pkg = LocalRegistry.openPackage('reference-bundling/case2')
2222
const result = await pkg.publish(pkg.packageId)
2323

24-
expect(result.notifications.filter(({ severity }) => severity === MESSAGE_SEVERITY.Error).length).toEqual(2)
24+
expect(result).toEqual(notificationsMatcher([
25+
errorNotificationMatcher('references an invalid location'),
26+
errorNotificationMatcher('does not exist'),
27+
]))
2528
expect(result.operations.size).toBe(1)
2629
expect(result.documents.get('openapi.yaml')?.dependencies.length).toBe(0)
2730
})
@@ -36,40 +39,43 @@ describe('Reference bundling test', () => {
3639
])
3740
})
3841

39-
test('should throw on missing transitive external reference', async () => {
42+
test('should throw on missing transitive external reference if error severity level configured', async () => {
4043
const pkg = LocalRegistry.openPackage('reference-bundling/case4')
4144

4245
await expect(
4346
pkg.publish(pkg.packageId, { validationRulesSeverity: { brokenRefs: VALIDATION_RULES_SEVERITY_LEVEL_ERROR } }),
4447
).rejects.toThrow(/does not exist/)
4548
})
4649

47-
test('should not throw on missing transitive external reference', async () => {
50+
test('should collect notifications when transitive external reference is missing if severity level is not configured', async () => {
4851
const pkg = LocalRegistry.openPackage('reference-bundling/case4')
4952
const result = await pkg.publish(pkg.packageId)
5053

51-
expect(result.notifications.filter(({ severity }) => severity === MESSAGE_SEVERITY.Error).length).toEqual(2)
54+
expect(result).toEqual(notificationsMatcher([
55+
errorNotificationMatcher('references an invalid location'),
56+
errorNotificationMatcher('does not exist'),
57+
]))
5258
expect(result.operations.size).toBe(1)
5359
expect(result.documents.get('openapi.yaml')?.dependencies).toEqual(['reference.yaml'])
5460
})
5561

56-
test('should throw on missing internal reference', async () => {
62+
test('should throw on missing internal reference if error severity level configured', async () => {
5763
const pkg = LocalRegistry.openPackage('reference-bundling/case5')
5864

5965
await expect(
6066
pkg.publish(pkg.packageId, { validationRulesSeverity: { brokenRefs: VALIDATION_RULES_SEVERITY_LEVEL_ERROR } }),
6167
).rejects.toThrow(/references an invalid location/)
6268
})
6369

64-
test('should not throw on missing internal reference', async () => {
70+
test('should collect missing internal reference notification if severity level is not configured', async () => {
6571
const pkg = LocalRegistry.openPackage('reference-bundling/case5')
6672
const result = await pkg.publish(pkg.packageId)
6773

68-
expect(result.notifications.filter(({ severity }) => severity === MESSAGE_SEVERITY.Error).length).toEqual(1)
74+
expect(result).toEqual(notificationsMatcher([errorNotificationMatcher('references an invalid location')]))
6975
expect(result.operations.size).toBe(1)
7076
})
7177

72-
test('should throw on non-textual external reference', async () => {
78+
test('should throw on non-textual external reference if error severity level configured', async () => {
7379
const pkg = LocalRegistry.openPackage('reference-bundling/case6')
7480
await expect(
7581
pkg.publish(pkg.packageId, { validationRulesSeverity: { brokenRefs: VALIDATION_RULES_SEVERITY_LEVEL_ERROR } }),

0 commit comments

Comments
 (0)