Skip to content

Commit

Permalink
use tap instead of ava, 100% test coverage, move types to types folde…
Browse files Browse the repository at this point in the history
…r, activate linting in ci-pipeline (#65)
  • Loading branch information
Uzlopak authored Aug 18, 2022
1 parent c449ccf commit 6090363
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 127 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ jobs:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
with:
license-check: true
lint: true
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 2 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- test/**/*[!jest].test.js
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
"version": "2.0.0",
"description": "A small utility for creating warnings and emitting them.",
"main": "index.js",
"types": "index.d.ts",
"types": "types/index.d.ts",
"scripts": {
"test": "standard && ava -v test.js && jest jest.test.js && tsd"
"lint": "standard",
"test": "npm run test:unit && npm run test:jest && npm run test:typescript",
"test:jest": "jest jest.test.js",
"test:unit": "tap",
"test:typescript": "tsd"
},
"repository": {
"type": "git",
Expand All @@ -27,9 +31,9 @@
},
"homepage": "https://github.com/fastify/fastify-warning#readme",
"devDependencies": {
"ava": "^3.10.1",
"jest": "^28.1.0",
"standard": "^17.0.0",
"tap": "^16.3.0",
"tsd": "^0.22.0"
}
}
122 changes: 0 additions & 122 deletions test.js

This file was deleted.

26 changes: 26 additions & 0 deletions test/emit-interpolated-string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const test = require('tap').test
const build = require('..')

test('emit with interpolated string', t => {
t.plan(4)
const { create, emit, emitted } = build()

process.on('warning', onWarning)
function onWarning (warning) {
t.equal(warning.name, 'FastifyDeprecation')
t.equal(warning.code, 'CODE')
t.equal(warning.message, 'Hello world')
t.ok(emitted.get('CODE'))
}

create('FastifyDeprecation', 'CODE', 'Hello %s')
emit('CODE', 'world')
emit('CODE', 'world')

setImmediate(() => {
process.removeListener('warning', onWarning)
t.end()
})
})
26 changes: 26 additions & 0 deletions test/emit-once-only.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const test = require('tap').test
const build = require('..')

test('emit should emit a given code only once', t => {
t.plan(4)

const { create, emit, emitted } = build()

process.on('warning', onWarning)
function onWarning (warning) {
t.equal(warning.name, 'FastifyDeprecation')
t.equal(warning.code, 'CODE')
t.equal(warning.message, 'Hello world')
t.ok(emitted.get('CODE'))
}

create('FastifyDeprecation', 'CODE', 'Hello world')
emit('CODE')
emit('CODE')
setImmediate(() => {
process.removeListener('warning', onWarning)
t.end()
})
})
91 changes: 91 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict'

const test = require('tap').test
const build = require('..')

process.removeAllListeners('warning')

test('Create warning with zero parameter', t => {
t.plan(3)

const { create } = build()
const buildWarnOpts = create('FastifyWarning', 'CODE', 'Not available')
const opts = buildWarnOpts()
t.equal(opts.name, 'FastifyWarning')
t.equal(opts.message, 'Not available')
t.equal(opts.code, 'CODE')
})

test('Create error with 1 parameter', t => {
t.plan(3)

const { create } = build()
const buildWarningOpts = create('FastifyWarning', 'CODE', 'hey %s')
const opts = buildWarningOpts('alice')
t.equal(opts.name, 'FastifyWarning')
t.equal(opts.message, 'hey alice')
t.equal(opts.code, 'CODE')
})

test('Create error with 2 parameters', t => {
t.plan(3)

const { create } = build()
const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s')
const opts = buildWarnOpts('alice', 'attitude')
t.equal(opts.name, 'FastifyWarning')
t.equal(opts.message, 'hey alice, I like your attitude')
t.equal(opts.code, 'CODE')
})

test('Create error with 3 parameters', t => {
t.plan(3)

const { create } = build()
const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s %s')
const opts = buildWarnOpts('alice', 'attitude', 'see you')
t.equal(opts.name, 'FastifyWarning')
t.equal(opts.message, 'hey alice, I like your attitude see you')
t.equal(opts.code, 'CODE')
})

test('Should throw when error code has no fastify name', t => {
t.plan(1)

const { create } = build()

t.throws(() => create(), new Error('Warning name must not be empty'))
})

test('Should throw when error has no code', t => {
t.plan(1)

const { create } = build()

t.throws(() => create('name'), new Error('Warning code must not be empty'))
})

test('Should throw when error has no message', t => {
t.plan(1)

const { create } = build()

t.throws(() => create('name', 'code'), new Error('Warning message must not be empty'))
})

test('Should throw if emit is called with unknown code ', t => {
t.plan(1)

const { emit } = build()

t.throws(() => emit('CODE'), new Error('The code \'CODE\' does not exist'))
})

test('Cannot reuse the same code more than once', t => {
t.plan(1)

const { create } = build()
create('FastifyWarning', 'CODE', 'Not available')

t.throws(() => create('FastifyWarning', 'CODE', 'Not available'), new Error("The code 'CODE' already exist"))
})
2 changes: 1 addition & 1 deletion jest.test.js → test/jest.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global test, expect */
'use strict'

const build = require('./')
const build = require('..')

test('works with jest', done => {
const { create, emit, emitted } = build()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion index.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectType } from 'tsd'
import Warinig, { BuildWarnOptsFn, WarnOpts } from './'
import Warinig, { BuildWarnOptsFn, WarnOpts } from '..'

const warning = Warinig()
const buildWarnOpts = warning.create('FastifyWarning', 'CODE', 'message')
Expand Down

0 comments on commit 6090363

Please sign in to comment.