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

test(asyncapi): use a simple rule tester #1650

Merged
merged 5 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion __karma__/jest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Expect } from 'expect/build/types';
import * as JestMock from 'jest-mock';

declare var global: NodeJS.Global & {
declare let global: NodeJS.Global & {
jest: typeof JestMock;
expect: Expect;
test: jest.It;
Expand All @@ -10,6 +10,7 @@ declare var global: NodeJS.Global & {
global.jest = require('jest-mock');
global.expect = require('expect');
global.test = it;
global.test.concurrent = it;

const message = () => "Good try. An email has been sent to Vincenzo and Jakub, and they'll find you. :troll: ;)";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { IRuleResult, Spectral } from '../../../../spectral';
import * as ruleset from '../../index.json';
import { STATIC_ASSETS } from '../../../../assets';
import { empty } from '../../../../utils';
import { Spectral } from '../../../../spectral';
import { isAsyncApiv2 } from '../../../../formats';
import * as ruleset from '../../index.json';

export async function createWithRules(rules: (keyof typeof ruleset['rules'])[]): Promise<Spectral> {
type Scenario = ReadonlyArray<
Readonly<{
name: string;
document: Record<string, unknown>;
errors: ReadonlyArray<Partial<IRuleResult>>;
}>
>;

export default (ruleName: keyof typeof ruleset['rules'], tests: Scenario): void => {
describe(`AsyncAPI: rule ${ruleName}`, () => {
it.concurrent.each(tests)('$name', async testCase => {
const s = await createWithRules([ruleName]);
const doc = JSON.stringify(testCase.document);
expect(await s.run(doc)).toEqual(testCase.errors.map(error => expect.objectContaining(error) as unknown));
});
});
};

async function createWithRules(rules: (keyof typeof ruleset['rules'])[]): Promise<Spectral> {
try {
Object.assign(STATIC_ASSETS, await import('../../../../../rulesets/assets/assets.asyncapi.json'), {
'my-ruleset': JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-channel-no-empty-parameter', [
{
name: 'valid case',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {},
},
},
errors: [],
},

{
name: 'channels.{channel} contains empty parameter substitution pattern',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {},
'users/{}/signedOut': {},
},
},
errors: [
{
message: 'Channel path should not have empty parameter substitution pattern.',
path: ['channels', 'users/{}/signedOut'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-channel-no-query-nor-fragment', [
{
name: 'valid case',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {},
},
},
errors: [],
},

{
name: 'channels.{channel} contains a query delimiter',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {},
'users/{userId}/signedOut?byMistake={didFatFingerTheSignOutButton}': {},
},
},
errors: [
{
message: 'Channel path should not include a query (`?`) or a fragment (`#`) delimiter.',
path: ['channels', 'users/{userId}/signedOut?byMistake={didFatFingerTheSignOutButton}'],
severity: DiagnosticSeverity.Warning,
},
],
},

{
name: 'channels.{channel} contains a fragment delimiter',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {},
'users/{userId}/signedOut#onPurpose': {},
},
},
errors: [
{
message: 'Channel path should not include a query (`?`) or a fragment (`#`) delimiter.',
path: ['channels', 'users/{userId}/signedOut#onPurpose'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-channel-no-trailing-slash', [
{
name: 'valid case',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {},
},
},
errors: [],
},

{
name: 'channels.{channel} ends with a trailing slash',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {},
'users/{userId}/signedOut/': {},
},
},
errors: [
{
message: 'Channel path should not end with a slash.',
path: ['channels', 'users/{userId}/signedOut/'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);

This file was deleted.

Loading