Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from xicombd/browser-tests
Browse files Browse the repository at this point in the history
Add browser tests
  • Loading branch information
daviddias committed Mar 21, 2016
2 parents 86d6fe2 + 206e40d commit 87e886d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ before_install:

script:
- npm run lint
- npm test
- npm test

addons:
firefox: 'latest'

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
39 changes: 39 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['mocha'],

files: [
'tests/multiplex-test.js',
'tests/browser.js'
],

preprocessors: {
'tests/*': ['webpack']
},

webpack: {
resolve: {
extensions: ['', '.js']
},
node: {
Buffer: true
}
},

webpackMiddleware: {
noInfo: true,
stats: {
colors: true
}
},
reporters: ['spec'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],
captureTimeout: 60000,
singleRun: true
})
}
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "src/index.js",
"scripts": {
"lint": "standard",
"test": "mocha tests/*-test.js",
"test:node": "mocha tests/*-test.js",
"test:browser": "node tests/karma.js",
"test": "npm run test:node && npm run test:browser",
"compliance": "node tests/compliance.js"
},
"repository": {
Expand All @@ -24,12 +26,20 @@
"devDependencies": {
"chai": "^3.5.0",
"interface-stream-muxer": "^0.3.1",
"karma": "^0.13.22",
"karma-chrome-launcher": "^0.2.2",
"karma-firefox-launcher": "^0.1.7",
"karma-mocha": "^0.2.2",
"karma-spec-reporter": "0.0.24",
"karma-webpack": "^1.7.0",
"libp2p-websockets": "^0.2.1",
"mocha": "^2.4.5",
"pre-commit": "^1.1.2",
"standard": "^6.0.7",
"stream-pair": "^1.0.3",
"tape": "^4.2.0",
"timed-tape": "^0.1.0",
"mocha": "^2.4.5"
"webpack": "^2.1.0-beta.4"
},
"dependencies": {
"multiplex": "^6.7.0"
Expand Down
35 changes: 35 additions & 0 deletions tests/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-env mocha */

const expect = require('chai').expect
const WSlibp2p = require('libp2p-websockets')
const multiplex = require('../src')
const multiaddr = require('multiaddr')

describe('browser + server', () => {
var ws
before((done) => {
ws = new WSlibp2p()
done()
})

it('create a stream and wait for server to create another', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
const dialerSocket = ws.dial(mh)

const dialer = multiplex(dialerSocket, false)
dialer.on('stream', (conn) => {
conn.on('data', (data) => {
expect(data.toString()).to.equal('hey')
conn.end()
done()
})
})

const conn = dialer.newStream()
conn.write('hey')

conn.on('error', (err) => {
expect(err).to.not.exist
})
})
})
38 changes: 38 additions & 0 deletions tests/karma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const Server = require('karma').Server
const path = require('path')
const WSlibp2p = require('libp2p-websockets')
const multiaddr = require('multiaddr')
const multiplex = require('../src')

var ws

function createServer (done) {
ws = new WSlibp2p()
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
ws.createListener(mh, (socket) => {
const listener = multiplex(socket, true)

listener.on('stream', (connA) => {
listener.newStream((err, connB) => {
if (err) {
throw err
}

connA.pipe(connB)
})
})
}, done)
}

function stopServer (done) {
ws.close(done)
}

function runTests (done) {
new Server({
configFile: path.join(__dirname, '/../karma.conf.js'),
singleRun: true
}, done).start()
}

createServer(() => runTests(() => stopServer(() => null)))

0 comments on commit 87e886d

Please sign in to comment.