-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
42 lines (40 loc) · 1.31 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const { createReadStream, readFileSync } = require('fs')
const ss = require('./index.js')
// List of files to be tested
const files = ['./CONTRIBUTING.md', './.gitignore']
describe('using the module with promises', () => {
files.forEach(file => {
it('works with ' + file, () =>
ss(createReadStream(file)).then(data => {
expect(data).toBe(readFileSync(file).toString())
})
)
})
it('provides an error when the file can\'t be read', () =>
ss(createReadStream('./not-a-file-in-here'))
.then(
() => Promise.reject(new Error('uh-oh!')), // If the library resolves this promise, then the library is wrong
() => expect(true).toBe(true) // If the library rejects this promise, it's all good :)
)
)
})
describe('using the module with callbacks', () => {
files.forEach(file => {
it('works with ' + file, () => new Promise((resolve, reject) => {
ss(createReadStream(file), (err, data) => {
if (err) {
reject(err)
} else {
expect(data).toBe(readFileSync(file).toString())
resolve()
}
})
}))
})
it('provides an error when the file can\'t be read', () => new Promise((resolve, reject) =>
ss(createReadStream('./not-a-file-in-here'), err => {
expect(err).toBeTruthy()
resolve()
})
))
})