Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 6414e66

Browse files
committed
Merge pull request #153 from ipfs/chai
Replace assert with chai
2 parents b040033 + 3bd6788 commit 6414e66

22 files changed

+110
-179
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"babel-plugin-transform-runtime": "^6.1.18",
3030
"babel-preset-es2015": "^6.0.15",
3131
"babel-runtime": "^5.8.34",
32+
"chai": "^3.4.1",
3233
"concurrently": "^1.0.0",
3334
"eslint-config-standard": "^4.4.0",
3435
"eslint-plugin-standard": "^1.3.1",

src/api/dht.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module.exports = send => {
2424
if (res.Type === 5) {
2525
cb(null, res.Extra)
2626
} else {
27-
cb(res)
27+
let error = new Error('key was not found (type 6)')
28+
cb(error)
2829
}
2930
})
3031
},

test/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"globals": {
66
"apiClients": true,
77
"isNode": true,
8-
"assert": true
8+
"expect": true
99
}
1010
}

test/api/add.spec.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,21 @@ describe('.add', () => {
3434
})
3535

3636
apiClients['a'].add(file, (err, res) => {
37-
if (err) throw err
37+
expect(err).to.not.exist
3838

3939
const added = res[0] != null ? res[0] : res
40-
assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
41-
assert.equal(added.Name, path.basename(testfilePath))
40+
expect(added).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
41+
expect(added).to.have.property('Name', path.basename(testfilePath))
4242
done()
4343
})
4444
})
4545

