forked from stoplightio/spectral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupKarma.ts
69 lines (57 loc) · 2.46 KB
/
setupKarma.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as jsonSpecv4 from 'ajv/lib/refs/json-schema-draft-04.json';
import { FetchMockSandbox } from 'fetch-mock';
const oasRuleset = JSON.parse(JSON.stringify(require('./rulesets/oas/index.json')));
const oasFunctions = JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/oas-functions.json')));
const oas2Schema = JSON.parse(JSON.stringify(require('./rulesets/oas/schemas/schema.oas2.json')));
const oas3Schema = JSON.parse(JSON.stringify(require('./rulesets/oas/schemas/schema.oas3.json')));
const asyncApiRuleset = JSON.parse(JSON.stringify(require('./rulesets/asyncapi/index.json')));
const asyncApiFunctions = JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/asyncapi-functions.json')));
const asyncApi2Schema = JSON.parse(JSON.stringify(require('./rulesets/asyncapi/schemas/schema.asyncapi2.json')));
const { fetch } = window;
let fetchMock: FetchMockSandbox;
beforeEach(() => {
fetchMock = require('fetch-mock').sandbox();
fetchMock.catch((url, _opts) => {
console.warn(`Url '${url}' hasn't been found. Have you forgotten to mock it in 'setupKarma.ts'?`);
return 404;
});
window.fetch = fetchMock;
fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas/index.json', {
status: 200,
body: JSON.parse(JSON.stringify(oasRuleset)),
});
fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas/schemas/schema.oas2.json', {
status: 200,
body: JSON.parse(JSON.stringify(oas2Schema)),
});
fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas/schemas/schema.oas3.json', {
status: 200,
body: JSON.parse(JSON.stringify(oas3Schema)),
});
fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/asyncapi/index.json', {
status: 200,
body: JSON.parse(JSON.stringify(asyncApiRuleset)),
});
fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/asyncapi/schemas/schema.asyncapi2.json', {
status: 200,
body: JSON.parse(JSON.stringify(asyncApi2Schema)),
});
[
['oas', oasFunctions],
['asyncapi', asyncApiFunctions],
].forEach(([rulesetName, funcs]) => {
for (const [name, fn] of Object.entries<string>(funcs)) {
fetchMock.get(`https://unpkg.com/@stoplight/spectral/rulesets/${rulesetName}/functions/${name}`, {
status: 200,
body: fn,
});
}
});
fetchMock.get('http://json-schema.org/draft-04/schema', {
status: 200,
body: JSON.parse(JSON.stringify(jsonSpecv4)),
});
});
afterEach(() => {
window.fetch = fetch;
});