/
rule-tester.js
613 lines (539 loc) · 21.4 KB
/
rule-tester.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/**
* @fileoverview Tests for ESLint Tester
* @author Nicholas C. Zakas
*/
"use strict";
/* global describe, it */
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var sinon = require("sinon"),
eslint = require("../../../lib/eslint"),
RuleTester = require("../../../lib/testers/rule-tester"),
assert = require("chai").assert;
//------------------------------------------------------------------------------
// Rewire Things
//------------------------------------------------------------------------------
/*
* So here's the situation. Because RuleTester uses it() and describe() from
* Mocha, any failures would show up in the output of this test file. That means
* when we tested that a failure is thrown, that would also count as a failure
* in the testing for RuleTester. In order to remove those results from the
* results of this file, we need to overwrite it() and describe() just in
* RuleTester to do nothing but run code. Effectively, it() and describe()
* just become regular functions inside of index.js, not at all related to Mocha.
* That allows the results of this file to be untainted and therefore accurate.
*/
RuleTester.describe = function(text, method) {
return method.apply(this);
};
RuleTester.it = function(text, method) {
return method.apply(this);
};
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("RuleTester", function() {
var ruleTester;
beforeEach(function() {
RuleTester.resetDefaultConfig();
ruleTester = new RuleTester();
});
it("should not throw an error when everything passes", function() {
assert.doesNotThrow(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression"}] }
]
});
});
});
it("should throw an error when valid code is invalid", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"eval(foo)"
],
invalid: [
{ code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression"}] }
]
});
}, /Should have no errors but had 1/);
});
it("should throw an error when valid code is invalid", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
{ code: "eval(foo)" }
],
invalid: [
{ code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression"}] }
]
});
}, /Should have no errors but had 1/);
});
it("should throw an error if invalid code is valid", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "Eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression"}] }
]
});
}, /Should have 1 error but had 0/);
});
it("should throw an error when the error message is wrong", function() {
assert.throws(function() {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
// Only the invalid test matters here
valid: [
"bar = baz;"
],
invalid: [
{ code: "var foo = bar;", errors: [{ message: "Bad error message." }] }
]
});
}, /Bad var\..*==.*Bad error message/);
});
it("should throw an error when the error is neither an object nor a string", function() {
assert.throws(function() {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
// Only the invalid test matters here
valid: [
"bar = baz;"
],
invalid: [
{ code: "var foo = bar;", errors: [42] }
]
});
}, /Error should be a string or object/);
});
it("should throw an error when the error is a string and it does not match error message", function() {
assert.throws(function() {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
// Only the invalid test matters here
valid: [
"bar = baz;"
],
invalid: [
{ code: "var foo = bar;", errors: ["Bad error message."] }
]
});
}, /Bad var\..*==.*Bad error message/);
});
it("should not throw an error when the error is a string and it matches error message", function() {
assert.doesNotThrow(function() {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
// Only the invalid test matters here
valid: [
"bar = baz;"
],
invalid: [
{ code: "var foo = bar;", errors: ["Bad var."] }
]
});
});
});
it("should throw an error when the expected output doesn't match", function() {
assert.throws(function() {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [
"bar = baz;"
],
invalid: [
{ code: "var foo = bar;", output: "foo = bar", errors: [{ message: "Bad var.", type: "VariableDeclaration"}] }
]
});
}, /Output is incorrect/);
});
it("should throw an error if invalid code specifies wrong type", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression2"}] }
]
});
}, /Error type should be CallExpression2/);
});
it("should throw an error if invalid code specifies wrong line", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "eval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression", line: 5 }] }
]
});
}, /Error line should be 5/);
});
it("should not skip line assertion if line is a falsy value", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "\neval(foo)", errors: [{ message: "eval sucks.", type: "CallExpression", line: 0 }] }
]
});
}, /Error line should be 0/);
});
it("should throw an error if invalid code specifies wrong column", function() {
var wrongColumn = 10,
expectedErrorMessage = "Error column should be 1";
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [ "Eval(foo)" ],
invalid: [ {
code: "eval(foo)",
errors: [ {
message: "eval sucks.",
column: wrongColumn
} ]
} ]
});
}, expectedErrorMessage);
});
it("should not skip column assertion if column is a falsy value", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [ "Eval(foo)" ],
invalid: [ {
code: "var foo; eval(foo)",
errors: [ { message: "eval sucks.", column: 0 } ]
} ]
});
}, /Error column should be 0/);
});
it("should throw an error if invalid code has the wrong number of errors", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "eval(foo)", errors: [
{ message: "eval sucks.", type: "CallExpression" },
{ message: "eval sucks.", type: "CallExpression" }
] }
]
});
}, /Should have 2 errors but had 1/);
});
it("should throw an error if invalid code does not have errors", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "eval(foo)" }
]
});
}, /Did not specify errors for an invalid test of no-eval/);
});
it("should throw an error if invalid code has the wrong explicit number of errors", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
"Eval(foo)"
],
invalid: [
{ code: "eval(foo)", errors: 2 }
]
});
}, /Should have 2 errors but had 1/);
});
// https://github.com/eslint/eslint/issues/4779
it("should throw an error if there's a parsing error and output doesn't match", function() {
assert.throws(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [],
invalid: [
{ code: "eval(`foo`)", output: "eval(`foo`);", errors: [{}] }
]
});
}, /fatal parsing error/i);
});
it("should not throw an error if invalid code has at least an expected empty error object", function() {
assert.doesNotThrow(function() {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [ "Eval(foo)" ],
invalid: [ {
code: "eval(foo)",
errors: [ {} ]
} ]
});
});
});
it("should pass-through the globals config of valid tests to the to rule", function() {
assert.doesNotThrow(function() {
ruleTester.run("no-test-global", require("../../fixtures/testers/rule-tester/no-test-global"), {
valid: [
"var test = 'foo'",
{
code: "var test2 = 'bar'",
globals: { test: true }
},
{
code: "var test2 = 'bar'",
global: { test: true }
}
],
invalid: [ { code: "bar", errors: 1 } ]
});
});
});
it("should pass-through the globals config of invalid tests to the to rule", function() {
assert.doesNotThrow(function() {
ruleTester.run("no-test-global", require("../../fixtures/testers/rule-tester/no-test-global"), {
valid: [ "var test = 'foo'" ],
invalid: [
{
code: "var test = 'foo'; var foo = 'bar'",
errors: 1
},
{
code: "var test = 'foo'",
globals: { foo: true },
errors: [ { message: "Global variable foo should not be used." } ]
},
{
code: "var test = 'foo'",
global: { foo: true },
errors: [ { message: "Global variable foo should not be used." } ]
}
]
});
});
});
it("should pass-through the settings config to rules", function() {
assert.doesNotThrow(function() {
ruleTester.run("no-test-settings", require("../../fixtures/testers/rule-tester/no-test-settings"), {
valid: [
{
code: "var test = 'bar'", settings: { test: 1 }
}
],
invalid: [
{
code: "var test = 'bar'", settings: { "no-test": 22 }, errors: 1
}
]
});
});
});
it("should pass-through the filename to the rule", function() {
(function() {
ruleTester.run("", require("../../fixtures/testers/rule-tester/no-test-filename"), {
valid: [
{
code: "var foo = 'bar'",
filename: "somefile.js"
}
],
invalid: [
{
code: "var foo = 'bar'",
errors: [
{ message: "Filename test was not defined." }
]
}
]
});
}());
});
it("should pass-through the options to the rule", function() {
assert.doesNotThrow(function() {
ruleTester.run("no-invalid-args", require("../../fixtures/testers/rule-tester/no-invalid-args"), {
valid: [
{
code: "var foo = 'bar'",
options: [ false ]
}
],
invalid: [
{
code: "var foo = 'bar'",
options: [ true ],
errors: [ { message: "Invalid args" } ]
}
]
});
});
});
it("should pass-through the parser to the rule", function() {
assert.doesNotThrow(function() {
var spy = sinon.spy(eslint, "verify");
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
{
code: "Eval(foo)"
}
],
invalid: [
{
code: "eval(foo)",
parser: "esprima",
errors: [ {} ]
}
]
});
assert.equal(spy.args[1][1].parser, "esprima");
});
});
it("should prevent invalid options schemas", function() {
assert.throws(function() {
ruleTester.run("no-invalid-schema", require("../../fixtures/testers/rule-tester/no-invalid-schema"), {
valid: [
"var answer = 6 * 7;",
{ code: "var answer = 6 * 7;", options: [] }
],
invalid: [
{ code: "var answer = 6 * 7;", options: ["bar"], errors: [{ message: "Expected nothing." }] }
]
});
}, /Schema for rule .* is invalid/);
});
it("should prevent schema violations in options", function() {
assert.throws(function() {
ruleTester.run("no-schema-violation", require("../../fixtures/testers/rule-tester/no-schema-violation"), {
valid: [
"var answer = 6 * 7;",
{ code: "var answer = 6 * 7;", options: ["foo"] }
],
invalid: [
{ code: "var answer = 6 * 7;", options: ["bar"], errors: [{ message: "Expected foo." }] }
]
});
}, /Value "bar" must be an enum value./);
});
it("should pass-through the tester config to the rule", function() {
ruleTester = new RuleTester({
global: { test: true }
});
assert.doesNotThrow(function() {
ruleTester.run("no-test-global", require("../../fixtures/testers/rule-tester/no-test-global"), {
valid: [
"var test = 'foo'",
"var test2 = test"
],
invalid: [ { code: "bar", errors: 1, global: { foo: true } } ]
});
});
});
it("should correctly set the global configuration", function() {
var config = { global: { test: true } };
RuleTester.setDefaultConfig(config);
assert(
RuleTester.getDefaultConfig().global.test,
"The default config object is incorrect"
);
});
it("should correctly reset the global configuration", function() {
var config = { global: { test: true } };
RuleTester.setDefaultConfig(config);
RuleTester.resetDefaultConfig();
assert.deepEqual(
RuleTester.getDefaultConfig(),
{ rules: {} },
"The default configuration has not reset correctly"
);
});
it("should enforce the global configuration to be an object", function() {
/**
* Set the default config for the rules tester
* @param {object} config configuration object
* @returns {function} Function to be executed
* @private
*/
function setConfig(config) {
return function() {
RuleTester.setDefaultConfig(config);
};
}
assert.throw(setConfig());
assert.throw(setConfig(1));
assert.throw(setConfig(3.14));
assert.throw(setConfig("foo"));
assert.throw(setConfig(null));
assert.throw(setConfig(true));
});
it("should pass-through the global config to the tester then to the to rule", function() {
var config = { global: { test: true } };
RuleTester.setDefaultConfig(config);
ruleTester = new RuleTester();
assert.doesNotThrow(function() {
ruleTester.run("no-test-global", require("../../fixtures/testers/rule-tester/no-test-global"), {
valid: [
"var test = 'foo'",
"var test2 = test"
],
invalid: [ { code: "bar", errors: 1, global: { foo: true } } ]
});
});
});
it("should throw an error if AST was modified", function() {
assert.throws(function() {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/modify-ast"), {
valid: [
"var foo = 0;"
],
invalid: []
});
}, "Rule should not modify AST.");
assert.throws(function() {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/modify-ast"), {
valid: [],
invalid: [
{code: "var bar = 0;", errors: ["error"]}
]
});
}, "Rule should not modify AST.");
});
it("should throw an error if AST was modified (at Program)", function() {
assert.throws(function() {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/modify-ast-at-first"), {
valid: [
"var foo = 0;"
],
invalid: []
});
}, "Rule should not modify AST.");
assert.throws(function() {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/modify-ast-at-first"), {
valid: [],
invalid: [
{code: "var bar = 0;", errors: ["error"]}
]
});
}, "Rule should not modify AST.");
});
it("should throw an error if AST was modified (at Program:exit)", function() {
assert.throws(function() {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/modify-ast-at-last"), {
valid: [
"var foo = 0;"
],
invalid: []
});
}, "Rule should not modify AST.");
assert.throws(function() {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/modify-ast-at-last"), {
valid: [],
invalid: [
{code: "var bar = 0;", errors: ["error"]}
]
});
}, "Rule should not modify AST.");
});
});