4646
it('add buffer', done => {
4747
let buf = new Buffer(testfile)
4848
apiClients['a'].add(buf, (err, res) => {
49-
if (err) throw err
50-
51-
assert.equal(res.length, 1)
52-
const added = res[0]
53-
assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
49+
expect(err).to.not.exist
50+
expect(res).to.have.length(1)
51+
expect(res[0]).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
5452
done()
5553
})
5654
})
@@ -61,11 +59,9 @@ describe('.add', () => {
6159
}
6260

6361
apiClients['a'].add(testfileBig, (err, res) => {
64-
if (err) throw err
65-
66-
assert.equal(res.length, 1)
67-
const added = res[0]
68-
assert.equal(added.Hash, 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq')
62+
expect(err).to.not.exist
63+
expect(res).to.have.length(1)
64+
expect(res[0]).to.have.a.property('Hash', 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq')
6965
done()
7066
})
7167
})
@@ -76,24 +72,24 @@ describe('.add', () => {
7672
}
7773

7874
apiClients['a'].add(testfilePath, (err, res) => {
79-
if (err) throw err
75+
expect(err).to.not.exist
8076

8177
const added = res[0] != null ? res[0] : res
82-
assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
78+
expect(added).to.have.property('Hash', 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
8379
done()
8480
})
8581
})
8682

8783
it('add a nested dir', done => {
8884
apiClients['a'].add(__dirname + '/../test-folder', { recursive: true }, (err, res) => {
8985
if (isNode) {
90-
if (err) throw err
86+
expect(err).to.not.exist
9187

9288
const added = res[res.length - 1]
93-
assert.equal(added.Hash, 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg')
89+
expect(added).to.have.property('Hash', 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg')
9490
done()
9591
} else {
96-
assert.equal(err.message, 'Recursive uploads are not supported in the browser')
92+
expect(err.message).to.be.equal('Recursive uploads are not supported in the browser')
9793
done()
9894
}
9995
})
@@ -104,21 +100,21 @@ describe('.add', () => {
104100
stream.push('Hello world')
105101
stream.push(null)
106102
apiClients['a'].add(stream, (err, res) => {
107-
if (err) throw err
103+
expect(err).to.not.exist
108104

109105
const added = res[0] != null ? res[0] : res
110-
assert.equal(added.Hash, 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
106+
expect(added).to.have.a.property('Hash', 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
111107
done()
112108
})
113109
})
114110

115111
it('add url', done => {
116112
const url = 'https://raw.githubusercontent.com/ipfs/js-ipfs-api/2a9cc63d7427353f2145af6b1a768a69e67c0588/README.md'
117113
apiClients['a'].add(url, (err, res) => {
118-
if (err) throw err
114+
expect(err).to.not.exist
119115

120116
const added = res[0] != null ? res[0] : res
121-
assert.equal(added.Hash, 'QmZmHgEX9baxUn3qMjsEXQzG6DyNcrVnwieQQTrpDdrFvt')
117+
expect(added).to.have.a.property('Hash', 'QmZmHgEX9baxUn3qMjsEXQzG6DyNcrVnwieQQTrpDdrFvt')
122118
done()
123119
})
124120
})

test/api/block.spec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@ describe('.block', () => {
66

77
it('block.put', done => {
88
apiClients['a'].block.put(blorb, (err, res) => {
9-
if (err) throw err
10-
const store = res.Key
11-
assert.equal(store, 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ')
9+
expect(err).to.not.exist
10+
expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ')
1211
done()
1312
})
1413
})
1514

1615
it('block.get', done => {
1716
apiClients['a'].block.get(blorbKey, (err, res) => {
18-
if (err) throw err
17+
expect(err).to.not.exist
1918

2019
let buf = ''
2120
res
2221
.on('data', function (data) { buf += data })
2322
.on('end', function () {
24-
assert.equal(buf, 'blorb')
23+
expect(buf).to.be.equal('blorb')
2524
done()
2625
})
2726
})

test/api/cat.spec.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ if (isNode) {
1515
describe('.cat', () => {
1616
it('cat', done => {
1717
apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
18-
if (err) {
19-
throw err
20-
}
18+
expect(err).to.not.exist
2119

2220
let buf = ''
2321
res
2422
.on('error', err => { throw err })
2523
.on('data', data => buf += data)
2624
.on('end', () => {
27-
assert.equal(buf, testfile)
25+
expect(buf).to.be.equal(testfile.toString())
2826
done()
2927
})
3028
})
@@ -36,16 +34,14 @@ describe('.cat', () => {
3634
}
3735

3836
apiClients['a'].cat('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => {
39-
if (err) {
40-
throw err
41-
}
37+
expect(err).to.not.exist
4238

4339
testfileBig = require('fs').createReadStream(__dirname + '/../15mb.random', { bufferSize: 128 })
4440

4541
// Do not blow out the memory of nodejs :)
4642
streamEqual(res, testfileBig, (err, equal) => {
47-
if (err) throw err
48-
assert(equal)
43+
expect(err).to.not.exist
44+
expect(equal).to.be.true
4945
done()
5046
})
5147
})

test/api/commands.spec.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
describe('.commands', () => {
44
it('lists commands', done => {
55
apiClients['a'].commands((err, res) => {
6-
if (err) {
7-
throw err
8-
}
9-
assert(res)
6+
expect(err).to.not.exist
7+
expect(res).to.exist
108
done()
119
})
1210
})

test/api/config.spec.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,19 @@ describe('.config', () => {
66
const confVal = 'arbitraryVal'
77

88
apiClients['a'].config.set(confKey, confVal, (err, res) => {
9-
if (err) throw err
9+
expect(err).to.not.exist
1010
apiClients['a'].config.get(confKey, (err, res) => {
11-
if (err) throw err
12-
assert.equal(res.Value, confVal)
11+
expect(err).to.not.exist
12+
expect(res).to.have.a.property('Value', confVal)
1313
done()
1414
})
1515
})
1616
})
1717

1818
it('.config.show', done => {
1919
apiClients['c'].config.show((err, res) => {
20-
if (err) {
21-
throw err
22-
}
23-
24-
assert(res)
20+
expect(err).to.not.exist
21+
expect(res).to.exist
2522
done()
2623
})
2724
})
@@ -32,11 +29,8 @@ describe('.config', () => {
3229
}
3330

3431
apiClients['c'].config.replace(__dirname + '/../r-config.json', (err, res) => {
35-
if (err) {
36-
throw err
37-
}
38-
39-
assert.equal(res, null)
32+
expect(err).to.not.exist
33+
expect(res).to.be.equal(null)
4034
done()
4135
})
4236
})

test/api/dht.spec.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,35 @@ describe('.dht', () => {
44
it('returns an error when getting a non-existent key from the DHT',
55
done => {
66
apiClients['a'].dht.get('non-existent', {timeout: '100ms'}, (err, value) => {
7-
assert(err)
7+
expect(err).to.be.an.instanceof(Error)
88
done()
99
})
1010
})
1111

1212
it('puts and gets a key value pair in the DHT', done => {
1313
apiClients['a'].dht.put('scope', 'interplanetary', (err, res) => {
14-
if (err) {
15-
throw err
16-
}
14+
expect(err).to.not.exist
1715

18-
assert.equal(typeof res, 'object')
16+
expect(res).to.be.an('array')
1917

20-
return done()
18+
done()
2119

2220
// non ipns or pk hashes fail to fetch, known bug
2321
// bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234
2422
// apiClients['a'].dht.get('scope', (err, value) => {
2523
// console.log('->>', err, value)
26-
// if (err) {
27-
// throw err
28-
// }
29-
// assert.equal(value, 'interplanetary')
24+
// expect(err).to.not.exist
25+
// expect(value).to.be.equal('interplanetary')
3026
// done()
3127
// })
3228
})
3329
})
3430

3531
it('.dht.findprovs', done => {
3632
apiClients['a'].dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
37-
if (err) {
38-
throw err
39-
}
33+
expect(err).to.not.exist
4034

41-
assert.equal(typeof res, 'object')
42-
assert(res)
35+
expect(res).to.be.an('array')
4336
done()
4437
})
4538
})

test/api/diag.spec.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@
33
describe('.diag', () => {
44
it('.diag.net', done => {
55
apiClients['a'].diag.net((err, res) => {
6-
if (err) {
7-
throw err
8-
}
9-
assert(res)
6+
expect(err).to.not.exist
7+
expect(res).to.exist
108
done()
119
})
1210
})
1311

1412
it('.diag.sys', done => {
1513
apiClients['a'].diag.sys((err, res) => {
16-
if (err) {
17-
throw err
18-
}
19-
assert(res)
20-
assert(res.memory)
21-
assert(res.diskinfo)
14+
expect(err).to.not.exist
15+
expect(res).to.exist
16+
expect(res).to.have.a.property('memory')
17+
expect(res).to.have.a.property('diskinfo')
2218
done()
2319
})
2420
})

test/api/id.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
describe('.id', () => {
44
it('id', done => {
55
apiClients['a'].id((err, res) => {
6-
if (err) throw err
7-
const id = res
8-
assert(id.ID)
9-
assert(id.PublicKey)
6+
expect(err).to.not.exist
7+
expect(res).to.have.a.property('ID')
8+
expect(res).to.have.a.property('PublicKey')
109
done()
1110
})
1211
})

test/api/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe('API', () => {
44
it('has the api object', () => {
5-
assert(apiClients['a'])
6-
assert(apiClients['a'].id)
5+
expect(apiClients['a']).to.exist
6+
expect(apiClients['a']).to.have.a.property('id')
77
})
88
})

test/api/log.spec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
describe('.log', () => {
44
it('.log.tail', done => {
55
apiClients['a'].log.tail((err, res) => {
6-
if (err) {
7-
throw err
8-
}
6+
expect(err).to.not.exist
7+
98
res.once('data', obj => {
10-
assert(obj)
11-
assert.equal(typeof obj, 'object')
9+
expect(obj).to.be.an('object')
1210
done()
1311
})
1412
})

test/api/name.spec.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@ describe('.name', () => {
55

66
it('.name.publish', done => {
77
apiClients['a'].name.publish('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
8-
if (err) {
9-
throw err
10-
}
11-
assert(res)
8+
expect(err).to.not.exist
129
name = res
10+
expect(name).to.exist
1311
done()
1412
})
1513
})
1614

1715
it('.name.resolve', done => {
1816
apiClients['a'].name.resolve(name.Name, (err, res) => {
19-
if (err) {
20-
throw err
21-
}
22-
23-
assert(res)
24-
assert.deepEqual(res, { Path: '/ipfs/' + name.Value })
17+
expect(err).to.not.exist
18+
expect(res).to.exist
19+
expect(res).to.be.eql({
20+
Path: '/ipfs/' + name.Value
21+
})
2522
done()
2623
})
2724
})

0 commit comments

Comments
 (0)