1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 | var assert = require('assert');
|
5 | var express = require('express');
|
6 | var req = require('supertest');
|
7 | var through = require('through');
|
8 | var browserify = require('browserify');
|
9 | var middleware = require('../index');
|
10 |
|
11 | function fixture(name) {
|
12 | return path.join(__dirname, 'fixtures', name);
|
13 | }
|
14 |
|
15 | var moreSemiColons = through(function(chunk) {
|
16 | this.queue(chunk.toString('utf8').replace(/;/g, ';;'));
|
17 | });
|
18 |
|
19 | function assertWorks(app, done) {
|
20 | req(app)
|
21 | .get('/bundle.js')
|
22 | .expect(200)
|
23 | .expect('Content-type', 'application/javascript')
|
24 | .end(function(err, res) {
|
25 | if (err) {
|
26 | return done(err);
|
27 | }
|
28 | assert.ok(/sourceMappingURL/.exec(res.text), 'source map missing');
|
29 | assert.ok(/module.exports = 'dep'/.exec(res.text), 'dep module missing');
|
30 | done(null, res.text);
|
31 | });
|
32 | }
|
33 |
|
34 | describe('connect-browserify', function() {
|
35 |
|
36 | it('works (configured with module id)', function(done) {
|
37 | var app = express();
|
38 | app.use('/bundle.js', middleware(fixture('main.js'), {debug: true}));
|
39 | assertWorks(app, done);
|
40 | });
|
41 |
|
42 | it('works (configured with options object)', function(done) {
|
43 | var app = express();
|
44 | app.use('/bundle.js', middleware({entry: fixture('main.js'), debug: true}));
|
45 | assertWorks(app, done);
|
46 | });
|
47 |
|
48 | it('works (configured with browserify instance)', function(done) {
|
49 | var app = express();
|
50 | app.use(
|
51 | '/bundle.js',
|
52 | middleware(browserify(fixture('main.js')), {debug: true}));
|
53 | assertWorks(app, done);
|
54 | });
|
55 |
|
56 | it('works (configured with bundle function)', function(done) {
|
57 | var app = express();
|
58 | app.use(
|
59 | '/bundle.js',
|
60 | middleware({
|
61 | entry: fixture('main.js'),
|
62 | bundle: function(b) {
|
63 | b.external('some-file.js');
|
64 | return b;
|
65 | },
|
66 | debug: true
|
67 | }));
|
68 | assertWorks(app, done);
|
69 | });
|
70 |
|
71 | it('allows post-bundle transforms via pipe', function(done) {
|
72 | var app = express();
|
73 | app.use(
|
74 | '/bundle.js',
|
75 | middleware({
|
76 | entry: fixture('main.js'),
|
77 | debug: true,
|
78 | pipes: function(stream) {
|
79 | return stream.pipe(moreSemiColons);
|
80 | }
|
81 | })
|
82 | );
|
83 | assertWorks(app, function(err, js) {
|
84 | if (err) {
|
85 | return done(err);
|
86 | }
|
87 | assert.ok(/;;/.exec(js), 'post-transform not applied');
|
88 | done();
|
89 | });
|
90 | });
|
91 |
|
92 | it('provides access to watchify instance', function(done) {
|
93 | var app = express();
|
94 | var handler = middleware({
|
95 | entry: fixture('main.js'),
|
96 | watch: true,
|
97 | debug: true
|
98 | });
|
99 | app.use('/bundle.js', handler);
|
100 | handler.watchify.on('time', function(time) {
|
101 | assert.ok(time);
|
102 | assertWorks(app, done);
|
103 | });
|
104 | });
|
105 |
|
106 | it('should expose build errors', function(done) {
|
107 | var app = express();
|
108 | var handler = middleware({
|
109 | entry: fixture('broken.js'),
|
110 | onError: makeRequest
|
111 | });
|
112 | app.use('/bundle.js', handler);
|
113 |
|
114 | app.use(function(err, req, res, next) {
|
115 | res.send(500, err.stack);
|
116 |
|
117 | next = !next;
|
118 | });
|
119 |
|
120 | function makeRequest(capturedError) {
|
121 | req(app)
|
122 | .get('/bundle.js')
|
123 | .expect(500)
|
124 | .end(function(err, res) {
|
125 | if (err) {
|
126 | done(err);
|
127 | }
|
128 | assert.ok(capturedError, 'expected error in onerror');
|
129 | assert.equal(res.text, capturedError.stack);
|
130 | done();
|
131 | });
|
132 | }
|
133 | });
|
134 |
|
135 | });
|
136 |
|