UNPKG

2.3 kBJavaScriptView Raw
1var parser = require('../');
2var test = require('tap').test;
3var path = require('path');
4var fs = require('fs');
5
6var files = {
7 foo: path.join(__dirname, '/files/foo.js'),
8 bar: path.join(__dirname, '/files/bar.js')
9};
10
11test('uses persistent cache', function (t) {
12 t.plan(1);
13 var p = parser({
14 persistentCache: function (file, id, pkg, fallback, cb) {
15 if (file === files.bar) {
16 return fallback(null, cb)
17 }
18 cb(null, {
19 source: 'file at ' + file + '@' + id,
20 package: pkg,
21 deps: { './bar': files.bar }
22 })
23 }
24 });
25 p.end({ id: 'foo', file: files.foo, entry: false });
26
27 var rows = [];
28 p.on('data', function (row) { rows.push(row) });
29 p.on('end', function () {
30 t.same(rows.sort(cmp), [
31 {
32 id: files.bar,
33 file: files.bar,
34 source: fs.readFileSync(files.bar, 'utf8'),
35 deps: {}
36 },
37 {
38 id: 'foo',
39 file: files.foo,
40 source: 'file at ' + files.foo + '@' + files.foo,
41 deps: { './bar': files.bar }
42 }
43 ].sort(cmp));
44 });
45});
46
47test('passes persistent cache error through', function (t) {
48 t.plan(1);
49 var p = parser({
50 persistentCache: function (file, id, pkg, fallback, cb) {
51 cb(new Error('foo'))
52 }
53 });
54 p.end({ id: 'foo', file: files.foo, entry: false });
55 p.on('error', function (err) { t.equals(err.message, 'foo') });
56});
57
58test('allow passing of the raw source as string', function (t) {
59 t.plan(1);
60 var p = parser({
61 persistentCache: function (file, id, pkg, fallback, cb) {
62 fallback(fs.readFileSync(files.bar, 'utf8'), cb)
63 }
64 });
65 p.end({ id: 'foo', file: files.foo, entry: false });
66
67 var rows = [];
68 p.on('data', function (row) { rows.push(row) });
69 p.on('end', function () {
70 t.same(rows.sort(cmp), [
71 {
72 id: 'foo',
73 file: files.foo,
74 source: fs.readFileSync(files.bar, 'utf8'),
75 deps: {}
76 }
77 ].sort(cmp));
78 });
79});
80
81function cmp (a, b) { return a.id < b.id ? -1 : 1 }