Skip to content

Commit

Permalink
chore: Replaced tap with Node test runner.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Feb 6, 2024
1 parent ee8e568 commit a732490
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 120 deletions.
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"format": "prettier -w src test",
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx src test",
"typecheck": "tsc -p . --emitDeclarationOnly",
"test": "c8 -c test/config/c8-local.json tap test/*.test.ts",
"test:ci": "c8 -c test/config/c8-ci.json tap --no-color test/*.test.ts",
"test": "c8 -c test/config/c8-local.json node --import tsx --test test/*.test.ts",
"test:ci": "c8 -c test/config/c8-ci.json node --import tsx --test-reporter=tap --test test/*.test.ts",
"ci": "npm run build && npm run test:ci",
"prepublishOnly": "npm run ci",
"postpublish": "git push origin && git push origin -f --tags"
Expand All @@ -67,14 +67,10 @@
"chokidar": "^3.5.3",
"concurrently": "^8.2.2",
"prettier": "^3.2.4",
"tap": "^18.7.0",
"ts-node": "^10.9.2",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
},
"engines": {
"node": ">= 18.18.0"
},
"tap": {
"extends": "./test/config/tap.yml"
}
}
31 changes: 13 additions & 18 deletions test/buffersAndStreams.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import { deepStrictEqual, rejects } from 'node:assert'
import { createReadStream, readFileSync } from 'node:fs'
import t from 'tap'
import { test } from 'node:test'
import { info } from '../src/index.js'
import { FastImageError } from '../src/models.js'

const fileName = import.meta.url.replace('file://', '')
const imagePath = new URL('fixtures/image.png', import.meta.url).toString().replace('file://', '')

