-
Notifications
You must be signed in to change notification settings - Fork 759
/
Copy pathcomplex.js
66 lines (54 loc) · 1.94 KB
/
complex.js
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
import path from 'node:path';
import { globSync } from 'glob';
import mapSpec, { plugins } from '../../src/specmap/index.js';
import Swagger from '../../src/index.js';
const { refs } = plugins;
const { allOf } = plugins;
describe('complex', () => {
beforeEach(() => {
refs.clearCache();
});
test('should resolve complex specs', () => {
jest.setTimeout(100000);
const dir = path.join(__dirname, 'data', 'complex');
const specFiles = globSync(`${dir}/**/*.json`);
const specs = specFiles
.sort((f1, f2) => {
const no1 = Number(path.basename(f1).split('.')[0]);
const no2 = Number(path.basename(f2).split('.')[0]);
return no1 - no2;
})
.map((filename) => ({ name: path.basename(filename), spec: require(filename) }));
// Runs test serially, just more convenient for debugging if a spec fails
return new Promise((resolve, reject) => {
function runNextTestCase(idx) {
if (idx === specs.length) {
return resolve();
}
const spec = specs[idx];
const startTime = new Date();
console.log('Run', spec.name); // eslint-disable-line no-console
return mapSpec({ spec: spec.spec, plugins: [refs, allOf] })
.then((res) => {
if (res.errors.length) throw res.errors[0];
expect(res.errors.length).toEqual(0);
const elapsed = new Date() - startTime;
console.log(' elapsed', elapsed, 'ms'); // eslint-disable-line no-console
})
.then(() => runNextTestCase(idx + 1))
.catch(reject);
}
runNextTestCase(0);
});
});
test('should resolve complex specs with allOf and nested references', async () => {
// Given
const spec = globalThis.loadJsonFile(
path.join(__dirname, 'data', 'specs', 'complex-example.json')
);
// When
const result = await Swagger.resolve({ spec });
// Then
expect(result).not.toEqual(null);
});
});