/
index.jest.test.ts
147 lines (125 loc) · 3.17 KB
/
index.jest.test.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { serveAssets } from '@stoplight/spectral-test-utils';
import { fetch } from '@stoplight/spectral-runtime';
import * as fs from 'fs';
import { bundleRuleset } from '../index';
import { IO } from '../types';
import { node } from '../presets/node';
import { browser } from '../presets/browser';
import { commonjs } from '../plugins/commonjs';
import { virtualFs } from '../plugins/virtualFs';
import { runtime } from '../presets/runtime';
import { builtins } from '../plugins/builtins';
jest.mock('fs');
describe('Ruleset Bundler', () => {
let io: IO;
beforeEach(() => {
io = {
fs,
fetch,
};
serveAssets({
'/p/.spectral/my-fn.js': `module.exports = function f() { return [] };`,
'/p/spectral.js': `import myFn from './.spectral/my-fn.js';
export default {
rules: {
rule: {
given: '$',
then: { function: myFn },
}
},
};`,
});
});
it('given runtime target, should support commonjs', async () => {
const code = await bundleRuleset('/p/spectral.js', {
target: 'runtime',
plugins: [...runtime(io), commonjs()],
});
expect(code).toContain(`\tvar myFn = function f() { return [] };
\tvar spectral = {
\t rules: {
\t rule: {
\t given: '$',
\t then: { function: myFn },
\t }
\t },
\t};
\treturn spectral;
})();`);
});
it('given browser target, should support commonjs', async () => {
const code = await bundleRuleset('/p/spectral.js', {
target: 'browser',
plugins: [...browser(io), commonjs()],
});
expect(code).toContain(`var myFn = function f() { return [] };
var spectral = {
rules: {
rule: {
given: '$',
then: { function: myFn },
}
},
};
export { spectral as default };`);
});
it('given node target, should support commonjs', async () => {
const code = await bundleRuleset('/p/spectral.js', {
target: 'node',
plugins: [...node(io), virtualFs(io), commonjs()],
});
expect(code).toContain(`var myFn = function f() { return [] };
var spectral = {
rules: {
rule: {
given: '$',
then: { function: myFn },
}
},
};
export { spectral as default };`);
});
it('given node target, should support commonjs for remote ruleset with builtin modules', async () => {
serveAssets({
'https://tmp/input.js': `var spectralFormats = require('@stoplight/spectral-formats');
var spectralFunctions = require('@stoplight/spectral-functions');
const ruleset = {
rules: {
'my-rule': {
given: '$',
then: {
function: spectralFunctions.schema,
functionOptions: {
schema: {
type: 'object',
},
},
},
},
},
};
module.exports = ruleset;
`,
});
const code = await bundleRuleset('https://tmp/input.js', {
target: 'node',
format: 'commonjs',
plugins: [builtins(), commonjs(), ...node({ fs, fetch }), virtualFs(io)],
});
expect(code).toContain(`const ruleset = {
rules: {
'my-rule': {
given: '$',
then: {
function: spectralFunctions.schema,
functionOptions: {
schema: {
type: 'object',
},
},
},
},
},
};`);
});
});