diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 325921f..079a8f0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,3 +15,4 @@ jobs: uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 with: license-check: true + lint: true diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.taprc b/.taprc new file mode 100644 index 0000000..aeac425 --- /dev/null +++ b/.taprc @@ -0,0 +1,2 @@ +files: + - test/**/*[!jest].test.js diff --git a/package.json b/package.json index 074aa44..dd7886a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/test.js b/test.js deleted file mode 100644 index 392aa1d..0000000 --- a/test.js +++ /dev/null @@ -1,122 +0,0 @@ -'use strict' - -const test = require('ava') -const build = require('./') - -process.removeAllListeners('warning') - -test('Create warning with zero parameter', t => { - const { create } = build() - const buildWarnOpts = create('FastifyWarning', 'CODE', 'Not available') - const opts = buildWarnOpts() - t.is(opts.name, 'FastifyWarning') - t.is(opts.message, 'Not available') - t.is(opts.code, 'CODE') -}) - -test('Create error with 1 parameter', t => { - const { create } = build() - const buildWarningOpts = create('FastifyWarning', 'CODE', 'hey %s') - const opts = buildWarningOpts('alice') - t.is(opts.name, 'FastifyWarning') - t.is(opts.message, 'hey alice') - t.is(opts.code, 'CODE') -}) - -test('Create error with 2 parameters', t => { - const { create } = build() - const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s') - const opts = buildWarnOpts('alice', 'attitude') - t.is(opts.name, 'FastifyWarning') - t.is(opts.message, 'hey alice, I like your attitude') - t.is(opts.code, 'CODE') -}) - -test('Create error with 3 parameters', t => { - const { create } = build() - const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s %s') - const opts = buildWarnOpts('alice', 'attitude', 'see you') - t.is(opts.name, 'FastifyWarning') - t.is(opts.message, 'hey alice, I like your attitude see you') - t.is(opts.code, 'CODE') -}) - -test('Should throw when error code has no fastify name', t => { - const { create } = build() - try { - create() - } catch (err) { - t.is(err.message, 'Warning name must not be empty') - } -}) - -test('Should throw when error has no code', t => { - const { create } = build() - try { - create('name') - } catch (err) { - t.is(err.message, 'Warning code must not be empty') - } -}) - -test('Should throw when error has no message', t => { - const { create } = build() - try { - create('name', 'code') - } catch (err) { - t.is(err.message, 'Warning message must not be empty') - } -}) - -test.serial.cb('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.is(warning.name, 'FastifyDeprecation') - t.is(warning.code, 'CODE') - t.is(warning.message, 'Hello world') - t.true(emitted.get('CODE')) - } - - create('FastifyDeprecation', 'CODE', 'Hello world') - emit('CODE') - emit('CODE') - setImmediate(() => { - process.removeListener('warning', onWarning) - t.end() - }) -}) - -test.serial.cb('emit with interpolated string', t => { - t.plan(4) - const { create, emit, emitted } = build() - - process.on('warning', onWarning) - function onWarning (warning) { - t.is(warning.name, 'FastifyDeprecation') - t.is(warning.code, 'CODE') - t.is(warning.message, 'Hello world') - t.true(emitted.get('CODE')) - } - - create('FastifyDeprecation', 'CODE', 'Hello %s') - emit('CODE', 'world') - emit('CODE', 'world') - - setImmediate(() => { - process.removeListener('warning', onWarning) - t.end() - }) -}) - -test('Cannot reuse the same code more than once', t => { - const { create } = build() - create('FastifyWarning', 'CODE', 'Not available') - try { - create('FastifyWarning', 'CODE', 'Not available') - } catch (err) { - t.is(err.message, "The code 'CODE' already exist") - } -}) diff --git a/test/emit-interpolated-string.test.js b/test/emit-interpolated-string.test.js new file mode 100644 index 0000000..c282a3c --- /dev/null +++ b/test/emit-interpolated-string.test.js @@ -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() + }) +}) diff --git a/test/emit-once-only.test.js b/test/emit-once-only.test.js new file mode 100644 index 0000000..e1d8a30 --- /dev/null +++ b/test/emit-once-only.test.js @@ -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() + }) +}) diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..b00b478 --- /dev/null +++ b/test/index.test.js @@ -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")) +}) diff --git a/jest.test.js b/test/jest.test.js similarity index 94% rename from jest.test.js rename to test/jest.test.js index ea8430c..b02660d 100644 --- a/jest.test.js +++ b/test/jest.test.js @@ -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() diff --git a/index.d.ts b/types/index.d.ts similarity index 100% rename from index.d.ts rename to types/index.d.ts diff --git a/index.test-d.ts b/types/index.test-d.ts similarity index 88% rename from index.test-d.ts rename to types/index.test-d.ts index d25ef57..a225b5c 100644 --- a/index.test-d.ts +++ b/types/index.test-d.ts @@ -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')