UNPKG

8.33 kBJavaScriptView Raw
1const fs = require('fs')
2const bulbo = require('../src/')
3
4const { expect } = require('chai')
5const rimraf = require('rimraf')
6const request = require('superagent')
7const vinylServe = require('vinyl-serve')
8const through2 = require('through2')
9const browserify = require('browserify')
10
11const SERVER_LAUNCH_WAIT = 800
12const BUILD_WAIT = 400
13const WATCH_BUILD_WAIT = 100
14const WATCH_BUILD_WAIT_CHANGED = 300
15
16describe('bulbo', () => {
17 beforeEach(() => {
18 bulbo.clear()
19 bulbo.setLogger(require('../src/util/logger')('bulbo'))
20 })
21
22 describe('asset', () => {
23 it('registers the asset', () => {
24 bulbo.asset('test/fixture/**/*.js')
25 expect(bulbo.isEmpty()).to.be.false
26 })
27
28 it('returns the asset\'s modifier', () => {
29 expect(bulbo.asset('test/fixture/**/*.js')).to.be.an('object')
30 expect(bulbo.asset('test/fixture/**/*.js').watch).to.be.a('function')
31 expect(bulbo.asset('test/fixture/**/*.js').watchOptions).to.be.a('function')
32 expect(bulbo.asset('test/fixture/**/*.js').base).to.be.a('function')
33 })
34
35 describe('.pipe', () => {
36 it('sets the transform to the asset', done => {
37 bulbo
38 .asset('test/fixture/js/{foo,bar}.js')
39 .base('test/fixture')
40 .pipe(through2.obj(function (file, enc, cb) {
41 file.contents = browserify(file.path).bundle()
42
43 cb(null, file)
44 }))
45
46 bulbo.build().then(() => {
47 const contents = fs.readFileSync('build/js/foo.js').toString()
48
49 expect(contents).to.contain('This is foo.js')
50 expect(contents).to.contain('This is bar.js')
51
52 rimraf('build', done)
53 }).catch(done)
54 })
55 })
56 })
57
58 describe('build', () => {
59 it('builds the assets and put them in build/ dir', done => {
60 bulbo.asset('test/fixture/**/*.js')
61
62 bulbo.build().then(() => {
63 expect(fs.readFileSync('build/js/foo.js').toString()).to.have.length.above(1)
64
65 rimraf('build', done)
66 })
67 })
68
69 it('does not stop at highWaterMark(=16) files', done => {
70 bulbo.asset('test/fixture/**/*.js')
71
72 bulbo.build().then(() => {
73 expect(fs.readFileSync('build/js/0.js').toString()).to.have.length.above(1)
74 expect(fs.readFileSync('build/js/1.js').toString()).to.have.length.above(1)
75 expect(fs.readFileSync('build/js/2.js').toString()).to.have.length.above(1)
76 expect(fs.readFileSync('build/js/3.js').toString()).to.have.length.above(1)
77 expect(fs.readFileSync('build/js/4.js').toString()).to.have.length.above(1)
78 expect(fs.readFileSync('build/js/5.js').toString()).to.have.length.above(1)
79 expect(fs.readFileSync('build/js/6.js').toString()).to.have.length.above(1)
80 expect(fs.readFileSync('build/js/7.js').toString()).to.have.length.above(1)
81 expect(fs.readFileSync('build/js/8.js').toString()).to.have.length.above(1)
82 expect(fs.readFileSync('build/js/9.js').toString()).to.have.length.above(1)
83 expect(fs.readFileSync('build/js/10.js').toString()).to.have.length.above(1)
84 expect(fs.readFileSync('build/js/11.js').toString()).to.have.length.above(1)
85 expect(fs.readFileSync('build/js/12.js').toString()).to.have.length.above(1)
86 expect(fs.readFileSync('build/js/13.js').toString()).to.have.length.above(1)
87 expect(fs.readFileSync('build/js/14.js').toString()).to.have.length.above(1)
88 expect(fs.readFileSync('build/js/15.js').toString()).to.have.length.above(1)
89 expect(fs.readFileSync('build/js/16.js').toString()).to.have.length.above(1)
90
91 rimraf('build', done)
92 })
93 })
94
95 it('does not throw when assets are empty', done => {
96 bulbo.build()
97
98 setTimeout(done, BUILD_WAIT)
99 })
100 })
101
102 describe('watchAndBuild', () => {
103 it('builds the assets', done => {
104 bulbo.asset('test/fixture/js/0.js').base('test/fixture')
105
106 bulbo.watchAndBuild()
107
108 setTimeout(() => {
109 expect(fs.readFileSync('build/js/0.js').toString()).to.equal("console.log('hello')\n")
110
111 bulbo.unwatch()
112 rimraf('build', done)
113 }, WATCH_BUILD_WAIT)
114 })
115
116 it('watches the assets', done => {
117 bulbo.asset('test/fixture/js/0.js').base('test/fixture')
118
119 bulbo.watchAndBuild()
120
121 setTimeout(() => {
122 expect(fs.readFileSync('build/js/0.js').toString()).to.equal("console.log('hello')\n")
123
124 fs.writeFileSync('test/fixture/js/0.js', "console.log('spam')\n")
125
126 setTimeout(() => {
127 expect(fs.readFileSync('build/js/0.js').toString()).to.equal("console.log('spam')\n")
128
129 fs.writeFileSync('test/fixture/js/0.js', "console.log('hello')\n")
130
131 bulbo.unwatch()
132 rimraf('build', done)
133 }, WATCH_BUILD_WAIT_CHANGED)
134 }, WATCH_BUILD_WAIT)
135 })
136 })
137
138 describe('serve', () => {
139 it('serves the assets at port 7100', done => {
140 bulbo.asset('test/fixture/**/*.js')
141
142 bulbo.serve().then(() => {
143 setTimeout(() => {
144 request.get('localhost:7100/js/foo.js').buffer().end((err, res) => {
145 if (err) { done(err) }
146
147 expect(res.text).to.contain('This is foo.js')
148 bulbo.unwatch()
149
150 vinylServe.stop(7100).then(() => {
151 done()
152 })
153 })
154 }, SERVER_LAUNCH_WAIT)
155 })
156 })
157
158 it('does not throw when the assets are empty', done => {
159 bulbo.serve()
160
161 setTimeout(() => {
162 bulbo.unwatch()
163
164 vinylServe.stop(7100).then(() => {
165 done()
166 })
167 }, SERVER_LAUNCH_WAIT)
168 })
169
170 it('serves the index.html when the directory is requested', done => {
171 bulbo.port(7101)
172
173 bulbo.asset('test/fixture/**/*.*')
174
175 bulbo.serve().then(() => {
176 setTimeout(() => {
177 request.get('localhost:7101/js/').buffer().end((err, res) => {
178 if (err) { done(err) }
179
180 expect(res.text).to.contain('This is js/index.html')
181
182 bulbo.unwatch()
183 vinylServe.stop(7101).then(() => {
184 done()
185 })
186 })
187 }, SERVER_LAUNCH_WAIT)
188 })
189 })
190 })
191
192 describe('port', () => {
193 it('sets the port number', done => {
194 bulbo.port(8500)
195
196 bulbo.serve().then(() => {
197 request.get('localhost:8500/__bulbo__').end((err, res) => {
198 vinylServe.stop(8500)
199
200 bulbo.unwatch()
201 done(err)
202 })
203 })
204 })
205 })
206
207 describe('dest', () => {
208 it('sets the build destination', done => {
209 bulbo.dest('build/dist')
210
211 bulbo.asset('test/fixture/**/*.js')
212
213 bulbo.build().then(() => {
214 expect(fs.readFileSync('build/dist/js/foo.js').toString()).to.have.length.above(1)
215
216 rimraf('build', done)
217 })
218 })
219 })
220
221 describe('debugPageTitle', () => {
222 it('sets the debug page\'s title', done => {
223 bulbo.asset('test/fixture/**/*.js')
224 bulbo.debugPageTitle('FooBarBaz')
225 bulbo.port(7111)
226
227 bulbo.serve().then(() => {
228 request.get('localhost:7111/__bulbo__').buffer().end((err, res) => {
229 if (err) { done(err) }
230
231 expect(res.text).to.contain('<title>FooBarBaz</title>')
232
233 bulbo.unwatch()
234 vinylServe.stop(7111)
235
236 done()
237 })
238 })
239 })
240 })
241
242 describe('debugPagePath', () => {
243 it('sets the debug page path', done => {
244 bulbo.asset('test/fixture/**/*.js')
245 bulbo.debugPagePath('__foobarbaz__')
246 bulbo.port(8113)
247
248 bulbo.serve().then(() => {
249 request.get('localhost:8113/__foobarbaz__').buffer().end((err, res) => {
250 if (err) { done(err) }
251
252 expect(res.text).to.contain('<html>')
253
254 bulbo.unwatch()
255 vinylServe.stop(8113)
256
257 done()
258 })
259 })
260 })
261 })
262
263 describe('base', () => {
264 it('sets the default base path of the assets', done => {
265 bulbo.asset('test/fixture/js/foo.js')
266 bulbo.base('test')
267 bulbo.port(8114)
268
269 bulbo.serve().then(() => {
270 setTimeout(() => {
271 request.get('localhost:8114/fixture/js/foo.js').buffer().end((err, res) => {
272 if (err) { done(err) }
273
274 expect(res.text).to.contain('This is foo.js')
275
276 bulbo.unwatch()
277 vinylServe.stop(8114)
278
279 bulbo.base(null)
280
281 done()
282 })
283 }, SERVER_LAUNCH_WAIT)
284 })
285 })
286 })
287})