Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Feb 6, 2019
1 parent 1ed276f commit d8b5dce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
6 changes: 2 additions & 4 deletions lib/common/api/deprecate.js
Expand Up @@ -36,12 +36,10 @@ const deprecate = {
},

function: (fn, newName) => {
// if newName is left blank, a removal warning will be displayed
const warn = warnOnce(fn.name, newName)

return function () {
if (!process.noDeprecation) warn()
return fn.apply(this, arguments)
warn()
fn.apply(this, arguments)
}
},

Expand Down
32 changes: 29 additions & 3 deletions spec/api-deprecations-spec.js
Expand Up @@ -82,9 +82,35 @@ describe('deprecations', () => {
expect(msg).to.include(prop)
})

it('warns exactly once when a function is deprecated with no replacement', () => {
let msg
deprecations.setHandler(m => { msg = m })

function oldFn () { return 'hello' }
deprecate.function(oldFn)
oldFn()

expect(msg).to.be.a('string')
expect(msg).to.include(oldFn)
})

it('warns exactly once when a function is deprecated with a replacement', () => {
let msg
deprecations.setHandler(m => { msg = m })

function oldFn () { return 'hello' }
function newFn () { return 'goodbye' }
deprecate.function(oldFn)
oldFn()

expect(msg).to.be.a('string')
expect(msg).to.include(oldFn)
expect(msg).to.include(newFn)
})

it('warns only once per item', () => {
const messages = []
deprecations.setHandler(message => { messages.push(message) })
deprecations.setHandler(message => messages.push(message))

const key = 'foo'
const val = 'bar'
Expand Down Expand Up @@ -125,15 +151,15 @@ describe('deprecations', () => {

const enableCallbackWarnings = () => {
warnings = []
deprecations.setHandler(warning => { warnings.push(warning) })
deprecations.setHandler(warning => warnings.push(warning))
process.enablePromiseAPIs = true
}

beforeEach(() => {
deprecations.setHandler(null)
process.throwDeprecation = true

promiseFunc = param => new Promise((resolve, reject) => { resolve(param) })
promiseFunc = param => new Promise((resolve, reject) => resolve(param))
})

it('acts as a pass-through for promise-based invocations', async () => {
Expand Down

0 comments on commit d8b5dce

Please sign in to comment.