Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 0fe9ca8

Browse files
committed
Run tests in the browser
1 parent cd4f2e2 commit 0fe9ca8

File tree

13 files changed

+197
-23
lines changed

13 files changed

+197
-23
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ before_install:
1212
# Workaround for a permissions issue with Travis virtual machine images
1313
script:
1414
- npm test
15+
16+
before_script:
17+
- export DISPLAY=:99.0
18+
- sh -e /etc/init.d/xvfb start

karma.conf.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module.exports = function (config) {
2+
config.set({
3+
4+
// base path, that will be used to resolve files and exclude
5+
basePath: '',
6+
7+
// frameworks to use
8+
frameworks: ['mocha'],
9+
10+
// list of files / patterns to load in the browser
11+
files: [
12+
'tests/test-core/browser.js'
13+
],
14+
15+
// list of preprocessors
16+
preprocessors: {
17+
'tests/test-core/*': ['webpack']
18+
},
19+
20+
webpack: {
21+
resolve: {
22+
extensions: ['', '.js']
23+
},
24+
externals: {
25+
fs: '{}'
26+
},
27+
node: {
28+
Buffer: true
29+
}
30+
},
31+
32+
webpackMiddleware: {
33+
noInfo: true,
34+
stats: {
35+
colors: true
36+
}
37+
},
38+
39+
// test results reporter to use
40+
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
41+
reporters: ['spec'],
42+
43+
// web server port
44+
port: 9876,
45+
46+
// enable / disable colors in the output (reporters and logs)
47+
colors: true,
48+
49+
// level of logging
50+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
51+
logLevel: config.LOG_INFO,
52+
53+
// enable / disable watching file and executing tests whenever any file changes
54+
autoWatch: false,
55+
56+
// Start these browsers, currently available:
57+
// - Chrome
58+
// - ChromeCanary
59+
// - Firefox
60+
// - Opera (has to be installed with `npm install karma-opera-launcher`)
61+
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
62+
// - PhantomJS
63+
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
64+
browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],
65+
66+
// If browser does not capture in given timeout [ms], kill it
67+
captureTimeout: 60000,
68+
69+
// Continuous Integration mode
70+
// if true, it capture browsers, run tests and exit
71+
singleRun: true
72+
})
73+
}

