Skip to content

Commit 338eae2

Browse files
committed
Update: Cleanup add tests & cover more cases
1 parent e20cec2 commit 338eae2

File tree

3 files changed

+195
-87
lines changed

3 files changed

+195
-87
lines changed

test/add.js

Lines changed: 179 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ var fs = require('fs');
44
var File = require('vinyl');
55
var path = require('path');
66
var expect = require('expect');
7+
var convert = require('convert-source-map');
8+
79
var sourcemaps = require('..');
8-
var stream = require('stream');
910

10-
var sourceContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.js')).toString();
11-
var sourceContentCSS = fs.readFileSync(path.join(__dirname, 'assets/test.css')).toString();
11+
var sourceContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.js'));
1212

1313
function makeFile() {
1414
return new File({
@@ -19,198 +19,290 @@ function makeFile() {
1919
});
2020
}
2121

22-
function makeFileCSS() {
23-
return new File({
24-
cwd: __dirname,
25-
base: path.join(__dirname, 'assets'),
26-
path: path.join(__dirname, 'assets', 'test.css'),
27-
contents: new Buffer(sourceContentCSS)
28-
});
22+
function makeSourcemap() {
23+
return {
24+
file: 'all.js',
25+
mappings: 'AAAAA,QAAAC,IAAA,YACAD,QAAAC,IAAA,YCDAD,QAAAC,IAAA,YACAD,QAAAC,IAAA',
26+
names: ['console', 'log'],
27+
sourceRoot: path.join(__dirname, 'assets'),
28+
sources: ['test1.js', 'test2.js'],
29+
sourcesContent: ['console.log("line 1.1");\nconsole.log("line 1.2");\n', 'console.log("line 2.1");\nconsole.log("line 2.2");'],
30+
version: 3
31+
};
2932
}
3033

3134
function makeFileWithInlineSourceMap() {
35+
var inline = convert.fromObject(makeSourcemap()).toComment();
3236
return new File({
3337
cwd: __dirname,
3438
base: path.join(__dirname, 'assets'),
3539
path: path.join(__dirname, 'assets', 'all.js'),
36-
contents: new Buffer('console.log("line 1.1"),console.log("line 1.2"),console.log("line 2.1"),console.log("line 2.2");\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWxsLmpzIiwic291cmNlcyI6WyJ0ZXN0MS5qcyIsInRlc3QyLmpzIl0sIm5hbWVzIjpbImNvbnNvbGUiLCJsb2ciXSwibWFwcGluZ3MiOiJBQUFBQSxRQUFBQyxJQUFBLFlBQ0FELFFBQUFDLElBQUEsWUNEQUQsUUFBQUMsSUFBQSxZQUNBRCxRQUFBQyxJQUFBIiwic291cmNlc0NvbnRlbnQiOlsiY29uc29sZS5sb2coJ2xpbmUgMS4xJyk7XG5jb25zb2xlLmxvZygnbGluZSAxLjInKTtcbiIsImNvbnNvbGUubG9nKCdsaW5lIDIuMScpO1xuY29uc29sZS5sb2coJ2xpbmUgMi4yJyk7Il0sInNvdXJjZVJvb3QiOiIvc291cmNlLyJ9')
40+
contents: new Buffer('console.log("line 1.1"),console.log("line 1.2"),console.log("line 2.1"),console.log("line 2.2");\n' + inline)
3741
});
3842
}
3943

4044
describe('add', function() {
4145

42-
describe('ensures file argument', function() {
43-
44-
it('is not undefined', function(done) {
45-
sourcemaps.add(undefined, function(err) {
46-
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
47-
done();
48-
});
49-
});
50-
51-
it('is not null', function(done) {
52-
sourcemaps.add(null, function(err) {
53-
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
54-
done();
55-
});
46+
it('errors if file argument is undefined', function(done) {
47+
sourcemaps.add(undefined, function(err) {
48+
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
49+
done();
5650
});
51+
});
5752

58-
it('is not a plain object', function(done) {
59-
sourcemaps.add({}, function(err) {
60-
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
61-
done();
62-
});
53+
it('errors if file argument is null', function(done) {
54+
sourcemaps.add(null, function(err) {
55+
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
56+
done();
6357
});
58+
});
6459

65-
// TODO: seems like a bad test
66-
it('is not a stream', function(done) {
67-
sourcemaps.add(new stream.Readable(), function(err) {
68-
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
69-
done();
70-
});
60+
it('errors if file argument is a plain object', function(done) {
61+
sourcemaps.add({}, function(err) {
62+
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Not a vinyl file').toExist();
63+
done();
7164
});
65+
});
7266

73-
it('is a vinyl object', function(done) {
74-
var file = makeFile();
75-
sourcemaps.add(file, function(err) {
76-
expect(err).toNotExist();
77-
done();
78-
});
67+
it('does not error if file argument is a vinyl object', function(done) {
68+
var file = makeFile();
69+
sourcemaps.add(file, function(err) {
70+
expect(err).toNotExist();
71+
done();
7972
});
8073
});
8174

82-
it('should add an empty sourceMap', function(done) {
75+
it('adds an empty sourceMap if none are found', function(done) {
8376
sourcemaps.add(makeFile(), function(err, data) {
84-
expect(File.isVinyl(data)).toExist();
8577
expect(data.sourceMap).toExist();
86-
expect(String(data.sourceMap.version)).toBe('3');
87-
expect(data.sourceMap.sources[0]).toBe('helloworld.js');
88-
expect(data.sourceMap.sourcesContent[0]).toBe(sourceContent);
78+
expect(data.sourceMap.version).toEqual(3);
79+
expect(data.sourceMap.sources[0]).toEqual('helloworld.js');
80+
expect(data.sourceMap.sourcesContent[0]).toEqual(sourceContent);
8981
expect(data.sourceMap.names).toEqual([]);
90-
expect(data.sourceMap.mappings).toBe('');
82+
expect(data.sourceMap.mappings).toEqual('');
9183
done(err);
9284
});
9385
});
9486

95-
it('should import an existing inline source map', function(done) {
87+
it('imports an existing inline sourcemap', function(done) {
9688
sourcemaps.add(makeFileWithInlineSourceMap(), function(err, data) {
97-
expect(data).toExist();
98-
expect(data instanceof File).toExist();
9989
expect(data.sourceMap).toExist();
100-
expect(String(data.sourceMap.version)).toBe('3');
90+
expect(data.sourceMap.version).toEqual(3);
10191
expect(data.sourceMap.sources).toEqual(['test1.js', 'test2.js']);
102-
expect(data.sourceMap.sourcesContent).toEqual(['console.log(\'line 1.1\');\nconsole.log(\'line 1.2\');\n', 'console.log(\'line 2.1\');\nconsole.log(\'line 2.2\');']);
103-
expect(data.sourceMap.mappings).toBe('AAAAA,QAAAC,IAAA,YACAD,QAAAC,IAAA,YCDAD,QAAAC,IAAA,YACAD,QAAAC,IAAA');
92+
expect(data.sourceMap.sourcesContent).toEqual(['console.log("line 1.1");\nconsole.log("line 1.2");\n', 'console.log("line 2.1");\nconsole.log("line 2.2");']);
93+
expect(data.sourceMap.mappings).toEqual('AAAAA,QAAAC,IAAA,YACAD,QAAAC,IAAA,YCDAD,QAAAC,IAAA,YACAD,QAAAC,IAAA');
10494
done(err);
10595
});
10696
});
10797

108-
it('should remove inline source', function(done) {
98+
it('removes an imported inline sourcemap', function(done) {
10999
sourcemaps.add(makeFileWithInlineSourceMap(), function(err, data) {
110-
expect(/sourceMappingURL/.test(data.contents.toString())).toNotExist();
100+
expect(/sourceMappingURL/.test(data.contents.toString())).toEqual(false);
111101
done(err);
112102
});
113103
});
114104

115-
it('should load external source map file reference in comment with \/\/# syntax', function(done) {
105+
it('loads external sourcemap file from \/\/# comment', function(done) {
116106
var file = makeFile();
117-
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
107+
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
118108
sourcemaps.add(file, function(err, data) {
119109
expect(data.sourceMap).toExist();
120-
expect(String(data.sourceMap.version)).toBe('3');
110+
expect(data.sourceMap.version).toEqual(3);
121111
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
122112
expect(data.sourceMap.sourcesContent).toEqual(['source content from source map']);
123-
expect(data.sourceMap.mappings).toBe('');
113+
expect(data.sourceMap.mappings).toEqual('');
124114
done(err);
125115
});
126116
});
127117

128-
it('should remove source map comment with the \/\/# syntax', function(done) {
118+
it('removes an imported sourcemap file \/\/# comment', function(done) {
129119
var file = makeFile();
130120
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
131121
sourcemaps.add(file, function(err, data) {
132-
expect(/sourceMappingURL/.test(data.contents.toString())).toNotExist();
122+
expect(/sourceMappingURL/.test(data.contents.toString())).toEqual(false);
123+
done(err);
124+
});
125+
});
126+
127+
it('loads external sourcemap file from \/\/@ comment', function(done) {
128+
var file = makeFile();
129+
file.contents = new Buffer(sourceContent + '\n//@ sourceMappingURL=helloworld2.js.map');
130+
sourcemaps.add(file, function(err, data) {
131+
expect(data.sourceMap).toExist();
132+
expect(data.sourceMap.version).toEqual(3);
133+
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
134+
expect(data.sourceMap.sourcesContent).toEqual(['source content from source map']);
135+
expect(data.sourceMap.mappings).toEqual('');
136+
done(err);
137+
});
138+
});
139+
140+
it('removes an imported sourcemap file \/\/@ comment', function(done) {
141+
var file = makeFile();
142+
file.contents = new Buffer(sourceContent + '\n//@ sourceMappingURL=helloworld2.js.map');
143+
sourcemaps.add(file, function(err, data) {
144+
expect(/sourceMappingURL/.test(data.contents.toString())).toEqual(false);
133145
done(err);
134146
});
135147
});
136148

137-
it('should load external source map if no source mapping comment', function (done) {
149+
it('loads external sourcemap file from \/*# *\/ comment', function(done) {
150+
var file = makeFile();
151+
file.contents = new Buffer(sourceContent + '\n/*# sourceMappingURL=helloworld2.js.map */');
152+
sourcemaps.add(file, function(err, data) {
153+
expect(data.sourceMap).toExist();
154+
expect(data.sourceMap.version).toEqual(3);
155+
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
156+
expect(data.sourceMap.sourcesContent).toEqual(['source content from source map']);
157+
expect(data.sourceMap.mappings).toEqual('');
158+
done(err);
159+
});
160+
});
161+
162+
it('removes an imported sourcemap file \/*# *\/ comment', function(done) {
163+
var file = makeFile();
164+
file.contents = new Buffer(sourceContent + '\n/*# sourceMappingURL=helloworld2.js.map */');
165+
sourcemaps.add(file, function(err, data) {
166+
expect(/sourceMappingURL/.test(data.contents.toString())).toEqual(false);
167+
done(err);
168+
});
169+
});
170+
171+
it('loads external sourcemap file from \/*@ *\/ comment', function(done) {
172+
var file = makeFile();
173+
file.contents = new Buffer(sourceContent + '\n/*@ sourceMappingURL=helloworld2.js.map */');
174+
sourcemaps.add(file, function(err, data) {
175+
expect(data.sourceMap).toExist();
176+
expect(data.sourceMap.version).toEqual(3);
177+
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
178+
expect(data.sourceMap.sourcesContent).toEqual(['source content from source map']);
179+
expect(data.sourceMap.mappings).toEqual('');
180+
done(err);
181+
});
182+
});
183+
184+
it('removes an imported sourcemap file \/*@ *\/ comment', function(done) {
185+
var file = makeFile();
186+
file.contents = new Buffer(sourceContent + '\n/*@ sourceMappingURL=helloworld2.js.map */');
187+
sourcemaps.add(file, function(err, data) {
188+
expect(/sourceMappingURL/.test(data.contents.toString())).toEqual(false);
189+
done(err);
190+
});
191+
});
192+
193+
it('loads external sourcemap by filename if no source mapping comment', function (done) {
138194
var file = makeFile();
139195
file.path = file.path.replace('helloworld.js', 'helloworld2.js');
140196
sourcemaps.add(file, function(err, data) {
141197
expect(data.sourceMap).toExist();
142-
expect(String(data.sourceMap.version)).toBe('3');
198+
expect(data.sourceMap.version).toEqual(3);
143199
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
144200
expect(data.sourceMap.sourcesContent).toEqual(['source content from source map']);
145-
expect(data.sourceMap.mappings).toBe('');
201+
expect(data.sourceMap.mappings).toEqual('');
146202
done(err);
147203
});
148204
});
149205

150-
it('should load external source map and add sourceContent if missing', function(done) {
206+
it('loads sourcesContent if missing', function(done) {
151207
var file = makeFile();
152208
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld3.js.map');
153209
sourcemaps.add(file, function(err, data) {
154210
expect(data.sourceMap).toExist();
155-
expect(String(data.sourceMap.version)).toBe('3');
156-
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'test1.js']);
157211
expect(data.sourceMap.sourcesContent).toEqual([file.contents.toString(), 'test1\n']);
158-
expect(data.sourceMap.mappings).toBe('');
159212
done(err);
160213
});
161214
});
162215

163-
it('should not throw when source file for sourceContent not found', function(done) {
216+
it('does not error when source file for sourcesContent not found', function(done) {
164217
var file = makeFile();
165218
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');
166219
sourcemaps.add(file, function(err, data) {
220+
expect(err).toNotExist();
167221
expect(data.sourceMap).toExist();
168-
expect(String(data.sourceMap.version)).toBe('3');
169222
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'missingfile']);
170223
expect(data.sourceMap.sourcesContent).toEqual([file.contents.toString(), null]);
171-
expect(data.sourceMap.mappings).toBe('');
172224
done(err);
173225
});
174226
});
175227

176-
it('should use unix style paths in sourcemap', function(done) {
228+
it('uses unix style paths in sourcemap', function(done) {
177229
var file = makeFile();
178230
file.base = file.cwd;
179231
sourcemaps.add(file, function(err, data) {
180-
expect(data.sourceMap.file).toBe('assets/helloworld.js');
232+
expect(data.sourceMap).toExist();
233+
expect(data.sourceMap.file).toEqual('assets/helloworld.js');
181234
expect(data.sourceMap.sources).toEqual(['assets/helloworld.js']);
182235
done(err);
183236
});
184237
});
185238

186-
it('should use sourceRoot when resolving path to sources', function(done) {
239+
it('normalizes Windows paths in sources to unix paths', function(done) {
187240
var file = makeFile();
188-
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld5.js.map');
241+
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld8.js.map');
189242
sourcemaps.add(file, function(err, data) {
190-
expect(data.sourceMap).toExist([]);
191-
expect(String(data.sourceMap.version)).toBe('3');
243+
expect(data.sourceMap).toExist();
192244
expect(data.sourceMap.sources).toEqual(['../helloworld.js', '../test1.js']);
245+
done(err);
246+
});
247+
});
248+
249+
it('sets file.relative as file property in sourcemap', function(done) {
250+
var file = makeFile();
251+
file.stem = 'brandnew';
252+
sourcemaps.add(file, function(err, data) {
253+
expect(data.sourceMap).toExist();
254+
expect(data.sourceMap.file).toEqual('brandnew.js');
255+
done(err);
256+
});
257+
});
258+
259+
it('normalizes Windows paths in file.relative before using in sourcemap', function(done) {
260+
var file = makeFile();
261+
file.stem = 'assets\\\\brandnew';
262+
sourcemaps.add(file, function(err, data) {
263+
expect(data.sourceMap).toExist();
264+
expect(data.sourceMap.file).toEqual('assets/brandnew.js');
265+
done(err);
266+
});
267+
});
268+
269+
it('uses relative sourceRoot to resolve sources', function(done) {
270+
var file = makeFile();
271+
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld5.js.map');
272+
sourcemaps.add(file, function(err, data) {
273+
expect(data.sourceMap).toExist();
274+
expect(data.sourceMap.sourceRoot).toEqual('test');
193275
expect(data.sourceMap.sourcesContent).toEqual([file.contents.toString(), 'test1\n']);
194-
expect(data.sourceMap.mappings).toBe('');
195-
expect(data.sourceMap.sourceRoot).toBe('test');
196276
done(err);
197277
});
198278
});
199279

200-
it('should not load source conent if the path is a url', function(done) {
280+
it('uses absolute sourceRoot to resolve sources', function(done) {
281+
var file = makeFile();
282+
var map = convert.fromObject(makeSourcemap());
283+
delete map.sourcemap.sourcesContent;
284+
var inline = map.toComment();
285+
file.contents = new Buffer(sourceContent + '\n' + inline);
286+
sourcemaps.add(file, function(err, data) {
287+
expect(data.sourceMap).toExist();
288+
expect(data.sourceMap.sourceRoot).toEqual(path.join(__dirname, 'assets'));
289+
expect(data.sourceMap.sourcesContent).toEqual(['test1\n', 'test2\n']);
290+
done(err);
291+
});
292+
});
293+
294+
it('does not load sourcesContent when sourceRoot is a url', function(done) {
201295
var file = makeFile();
202296
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld6.js.map');
203297
sourcemaps.add(file, function(err, data) {
204298
expect(data.sourceMap).toExist();
205-
expect(String(data.sourceMap.version)).toBe('3');
206-
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'http://example2.com/test1.js']);
299+
expect(data.sourceMap.sourceRoot).toEqual('http://example.com/');
207300
expect(data.sourceMap.sourcesContent).toEqual([null, null]);
208-
expect(data.sourceMap.mappings).toBe('');
209301
done(err);
210302
});
211303
});
212304

213-
it('should pass through whe file already has a source map', function(done) {
305+
it('passes file through when it already has a sourcemap', function(done) {
214306
var sourceMap = {
215307
version: 3,
216308
names: [],
@@ -223,9 +315,9 @@ describe('add', function() {
223315
file.sourceMap = sourceMap;
224316
sourcemaps.add(file, function(err, data) {
225317
expect(data).toExist();
226-
expect(data instanceof File).toExist();
318+
expect(File.isVinyl(data)).toEqual(true);
227319
expect(data.sourceMap).toBe(sourceMap);
228-
expect(data).toEqual(file);
320+
expect(data).toBe(file);
229321
done(err);
230322
});
231323
});

0 commit comments

Comments
 (0)