/
gulp-babili-test.js
167 lines (145 loc) · 3.99 KB
/
gulp-babili-test.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
jest.autoMockOff();
const gutil = require("gulp-util");
const babelCore = require("babel-core");
const babiliPreset = require("babel-preset-babili");
const unpad = require("../../../utils/unpad");
const gulpBabili = require("../src/index");
describe("gulp-babili", () => {
it("should work with a good default", () => {
return new Promise((resolve, reject) => {
const stream = gulpBabili();
const source = unpad(`
function foo() {
const a = 10;
return a;
}
`);
const expected = "function foo(){return 10}";
stream.on("data", function (file) {
expect(file.contents.toString()).toBe(expected);
resolve();
});
stream.on("error", reject);
stream.write(new gutil.File({
path: "defaults.js",
contents: new Buffer(source),
}));
});
});
it("should take options and pass them to babili", () => {
return new Promise((resolve, reject) => {
const stream = gulpBabili({
mangle: {
blacklist: {
bar: true
}
}
});
const source = unpad(`
function foo(bar, baz) {
return bar + baz;
}
`);
const expected = "function foo(bar,a){return bar+a}";
stream.on("data", function (file) {
expect(file.contents.toString()).toBe(expected);
resolve();
});
stream.on("error", reject);
stream.write(new gutil.File({
path: "options.js",
contents: new Buffer(source)
}));
});
});
it("should take custom babel and babili", () => {
return new Promise((resolve, reject) => {
const babel = Object.assign({}, babelCore);
let usedTransform = false;
Object.defineProperty(babel, "transform", {
get() {
usedTransform = true;
return babelCore.transform;
}
});
let usedPreset = false;
const babili = function (...args) {
usedPreset = true;
return babiliPreset(...args);
};
const stream = gulpBabili({}, {
babel,
babili,
});
stream.on("data", function () {
expect(usedTransform).toBe(true);
expect(usedPreset).toBe(true);
resolve();
});
stream.on("error", reject);
stream.write(new gutil.File({
path: "custom-transformers.js",
contents: new Buffer("foo()")
}));
});
});
describe("comments", () => {
const source = unpad(`
/**
* @license
* This is a test
*/
foo();
// this is another comment
bar();
/* YAC - yet another comment */
baz();
`);
let file;
beforeEach(() => {
file = new gutil.File({
path: "comments.js",
contents: new Buffer(source)
});
});
xit("should remove comments by default except license and preserve", () => {
return new Promise((resolve, reject) => {
const stream = gulpBabili();
stream.on("data", function (file) {
expect(file.contents.toString()).toMatchSnapshot();
resolve();
});
stream.on("error", reject);
stream.write(file);
});
});
it("should remove all comments when false", () => {
return new Promise((resolve, reject) => {
const stream = gulpBabili({
comments: false
});
stream.on("data", () => {
expect(file.contents.toString()).toMatchSnapshot();
resolve();
});
stream.on("error", reject);
stream.write(file);
});
});
xit("should take a custom function", () => {
return new Promise((resolve, reject) => {
const stream = gulpBabili({
comments(contents) {
return contents.indexOf("YAC") !== -1;
}
});
stream.on("data", () => {
expect(file.contents.toString()).toMatchSnapshot();
resolve();
});
stream.on("error", reject);
stream.write(file);
});
});
});
});