package.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"scripts": {
1010
"lint": "standard",
1111
"coverage": "istanbul cover --print both -- _mocha tests/test-*/index.js",
12-
"test": "mocha tests/test-*/index.js",
12+
"test": "npm run test:node && npm run test:browser",
13+
"test:node": "mocha tests/test-*/index.js",
14+
"test:browser": "karma start karma.conf.js",
1315
"test:core": "mocha tests/test-core/index.js",
1416
"test:cli": "mocha tests/test-cli/index.js",
1517
"test:cli-offline": "mocha tests/test-cli-offline/index.js"
@@ -32,20 +34,33 @@
3234
},
3335
"homepage": "https://github.com/ipfs/js-ipfs#readme",
3436
"devDependencies": {
37+
"async": "^1.5.2",
3538
"chai": "^3.4.1",
39+
"fs-blob-store": "^5.2.1",
3640
"istanbul": "^0.4.1",
41+
"karma": "^0.13.19",
42+
"karma-chrome-launcher": "^0.2.2",
43+
"karma-cli": "^0.1.2",
44+
"karma-firefox-launcher": "^0.1.7",
45+
"karma-mocha": "^0.2.1",
46+
"karma-spec-reporter": "0.0.23",
47+
"karma-webpack": "^1.7.0",
48+
"local-storage-blob-store": "0.0.2",
49+
"lodash": "^4.0.0",
3750
"mocha": "^2.3.4",
3851
"ncp": "^2.0.0",
3952
"nexpect": "^0.5.0",
4053
"pre-commit": "^1.1.2",
54+
"raw-loader": "^0.5.1",
4155
"rimraf": "^2.4.4",
42-
"standard": "^5.3.1"
56+
"standard": "^5.4.1",
57+
"webpack": "^1.12.11"
4358
},
4459
"dependencies": {
4560
"boom": "^3.1.1",
4661
"debug": "^2.2.0",
4762
"hapi": "^12.0.0",
48-
"ipfs-repo": "^0.2.2",
63+
"ipfs-repo": "^0.4.1",
4964
"lodash.get": "^4.0.0",
5065
"lodash.set": "^4.0.0",
5166
"ronin": "^0.3.11"

src/ipfs-core/config.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/ipfs-core/default-repo/browser.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const localStorage = require('local-storage-blob-store')
4+
const IPFSRepo = require('ipfs-repo')
5+
6+
const options = {
7+
stores: {
8+
keys: localStorage,
9+
config: localStorage,
10+
datastore: localStorage,
11+
// datastoreLegacy: needs https://github.com/ipfs/js-ipfs-repo/issues/6#issuecomment-164650642
12+
logs: localStorage,
13+
locks: localStorage,
14+
version: localStorage
15+
}
16+
}
17+
18+
module.exports = () => {
19+
return new IPFSRepo('ipfs', options)
20+
}

src/ipfs-core/default-repo/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const isNode = !global.window
2+
3+
module.exports = isNode
4+
? require('./node')
5+
: require('./browser')

src/ipfs-core/default-repo/node.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const os = require('os')
4+
const fs = require('fs-blob-store')
5+
const IPFSRepo = require('ipfs-repo')
6+
7+
const options = {
8+
stores: {
9+
keys: fs,
10+
config: fs,
11+
datastore: fs,
12+
// datastoreLegacy: needs https://github.com/ipfs/js-ipfs-repo/issues/6#issuecomment-164650642
13+
logs: fs,
14+
locks: fs,
15+
version: fs
16+
}
17+
}
18+
19+
module.exports = () => {
20+
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
21+
return new IPFSRepo(repoPath, options)
22+
}

src/ipfs-core/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict'
22

3-
const config = require('./config')
4-
const IPFSRepo = require('ipfs-repo')
3+
const defaultRepo = require('./default-repo')
54

65
exports = module.exports = IPFS
7-
exports.config = config
86

9-
function IPFS () {
7+
function IPFS (repo) {
108
if (!(this instanceof IPFS)) {
119
throw new Error('Must be instantiated with new')
1210
}
1311

14-
var repo = new IPFSRepo(config.repoPath())
12+
if (!repo) {
13+
repo = defaultRepo()
14+
}
1515

1616
this.daemon = callback => {
1717
// 1. read repo to get peer data

tests/test-core/browser.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* globals describe, before */
2+
3+
// const expect = require('chai').expect
4+
const async = require('async')
5+
const store = require('local-storage-blob-store')
6+
const _ = require('lodash')
7+
8+
var repoContext = require.context('raw!../repo-example', true)
9+
10+
describe('core', function () {
11+
before(function (done) {
12+
window.localStorage.clear()
13+
14+
var repoData = []
15+
repoContext.keys().forEach(function (key) {
16+
repoData.push({
17+
key: key.replace('./', ''),
18+
value: repoContext(key)
19+
})
20+
})
21+
22+
var mainBlob = store('ipfs')
23+
var blocksBlob = store('ipfs/')
24+
25+
async.eachSeries(repoData, (file, cb) => {
26+
if (_.startsWith(file.key, 'datastore/')) {
27+
return cb()
28+
}
29+
30+
const blob = _.startsWith(file.key, 'blocks/')
31+
? blocksBlob
32+
: mainBlob
33+
34+
blob.createWriteStream({
35+
key: file.key
36+
}).end(file.value, cb)
37+
}, done)
38+
})
39+
40+
const testsContext = require.context('.', true, /test-*/)
41+
testsContext
42+
.keys()
43+
.forEach(key => testsContext(key))
44+
})

tests/test-core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('core', () => {
2828

2929
const tests = fs.readdirSync(__dirname)
3030
tests.filter(file => {
31-
if (file === 'index.js') {
31+
if (file === 'index.js' || file === 'browser.js') {
3232
return false
3333
} else {
3434
return true

tests/test-core/test-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('config', () => {
6767
}
6868

6969
it('show', done => {
70-
let ipfs = new IPFS()
70+
const ipfs = new IPFS()
7171
ipfs.config.show((err, config) => {
7272
expect(err).to.not.exist
7373
expect(config).to.deep.equal(defaultConfig)
@@ -76,7 +76,7 @@ describe('config', () => {
7676
})
7777

7878
it('replace', done => {
79-
let ipfs = new IPFS()
79+
const ipfs = new IPFS()
8080
ipfs.config.replace({}, err => {
8181
expect(err).to.not.exist
8282
ipfs.config.show((err, config) => {
@@ -92,7 +92,7 @@ describe('config', () => {
9292

9393
// cli only feature built with show and replace
9494
// it.skip('edit', done => {
95-
// let ipfs = new IPFS()
95+
// const ipfs = new IPFS()
9696
// ipfs.config((err, config) => {
9797
// expect(err).to.not.exist
9898
// done()

tests/test-core/test-id.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const IPFS = require('../../src/ipfs-core')
99

1010
describe('id', () => {
1111
it('get id', done => {
12-
let ipfs = new IPFS()
12+
const ipfs = new IPFS()
1313
ipfs.id((err, id) => {
1414
expect(err).to.not.exist
1515
expect(id).to.deep.equal({ ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',

tests/test-core/test-version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const IPFS = require('../../src/ipfs-core')
99

1010
describe('version', () => {
1111
it('get version', done => {
12-
let ipfs = new IPFS()
12+
const ipfs = new IPFS()
1313
ipfs.version((err, version) => {
1414
expect(err).to.not.exist
1515
expect(version).to.equal('0.4.0-dev')

0 commit comments

Comments
 (0)