-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
test.js
153 lines (127 loc) · 3.56 KB
/
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
'use strict';
const path = require('path');
const assert = require('assert');
const Vinyl = require('vinyl');
const sourceMaps = require('gulp-sourcemaps');
const babel = require('.');
it('should transpile with Babel', cb => {
const stream = babel({
plugins: ['@babel/transform-block-scoping']
});
stream.on('data', file => {
assert(/var foo/.test(file.contents.toString()), file.contents.toString());
assert.strictEqual(file.relative, 'fixture.js');
});
stream.on('end', cb);
stream.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fixture.jsx'),
contents: Buffer.from('let foo;')
}));
stream.end();
});
it('should generate source maps', cb => {
const init = sourceMaps.init();
const write = sourceMaps.write();
init
.pipe(babel({
plugins: ['@babel/transform-arrow-functions']
}))
.pipe(write);
write.on('data', file => {
assert.deepStrictEqual(file.sourceMap.sources, ['fixture.es2015']);
assert.strictEqual(file.sourceMap.file, 'fixture.js');
const contents = file.contents.toString();
assert(/function/.test(contents));
assert(/sourceMappingURL/.test(contents));
cb();
});
init.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fixture.es2015'),
contents: Buffer.from('[].map(v => v + 1)'),
sourceMap: ''
}));
init.end();
});
it('should generate source maps for file in nested folder', cb => {
const init = sourceMaps.init();
const write = sourceMaps.write();
init
.pipe(babel({
plugins: ['@babel/transform-arrow-functions']
}))
.pipe(write);
write.on('data', file => {
assert.deepStrictEqual(file.sourceMap.sources, ['nested/fixture.es2015']);
assert.strictEqual(file.sourceMap.file, 'nested/fixture.js');
const contents = file.contents.toString();
assert(/function/.test(contents));
assert(/sourceMappingURL/.test(contents));
cb();
});
init.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/nested/fixture.es2015'),
contents: Buffer.from('[].map(v => v + 1)'),
sourceMap: ''
}));
init.end();
});
it('should pass the result of transform().metadata in file.babel', cb => {
const stream = babel({
plugins: [{
post(file) {
file.metadata.test = 'metadata';
}
}]
});
stream.on('data', file => {
assert.deepStrictEqual(file.babel, {test: 'metadata'});
});
stream.on('end', cb);
stream.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fixture.js'),
contents: Buffer.from('class MyClass {};')
}));
stream.end();
});
it('should not rename ignored files', cb => {
const stream = babel({
ignore: [/fixture/]
});
const inputFile = {
cwd: __dirname
};
inputFile.base = path.join(inputFile.cwd, 'fixture');
inputFile.basename = 'fixture.jsx';
inputFile.path = path.join(inputFile.base, inputFile.basename);
inputFile.contents = Buffer.from(';');
stream
.on('data', file => {
assert.strictEqual(file.relative, inputFile.basename);
})
.on('end', cb)
.end(new Vinyl(inputFile));
});
it('should not rename files without an extension', cb => {
const stream = babel();
const inputFile = {
cwd: __dirname
};
inputFile.base = path.join(inputFile.cwd, 'bin');
inputFile.basename = 'app';
inputFile.path = path.join(inputFile.base, inputFile.basename);
inputFile.contents = Buffer.from(';');
stream
.on('data', file => {
assert.strictEqual(file.relative, inputFile.basename);
})
.on('end', cb)
.end(new Vinyl(inputFile));
});