/
tests.ts
222 lines (171 loc) · 7.62 KB
/
tests.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import * as path from 'path';
import * as url from 'url';
import * as sinon from 'sinon';
import { EventEmitter2 } from 'eventemitter2';
import * as proxyquire from 'proxyquire';
import anyTest, { TestInterface } from 'ava';
import loadJSONFile from 'hint/dist/src/lib/utils/fs/load-json-file';
import { getAsUri } from 'hint/dist/src/lib/utils/network/as-uri';
import { Engine } from 'hint';
import { PackageJsonEvents } from '../src/parser';
import { FetchEnd } from 'hint/dist/src/lib/types';
type SandboxContext = {
sandbox: sinon.SinonSandbox;
};
const test = anyTest as TestInterface<SandboxContext>;
const validPath = path.join(__dirname, 'fixtures', 'valid', 'package.json');
const validJSON = loadJSONFile(validPath);
const invalidJSON = `{
"name": "app",
"version": "1.0.0",
"scripts": [
"echo "Error: no test specified" && exit 1"
],
"dependencies": {
"extend": "3.0.2"
},
"devDependencies": {
"gulp": "^4.0.0"
}
}`;
const mockContext = (sandbox: sinon.SinonSandbox) => {
const engine = new EventEmitter2({
delimiter: '::',
maxListeners: 0,
wildcard: true
}) as Engine<PackageJsonEvents>;
const engineEmitAsyncSpy: sinon.SinonSpy<any, any> = sandbox.spy(engine, 'emitAsync');
return { engine, engineEmitAsyncSpy };
};
const loadScript = (mockPath: boolean = false) => {
const newPath = {
basename(): string {
return 'foo';
}
};
const script = proxyquire('../src/parser', mockPath ? { path: newPath } : {});
return script.default;
};
test.beforeEach((t) => {
t.context.sandbox = sinon.createSandbox();
});
test.afterEach.always((t) => {
t.context.sandbox.restore();
});
test('If we receive a valid package.json, it should emit the event parse::end::package-json', async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: url.format(getAsUri(validPath)!),
response: { body: { content: JSON.stringify(validJSON) } }
} as FetchEnd);
// 3 calls, fetch json, parse start and parse end.
t.is(engineEmitAsyncSpy.callCount, 3);
t.is(engineEmitAsyncSpy.args[1][0], 'parse::start::package-json');
t.is(engineEmitAsyncSpy.args[2][0], 'parse::end::package-json');
});
test('If we receive a valid package.json, it should emit exactly the contents of the file', async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: url.format(getAsUri(validPath)!),
response: { body: { content: JSON.stringify(validJSON) } }
} as FetchEnd);
t.deepEqual(engineEmitAsyncSpy.args[2][1].config, validJSON);
});
test('If the .json file being fetched is not package.json, the only event emitted should be fetch::end::json.', async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript(true);
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: url.format(getAsUri(validPath)!),
response: { body: { content: JSON.stringify(validJSON) } }
} as FetchEnd);
t.true(engineEmitAsyncSpy.calledOnce);
t.is(engineEmitAsyncSpy.args[0][0], 'fetch::end::json');
});
test(`Even if package.json contains an invalid schema, it should still emit the 'parse::start::package-json' event`, async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: 'package.json',
response: { body: { content: invalidJSON } }
} as FetchEnd);
t.is(engineEmitAsyncSpy.args[1][0], 'parse::start::package-json');
});
test(`If package.json contains an invalid schema, it should emit an error related to the schema of the package.json`, async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: 'package.json',
response: { body: { content: invalidJSON } }
} as FetchEnd);
t.is(engineEmitAsyncSpy.args[2][0], 'parse::error::package-json::schema');
});
test(`If package.json contains an invalid schema, it should emit an array containing the errors `, async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: 'package.json',
response: { body: { content: invalidJSON } }
} as FetchEnd);
t.is(Object.keys(engineEmitAsyncSpy.args[2][1]).includes('errors'), true);
t.is(Array.isArray(engineEmitAsyncSpy.args[2][1].errors), true);
t.is(engineEmitAsyncSpy.args[2][1].errors.length, 1);
});
test(`If package.json contains an invalid schema, it should emit an array of errors that is easily understandable `, async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: 'package.json',
response: { body: { content: invalidJSON } }
} as FetchEnd);
t.is(Object.keys(engineEmitAsyncSpy.args[2][1]).includes('prettifiedErrors'), true);
t.is(Array.isArray(engineEmitAsyncSpy.args[2][1].prettifiedErrors), true);
t.is(engineEmitAsyncSpy.args[2][1].prettifiedErrors.length, 1);
});
test(`If the file contains an invalid json, it should emit an error related to the json format of the package.json`, async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
await engine.emitAsync('fetch::end::json', {
resource: 'package.json',
response: { body: { content: 'invalidJson' } }
} as FetchEnd);
// 2 calls, fetch json, and the error.
t.is(engineEmitAsyncSpy.callCount, 2);
t.is(engineEmitAsyncSpy.args[1][0], 'parse::error::package-json::json');
});
test(`If 'package.json' contains a custom property (e.g. 'browserslist'), it should still emit the event parse::end::package-json`, async (t) => {
const { engine, engineEmitAsyncSpy } = mockContext(t.context.sandbox);
const PackageJsonParser = loadScript();
new PackageJsonParser(engine); // eslint-disable-line no-new
const nonStandardJSON = `{
"name": "app",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
},
"browserslist": [
"last 1 version",
]
"dependencies": {
"extend": "3.0.2"
},
"devDependencies": {
"gulp": "^4.0.0"
}
}`;
await engine.emitAsync('fetch::end::json', {
resource: 'package.json',
response: { body: { content: nonStandardJSON } }
} as FetchEnd);
// 3 calls, fetch json, parse start and parse end.
t.is(engineEmitAsyncSpy.callCount, 3);
t.is(engineEmitAsyncSpy.args[2][0], 'parse::end::package-json');
});