/
index.js
68 lines (54 loc) · 2.59 KB
/
index.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
var reduceArgs = require('../index');
var assert = require("assert");
var fs = require("fs");
function min(code, conf) {
conf = conf || {};
conf.test = conf.test || /^fn$/;
var result = reduceArgs(code, [conf]).code;
return result;
}
function getSource(file) {
return fs.readFileSync(__dirname + file, {encoding:'utf8'});
}
function spaceless(str) {
return str.replace(/\s+/g, '');
}
describe('reduceArgs', function() {
var code = 'fn(true, true, false);';
it('should remove arguments at the given indexes', function() {
assert.equal(min(code, {stripArgs: [1,2]}), 'fn(true);', 'should strip the last two args');
assert.equal(min(code, {stripArgs: [0]}), 'fn(null, true, false);', 'should strip the first arg');
assert.equal(min(code, {stripArgs: []}), code, 'should strip no args');
});
it('should keep arguments at the given indexes', function() {
assert.equal(min(code, {keepArgs: [1,2]}), 'fn(null, true, false);', 'should keep the last two args');
assert.equal(min(code, {keepArgs: [1]}), 'fn(null, true);', 'should only keep the second arg');
assert.equal(min(code, {keepArgs: []}), 'fn();', 'should strip all args');
});
it('should remove the function call', function() {
assert.equal(min(code, {removeCall: true}), ';', 'should remove the call');
});
it('should only hit the matching the functions', function() {
assert.equal(min(code, {removeCall: true, test: /fn/}), ';', 'should remove any call matching /fn/');
assert.equal(min(code, {removeCall: true, test: /^fn$/}), ';', 'should remove the call to /^fn$/');
assert.equal(min(code, {removeCall: true, test: /^console\.log$/}), code, 'shouldn\t remove the call');
});
it('should remove the console function calls and drop the invariant msg.', function() {
var testCode = 'console.log(true); invariant(true, "this message should be stripped");';
var expectCode = 'invariant(true);';
var resultCode = reduceArgs(testCode, [
{ test: /^console./, removeCall: true },
{ test: /^invariant$/, keepArgs: [0] }
]).code;
assert.equal(resultCode, expectCode);
});
it('should match the expected output.', function() {
var testCode = getSource('/full.test.js');
var expectCode = getSource('/full.expect.js');
var resultCode = reduceArgs(testCode, [
{ test: /^console\./, removeCall: true },
{ test: /^invariant$/, keepArgs: [0] }
]).code;
assert.equal(spaceless(resultCode), spaceless(expectCode));
});
});