Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make emit work in jest #3

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
declare function warning (): Warning

export declare class FastifyWarning extends Error {
code: string
export declare class WarnOpts {
code: string;
name: string;
message: string;
}

export type BuildWarnOptsFn = (a?: any, b?: any, c?: any) => WarnOpts

interface Warning {
create(name: string, code: string, message: string): FastifyWarning,
create(name: string, code: string, message: string): BuildWarnOptsFn,
emit(cod: string, a?: any, b?: any, c?: any): void,
emitted: Map<string, boolean>
}
Expand Down
37 changes: 16 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { inherits, format } = require('util')
const { format } = require('util')

function build () {
const codes = {}
Expand All @@ -17,35 +17,28 @@ function build () {
throw new Error(`The code '${code}' already exist`)
}

function FastifyWarning (a, b, c) {
if (!(this instanceof FastifyWarning)) {
return new FastifyWarning(a, b, c)
}
Error.captureStackTrace(this, FastifyWarning)
this.name = name
this.code = code

function buildWarnOpts (a, b, c) {
// more performant than spread (...) operator
let formatted
if (a && b && c) {
this.message = format(message, a, b, c)
formatted = format(message, a, b, c)
} else if (a && b) {
this.message = format(message, a, b)
formatted = format(message, a, b)
} else if (a) {
this.message = format(message, a)
formatted = format(message, a)
} else {
this.message = message
formatted = message
}
}
FastifyWarning.prototype[Symbol.toStringTag] = 'Warning'

FastifyWarning.prototype.toString = function () {
return `${this.name} [${this.code}]: ${this.message}`
return {
code,
name,
message: formatted
}
}

inherits(FastifyWarning, Error)

emitted.set(code, false)
codes[code] = FastifyWarning
codes[code] = buildWarnOpts

return codes[code]
}
Expand All @@ -54,7 +47,9 @@ function build () {
if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`)
if (emitted.get(code) === true) return
emitted.set(code, true)
process.emitWarning(new codes[code](a, b, c))

const warning = codes[code](a, b, c)
process.emitWarning(warning.message, warning.name, warning.code)
}

return {
Expand Down
13 changes: 8 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { expectType } from 'tsd'
import Warinig, { FastifyWarning } from './'
import Warinig, { BuildWarnOptsFn, WarnOpts } from './'

const warning = Warinig()
const warn = warning.create('FastifyWarning', 'CODE', 'message')
expectType<FastifyWarning>(warn)
expectType<string>(warn.code)
expectType<string>(warn.message)
const buildWarnOpts = warning.create('FastifyWarning', 'CODE', 'message')
expectType<BuildWarnOptsFn>(buildWarnOpts)
const opts = buildWarnOpts()
expectType<WarnOpts>(opts)
expectType<string>(opts.code)
expectType<string>(opts.message)
expectType<string>(opts.name)

expectType<void>(warning.emit('CODE'))
expectType<Map<string, boolean>>(warning.emitted)
20 changes: 20 additions & 0 deletions jest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* global test, expect */
'use strict'

const build = require('./')

test('works with jest', done => {
const { create, emit, emitted } = build()

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

// we cannot actually listen to process warning event
// because jest messes with it (that's the point of this test)
// we can only test it was emitted indirectly
// and test no exception is raised
setImmediate(() => {
expect(emitted.get('CODE')).toBeTruthy()
done()
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "standard && ava -v && tsd"
"test": "standard && ava -v test.js && jest jest.test.js && tsd"
},
"repository": {
"type": "git",
Expand All @@ -28,6 +28,7 @@
"homepage": "https://github.com/fastify/fastify-warning#readme",
"devDependencies": {
"ava": "^3.10.1",
"jest": "^26.1.0",
"standard": "^14.3.4",
"tsd": "^0.13.1"
}
Expand Down
61 changes: 20 additions & 41 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,38 @@ process.removeAllListeners('warning')

test('Create warning with zero parameter', t => {
const { create } = build()
const NewWarn = create('FastifyWarning', 'CODE', 'Not available')
const err = new NewWarn()
t.true(err instanceof Error)
t.is(err.name, 'FastifyWarning')
t.is(err.message, 'Not available')
t.is(err.code, 'CODE')
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 NewWarn = create('FastifyWarning', 'CODE', 'hey %s')
const err = new NewWarn('alice')
t.true(err instanceof Error)
t.is(err.name, 'FastifyWarning')
t.is(err.message, 'hey alice')
t.is(err.code, 'CODE')
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 NewWarn = create('FastifyWarning', 'CODE', 'hey %s, I like your %s')
const err = new NewWarn('alice', 'attitude')
t.true(err instanceof Error)
t.is(err.name, 'FastifyWarning')
t.is(err.message, 'hey alice, I like your attitude')
t.is(err.code, 'CODE')
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 NewWarn = create('FastifyWarning', 'CODE', 'hey %s, I like your %s %s')
const err = new NewWarn('alice', 'attitude', 'see you')
t.true(err instanceof Error)
t.is(err.name, 'FastifyWarning')
t.is(err.message, 'hey alice, I like your attitude see you')
t.is(err.code, 'CODE')
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 => {
Expand Down Expand Up @@ -72,23 +68,6 @@ test('Should throw when error has no message', t => {
}
})

test('FastifyWarning.toString returns code', t => {
const { create } = build()
const NewWarn = create('FastifyWarning', 'CODE', 'foo')
const err = new NewWarn()
t.is(err.toString(), 'FastifyWarning [CODE]: foo')
})

test('Create the warning without the new keyword', t => {
const { create } = build()
const NewWarn = create('FastifyWarning', 'CODE', 'Not available')
const err = NewWarn()
t.true(err instanceof Error)
t.is(err.name, 'FastifyWarning')
t.is(err.message, 'Not available')
t.is(err.code, 'CODE')
})

test.serial.cb('emit should emit a given code only once', t => {
t.plan(4)
const { create, emit, emitted } = build()
Expand Down