Skip to content

Commit

Permalink
test(read-chunk): from Jest to test (#6478)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggunullu authored Oct 24, 2022
1 parent caf0eb3 commit 2d4317b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
31 changes: 16 additions & 15 deletions @vates/read-chunk/index.spec.js → @vates/read-chunk/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

/* eslint-env jest */
const { describe, it } = require('test')
const assert = require('node:assert').strict

const { Readable } = require('stream')

Expand All @@ -11,42 +12,42 @@ makeStream.obj = Readable.from

describe('readChunk', () => {
it('returns null if stream is empty', async () => {
expect(await readChunk(makeStream([]))).toBe(null)
assert.strictEqual(await readChunk(makeStream([])), null)
})

it('returns null if the stream is already ended', async () => {
const stream = await makeStream([])
await readChunk(stream)

expect(await readChunk(stream)).toBe(null)
assert.strictEqual(await readChunk(stream), null)
})

describe('with binary stream', () => {
it('returns the first chunk of data', async () => {
expect(await readChunk(makeStream(['foo', 'bar']))).toEqual(Buffer.from('foo'))
assert.deepEqual(await readChunk(makeStream(['foo', 'bar'])), Buffer.from('foo'))
})

it('returns a chunk of the specified size (smaller than first)', async () => {
expect(await readChunk(makeStream(['foo', 'bar']), 2)).toEqual(Buffer.from('fo'))
assert.deepEqual(await readChunk(makeStream(['foo', 'bar']), 2), Buffer.from('fo'))
})

it('returns a chunk of the specified size (larger than first)', async () => {
expect(await readChunk(makeStream(['foo', 'bar']), 4)).toEqual(Buffer.from('foob'))
assert.deepEqual(await readChunk(makeStream(['foo', 'bar']), 4), Buffer.from('foob'))
})

it('returns less data if stream ends', async () => {
expect(await readChunk(makeStream(['foo', 'bar']), 10)).toEqual(Buffer.from('foobar'))
assert.deepEqual(await readChunk(makeStream(['foo', 'bar']), 10), Buffer.from('foobar'))
})

it('returns an empty buffer if the specified size is 0', async () => {
expect(await readChunk(makeStream(['foo', 'bar']), 0)).toEqual(Buffer.alloc(0))
assert.deepEqual(await readChunk(makeStream(['foo', 'bar']), 0), Buffer.alloc(0))
})
})

describe('with object stream', () => {
it('returns the first chunk of data verbatim', async () => {
const chunks = [{}, {}]
expect(await readChunk(makeStream.obj(chunks))).toBe(chunks[0])
assert.strictEqual(await readChunk(makeStream.obj(chunks)), chunks[0])
})
})
})
Expand All @@ -62,15 +63,15 @@ const rejectionOf = promise =>
describe('readChunkStrict', function () {
it('throws if stream is empty', async () => {
const error = await rejectionOf(readChunkStrict(makeStream([])))
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('stream has ended without data')
expect(error.chunk).toEqual(undefined)
assert(error instanceof Error)
assert.strictEqual(error.message, 'stream has ended without data')
assert.strictEqual(error.chunk, undefined)
})

it('throws if stream ends with not enough data', async () => {
const error = await rejectionOf(readChunkStrict(makeStream(['foo', 'bar']), 10))
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('stream has ended with not enough data')
expect(error.chunk).toEqual(Buffer.from('foobar'))
assert(error instanceof Error)
assert.strictEqual(error.message, 'stream has ended with not enough data')
assert.deepEqual(error.chunk, Buffer.from('foobar'))
})
})
6 changes: 5 additions & 1 deletion @vates/read-chunk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
"node": ">=8.10"
},
"scripts": {
"postversion": "npm publish --access public"
"postversion": "npm publish --access public",
"test": "node--test"
},
"author": {
"name": "Vates SAS",
"url": "https://vates.fr"
},
"devDependencies": {
"test": "^3.2.1"
}
}

0 comments on commit 2d4317b

Please sign in to comment.