UNPKG

5.17 kBJavaScriptView Raw
1'use strict'
2
3/* eslint-env mocha */
4const expect = require('expect.js')
5const http = require('http')
6const path = require('path')
7const { spawn } = require('child_process')
8
9const cmd = path.join(__dirname, '../bin/porter-serve.js')
10const componentRoot = path.join(__dirname, '../../porter-component')
11const appRoot = path.join(__dirname, '../../porter-app')
12
13describe('porter-serve --port', function() {
14 it('should be able to change port with --port', async function() {
15 const proc = spawn(cmd, ['--port', 9527], { cwd: componentRoot, stdio: ['pipe', 'pipe', process.stderr] })
16 await new Promise(resolve => {
17 proc.stdout.on('data', chunk => {
18 if (chunk.includes('Server started')) resolve()
19 })
20 })
21 const res = await new Promise(resolve => http.get('http://localhost:9527/loader.js', resolve))
22 expect(res.statusCode).to.eql(200)
23 expect(res.headers['content-type']).to.contain('application/javascript')
24 await new Promise(resolve => {
25 proc.on('exit', resolve)
26 proc.kill()
27 })
28 })
29})
30
31describe('porter-serve component', function() {
32 let proc
33
34 before(async function() {
35 proc = spawn(cmd, { cwd: componentRoot, stdio: ['pipe', 'pipe', process.stderr] })
36 await new Promise(resolve => {
37 proc.stdout.on('data', chunk => {
38 if (chunk.includes('Server started')) resolve()
39 })
40 // porter-serve runs in daemon, cannot wait for it to exit/close.
41 })
42 })
43
44 after(async function() {
45 await new Promise(resolve => {
46 proc.on('exit', resolve)
47 proc.kill()
48 })
49 })
50
51 it('should serve loader', async function() {
52 const res = await new Promise(resolve => http.get('http://localhost:5000/loader.js', resolve))
53 expect(res.statusCode).to.eql(200)
54 expect(res.headers['content-type']).to.contain('application/javascript')
55 })
56
57 it('should serve test runner', async function() {
58 const res = await new Promise(resolve => http.get('http://localhost:5000/runner.html', resolve))
59 expect(res.statusCode).to.eql(200)
60 expect(res.headers['content-type']).to.contain('text/html')
61 })
62
63 // require('mocha') does not work yet.
64 it('should serve mocha', async function() {
65 const res = await new Promise(resolve => http.get('http://localhost:5000/node_modules/mocha/mocha.js', resolve))
66 expect(res.statusCode).to.eql(200)
67 expect(res.headers['content-type']).to.contain('application/javascript')
68 })
69
70 it('should be able to access component files', async function() {
71 const pkg = require(`${componentRoot}/package.json`)
72 const res = await new Promise(resolve => http.get(`http://localhost:5000/${pkg.name}/${pkg.version}/index.js`, resolve))
73 expect(res.statusCode).to.eql(200)
74 expect(res.headers['content-type']).to.contain('application/javascript')
75 })
76})
77
78describe('porter-serve component --headless', function() {
79 it('should be able to run component tests headlessly', async function() {
80 const proc = spawn(cmd, ['--headless'], { stdio: 'inherit', cwd: componentRoot })
81 await new Promise((resolve, reject) => {
82 proc.on('exit', code => {
83 if (code > 0) {
84 reject(new Error(`${cmd} existed with non-zero code: ${code}`))
85 } else {
86 resolve()
87 }
88 })
89 })
90 })
91})
92
93describe('porter-serve web application', function() {
94 let proc
95
96 before(async function() {
97 proc = spawn(cmd, [
98 '--paths', 'components',
99 '--paths', 'browser_modules',
100 ], {
101 cwd: appRoot,
102 stdio: ['pipe', 'pipe', process.stderr]
103 })
104 await new Promise(resolve => {
105 proc.stdout.on('data', chunk => {
106 console.log('' + chunk)
107 if (chunk.includes('Server started')) resolve()
108 })
109 })
110 })
111
112 after(async function() {
113 await new Promise(resolve => {
114 proc.on('exit', resolve)
115 proc.kill()
116 })
117 })
118
119 it('should be able to serve as a full webapp development environment', async function() {
120 const pkg = require(`${appRoot}/package.json`)
121 const res = await new Promise(resolve => http.get(`http://localhost:5000/${pkg.name}/${pkg.version}/home.js`, resolve))
122 expect(res.statusCode).to.eql(200)
123 expect(res.headers['content-type']).to.contain('application/javascript')
124 })
125
126 it('should be able to serve components in another path', async function() {
127 const pkg = require(`${appRoot}/package.json`)
128 const res = await new Promise(resolve => http.get(`http://localhost:5000/${pkg.name}/${pkg.version}/test/suite.js`, resolve))
129 expect(res.statusCode).to.eql(200)
130 expect(res.headers['content-type']).to.contain('application/javascript')
131 })
132})
133
134describe('porter-serve web application --headless', function() {
135 it('should be able to run web application tests headlessly', async function() {
136 const proc = spawn(cmd, [
137 '--paths', 'components',
138 '--paths', 'browser_modules',
139 '--headless'
140 ], { stdio: 'inherit', cwd: appRoot })
141 await new Promise((resolve, reject) => {
142 proc.on('exit', code => {
143 if (code > 0) {
144 reject(new Error(`${cmd} exits with non-zero code: ${code}`))
145 } else {
146 resolve()
147 }
148 })
149 })
150 })
151})