t.test('fastimage.info', t => {
t.test('when working with buffers', t => {
t.test('should return the information of a image', async t => {
test('fastimage.info', async () => {
await test('when working with buffers', async () => {
await test('should return the information of a image', async () => {
const buffer = readFileSync(imagePath)

const data = await info(buffer)

t.same(data, {
deepStrictEqual(data, {
width: 150,
height: 150,
type: 'png',
Expand All @@ -24,20 +25,18 @@ t.test('fastimage.info', t => {
})
})

t.test('should return a error when the data is not a image', async t => {
await test('should return a error when the data is not a image', async () => {
const buffer = readFileSync(fileName)

await t.rejects(info(buffer), new FastImageError('Unsupported data.', 'UNSUPPORTED'))
await rejects(info(buffer), new FastImageError('Unsupported data.', 'UNSUPPORTED'))
})

t.end()
})

t.test('when working with streams', t => {
t.test('should return the information of a image', async t => {
await test('when working with streams', async () => {
await test('should return the information of a image', async () => {
const data = await info(createReadStream(imagePath))

t.same(data, {
deepStrictEqual(data, {
width: 150,
height: 150,
type: 'png',
Expand All @@ -46,12 +45,8 @@ t.test('fastimage.info', t => {
})
})

t.test('should return a error when the data is not a image', async t => {
await t.rejects(info(fileName), new FastImageError('Unsupported data.', 'UNSUPPORTED'))
await test('should return a error when the data is not a image', async () => {
await rejects(info(fileName), new FastImageError('Unsupported data.', 'UNSUPPORTED'))
})

t.end()
})

t.end()
})
7 changes: 0 additions & 7 deletions test/config/tap.yml

This file was deleted.

56 changes: 25 additions & 31 deletions test/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,66 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import { chmodSync, unlinkSync, writeFileSync } from 'node:fs'
import { deepStrictEqual, ifError } from 'node:assert'
import { chmodSync, existsSync, unlinkSync, writeFileSync } from 'node:fs'
import { dirname } from 'node:path'
import t from 'tap'
import { test } from 'node:test'
import { info } from '../src/index.js'
import { FastImageError } from '../src/models.js'

const fileName = import.meta.url.replace('file://', '')
const imagePath = new URL('fixtures/image.png', import.meta.url).toString().replace('file://', '')

t.test('fastimage.info', t => {
t.test('when working with local files', t => {
t.test('should return the information of a image', t => {
test('fastimage.info', async () => {
await test('when working with local files', async () => {
await test('should return the information of a image', () => {
info(imagePath, (error, data) => {
t.error(error)
ifError(error)

t.same(data, {
deepStrictEqual(data, {
width: 150,
height: 150,
type: 'png',
time: data!.time,
analyzed: 409
})

t.end()
})
})

t.test('should return a error when the path is a directory', t => {
await test('should return a error when the path is a directory', () => {
info(dirname(fileName), (error, data) => {
t.error(data)
t.strictSame(error, new FastImageError('Source is a directory.', 'FS_ERROR'))
t.end()
ifError(data)
deepStrictEqual(error, new FastImageError('Source is a directory.', 'FS_ERROR'))
})
})

t.test('should return a error when the path cannot be found', t => {
await test('should return a error when the path cannot be found', () => {
info('/not/existent', (error, data) => {
t.error(data)
t.strictSame(error, new FastImageError('Source not found.', 'FS_ERROR'))
t.end()
ifError(data)
deepStrictEqual(error, new FastImageError('Source not found.', 'FS_ERROR'))
})
})

t.test('should return a error when the path cannot be read', t => {
await test('should return a error when the path cannot be read', () => {
const unreadablePath = imagePath.replace('image.png', 'unreadable.png')
writeFileSync(unreadablePath, 'foo', 'utf8')
chmodSync(unreadablePath, 0)

if (!existsSync(unreadablePath)) {
writeFileSync(unreadablePath, 'foo', 'utf8')
chmodSync(unreadablePath, 0)
}

info(unreadablePath, (error, data) => {
t.error(data)
t.strictSame(error, new FastImageError('Source is not readable.', 'FS_ERROR'))
t.end()
ifError(data)
deepStrictEqual(error, new FastImageError('Source is not readable.', 'FS_ERROR'))

unlinkSync(unreadablePath)
})
})

t.test('should return a error when the path is not a image', t => {
await test('should return a error when the path is not a image', () => {
info(fileName, (error, data) => {
t.error(data)
t.strictSame(error, new FastImageError('Unsupported data.', 'UNSUPPORTED'))
t.end()
ifError(data)
deepStrictEqual(error, new FastImageError('Unsupported data.', 'UNSUPPORTED'))
})
})

t.end()
})

t.end()
})
15 changes: 7 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import t from 'tap'
import { deepStrictEqual, ok, rejects } from 'node:assert'
import { test } from 'node:test'
import { info } from '../src/index.js'
import { FastImageError } from '../src/models.js'

t.test('fastimage.info', t => {
t.test('side cases', async t => {
test('fastimage.info', async () => {
await test('side cases', async () => {
// This is a file which is corrupted. To correctly recognize the threshold must be disabled.
await t.rejects(
await rejects(
info(
'https://upload.wikimedia.org/wikipedia/commons/b/b2/%27Journey_to_the_Center_of_the_Earth%27_by_%C3%89douard_Riou_38.jpg'
),
Expand All @@ -19,7 +20,7 @@ t.test('fastimage.info', t => {
{ threshold: 0 }
)

t.same(data, {
deepStrictEqual(data, {
width: 980,
height: 1448,
type: 'jpg',
Expand All @@ -30,8 +31,6 @@ t.test('fastimage.info', t => {
size: 554_617
})

t.ok(data.analyzed < data.size!)
ok(data.analyzed < data.size)
})

t.end()
})
25 changes: 9 additions & 16 deletions test/streams.test.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import { deepStrictEqual } from 'node:assert'
import { createReadStream } from 'node:fs'
import t from 'tap'
import { test } from 'node:test'
import { stream } from '../src/index.js'
import { FastImageError } from '../src/models.js'

const fileName = import.meta.url.replace('file://', '')
const imagePath = new URL('fixtures/image.png', import.meta.url).toString().replace('file://', '')

t.test('fastimage.stream', t => {
t.test('should emit info event when info are ready', t => {
test('fastimage.stream', async () => {
await test('should emit info event when info are ready', () => {
const input = createReadStream(imagePath, { highWaterMark: 200 })

const pipe = input.pipe(stream())

pipe.on('info', data => {
t.same(data, {
deepStrictEqual(data, {
width: 150,
height: 150,
type: 'png',
time: data.time,
analyzed: 200
})

t.end()
})
})

t.test('should emit error event in case of errors', t => {
await test('should emit error event in case of errors', () => {
const input = createReadStream(fileName)

const pipe = input.pipe(stream())

pipe.on('error', error => {
t.strictSame(error, new FastImageError('Unsupported data.', 'UNSUPPORTED'))

t.end()
deepStrictEqual(error, new FastImageError('Unsupported data.', 'UNSUPPORTED'))
})
})

t.test('should accept the threshold option', t => {
await test('should accept the threshold option', () => {
const input = createReadStream(imagePath, { highWaterMark: 1 })

const pipe = input.pipe(stream({ threshold: 10 }))

pipe.on('error', error => {
t.strictSame(error, new FastImageError('Unsupported data.', 'UNSUPPORTED'))

t.end()
deepStrictEqual(error, new FastImageError('Unsupported data.', 'UNSUPPORTED'))
})
})

t.end()
})
Loading

0 comments on commit a732490

Please sign in to comment.