Skip to content

Commit 6090363

Browse files
authored
use tap instead of ava, 100% test coverage, move types to types folder, activate linting in ci-pipeline (#65)
1 parent c449ccf commit 6090363

File tree

11 files changed

+156
-127
lines changed

11 files changed

+156
-127
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ jobs:
1515
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
1616
with:
1717
license-check: true
18+
lint: true

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.taprc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
files:
2+
- test/**/*[!jest].test.js

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
"version": "2.0.0",
44
"description": "A small utility for creating warnings and emitting them.",
55
"main": "index.js",
6-
"types": "index.d.ts",
6+
"types": "types/index.d.ts",
77
"scripts": {
8-
"test": "standard && ava -v test.js && jest jest.test.js && tsd"
8+
"lint": "standard",
9+
"test": "npm run test:unit && npm run test:jest && npm run test:typescript",
10+
"test:jest": "jest jest.test.js",
11+
"test:unit": "tap",
12+
"test:typescript": "tsd"
913
},
1014
"repository": {
1115
"type": "git",
@@ -27,9 +31,9 @@
2731
},
2832
"homepage": "https://github.com/fastify/fastify-warning#readme",
2933
"devDependencies": {
30-
"ava": "^3.10.1",
3134
"jest": "^28.1.0",
3235
"standard": "^17.0.0",
36+
"tap": "^16.3.0",
3337
"tsd": "^0.22.0"
3438
}
3539
}

test.js

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
test('emit with interpolated string', t => {
7+
t.plan(4)
8+
const { create, emit, emitted } = build()
9+
10+
process.on('warning', onWarning)
11+
function onWarning (warning) {
12+
t.equal(warning.name, 'FastifyDeprecation')
13+
t.equal(warning.code, 'CODE')
14+
t.equal(warning.message, 'Hello world')
15+
t.ok(emitted.get('CODE'))
16+
}
17+
18+
create('FastifyDeprecation', 'CODE', 'Hello %s')
19+
emit('CODE', 'world')
20+
emit('CODE', 'world')
21+
22+
setImmediate(() => {
23+
process.removeListener('warning', onWarning)
24+
t.end()
25+
})
26+
})

test/emit-once-only.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
test('emit should emit a given code only once', t => {
7+
t.plan(4)
8+
9+
const { create, emit, emitted } = build()
10+
11+
process.on('warning', onWarning)
12+
function onWarning (warning) {
13+
t.equal(warning.name, 'FastifyDeprecation')
14+
t.equal(warning.code, 'CODE')
15+
t.equal(warning.message, 'Hello world')
16+
t.ok(emitted.get('CODE'))
17+
}
18+
19+
create('FastifyDeprecation', 'CODE', 'Hello world')
20+
emit('CODE')
21+
emit('CODE')
22+
setImmediate(() => {
23+
process.removeListener('warning', onWarning)
24+
t.end()
25+
})
26+
})

test/index.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
process.removeAllListeners('warning')
7+
8+
test('Create warning with zero parameter', t => {
9+
t.plan(3)
10+
11+
const { create } = build()
12+
const buildWarnOpts = create('FastifyWarning', 'CODE', 'Not available')
13+
const opts = buildWarnOpts()
14+
t.equal(opts.name, 'FastifyWarning')
15+
t.equal(opts.message, 'Not available')
16+
t.equal(opts.code, 'CODE')
17+
})
18+
19+
test('Create error with 1 parameter', t => {
20+
t.plan(3)
21+
22+
const { create } = build()
23+
const buildWarningOpts = create('FastifyWarning', 'CODE', 'hey %s')
24+
const opts = buildWarningOpts('alice')
25+
t.equal(opts.name, 'FastifyWarning')
26+
t.equal(opts.message, 'hey alice')
27+
t.equal(opts.code, 'CODE')
28+
})
29+
30+
test('Create error with 2 parameters', t => {
31+
t.plan(3)
32+
33+
const { create } = build()
34+
const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s')
35+
const opts = buildWarnOpts('alice', 'attitude')
36+
t.equal(opts.name, 'FastifyWarning')
37+
t.equal(opts.message, 'hey alice, I like your attitude')
38+
t.equal(opts.code, 'CODE')
39+
})
40+
41+
test('Create error with 3 parameters', t => {
42+
t.plan(3)
43+
44+
const { create } = build()
45+
const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s %s')
46+
const opts = buildWarnOpts('alice', 'attitude', 'see you')
47+
t.equal(opts.name, 'FastifyWarning')
48+
t.equal(opts.message, 'hey alice, I like your attitude see you')
49+
t.equal(opts.code, 'CODE')
50+
})
51+
52+
test('Should throw when error code has no fastify name', t => {
53+
t.plan(1)
54+
55+
const { create } = build()
56+
57+
t.throws(() => create(), new Error('Warning name must not be empty'))
58+
})
59+
60+
test('Should throw when error has no code', t => {
61+
t.plan(1)
62+
63+
const { create } = build()
64+
65+
t.throws(() => create('name'), new Error('Warning code must not be empty'))
66+
})
67+
68+
test('Should throw when error has no message', t => {
69+
t.plan(1)
70+
71+
const { create } = build()
72+
73+
t.throws(() => create('name', 'code'), new Error('Warning message must not be empty'))
74+
})
75+
76+
test('Should throw if emit is called with unknown code ', t => {
77+
t.plan(1)
78+
79+
const { emit } = build()
80+
81+
t.throws(() => emit('CODE'), new Error('The code \'CODE\' does not exist'))
82+
})
83+
84+
test('Cannot reuse the same code more than once', t => {
85+
t.plan(1)
86+
87+
const { create } = build()
88+
create('FastifyWarning', 'CODE', 'Not available')
89+
90+
t.throws(() => create('FastifyWarning', 'CODE', 'Not available'), new Error("The code 'CODE' already exist"))
91+
})

jest.test.js renamed to test/jest.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global test, expect */
22
'use strict'
33

4-
const build = require('./')
4+
const build = require('..')
55

66
test('works with jest', done => {
77
const { create, emit, emitted } = build()
File renamed without changes.

0 commit comments

Comments
 (0)