api-deprecations-spec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 'use strict'
  2. const chai = require('chai')
  3. const dirtyChai = require('dirty-chai')
  4. const { deprecations, deprecate } = require('electron')
  5. const { expect } = chai
  6. chai.use(dirtyChai)
  7. describe('deprecations', () => {
  8. beforeEach(() => {
  9. deprecations.setHandler(null)
  10. process.throwDeprecation = true
  11. })
  12. it('allows a deprecation handler function to be specified', () => {
  13. const messages = []
  14. deprecations.setHandler(message => {
  15. messages.push(message)
  16. })
  17. deprecate.log('this is deprecated')
  18. expect(messages).to.deep.equal(['this is deprecated'])
  19. })
  20. it('returns a deprecation handler after one is set', () => {
  21. const messages = []
  22. deprecations.setHandler(message => {
  23. messages.push(message)
  24. })
  25. deprecate.log('this is deprecated')
  26. expect(deprecations.getHandler()).to.be.a('function')
  27. })
  28. it('renames a property', () => {
  29. let msg
  30. deprecations.setHandler(m => { msg = m })
  31. const oldProp = 'dingyOldName'
  32. const newProp = 'shinyNewName'
  33. let value = 0
  34. const o = { [newProp]: value }
  35. expect(o).to.not.have.a.property(oldProp)
  36. expect(o).to.have.a.property(newProp).that.is.a('number')
  37. deprecate.renameProperty(o, oldProp, newProp)
  38. o[oldProp] = ++value
  39. expect(msg).to.be.a('string')
  40. expect(msg).to.include(oldProp)
  41. expect(msg).to.include(newProp)
  42. expect(o).to.have.a.property(newProp).that.is.equal(value)
  43. expect(o).to.have.a.property(oldProp).that.is.equal(value)
  44. })
  45. it('doesn\'t deprecate a property not on an object', () => {
  46. const o = {}
  47. expect(() => {
  48. deprecate.removeProperty(o, 'iDoNotExist')
  49. }).to.throw(/iDoNotExist/)
  50. })
  51. it('deprecates a property of an object', () => {
  52. let msg
  53. deprecations.setHandler(m => { msg = m })
  54. const prop = 'itMustGo'
  55. const o = { [prop]: 0 }
  56. deprecate.removeProperty(o, prop)
  57. const temp = o[prop]
  58. expect(temp).to.equal(0)
  59. expect(msg).to.be.a('string')
  60. expect(msg).to.include(prop)
  61. })
  62. it('warns exactly once when a function is deprecated with no replacement', () => {
  63. let msg
  64. deprecations.setHandler(m => { msg = m })
  65. function oldFn () { return 'hello' }
  66. const deprecatedFn = deprecate.function(oldFn)
  67. deprecatedFn()
  68. expect(msg).to.be.a('string')
  69. expect(msg).to.include('oldFn')
  70. })
  71. it('warns exactly once when a function is deprecated with a replacement', () => {
  72. let msg
  73. deprecations.setHandler(m => { msg = m })
  74. function oldFn () { return 'hello' }
  75. function newFn () { return 'goodbye' }
  76. const deprecatedFn = deprecate.function(oldFn, newFn)
  77. deprecatedFn()
  78. expect(msg).to.be.a('string')
  79. expect(msg).to.include('oldFn')
  80. expect(msg).to.include('newFn')
  81. })
  82. it('warns only once per item', () => {
  83. const messages = []
  84. deprecations.setHandler(message => messages.push(message))
  85. const key = 'foo'
  86. const val = 'bar'
  87. const o = { [key]: val }
  88. deprecate.removeProperty(o, key)
  89. for (let i = 0; i < 3; ++i) {
  90. expect(o[key]).to.equal(val)
  91. expect(messages).to.have.length(1)
  92. }
  93. })
  94. it('warns if deprecated property is already set', () => {
  95. let msg
  96. deprecations.setHandler(m => { msg = m })
  97. const oldProp = 'dingyOldName'
  98. const newProp = 'shinyNewName'
  99. const o = { [oldProp]: 0 }
  100. deprecate.renameProperty(o, oldProp, newProp)
  101. expect(msg).to.be.a('string')
  102. expect(msg).to.include(oldProp)
  103. expect(msg).to.include(newProp)
  104. })
  105. it('throws an exception if no deprecation handler is specified', () => {
  106. expect(() => {
  107. deprecate.log('this is deprecated')
  108. }).to.throw(/this is deprecated/)
  109. })
  110. describe('promisify', () => {
  111. const expected = 'Hello, world!'
  112. let promiseFunc
  113. let warnings
  114. const enableCallbackWarnings = () => {
  115. warnings = []
  116. deprecations.setHandler(warning => warnings.push(warning))
  117. process.enablePromiseAPIs = true
  118. }
  119. beforeEach(() => {
  120. deprecations.setHandler(null)
  121. process.throwDeprecation = true
  122. promiseFunc = param => new Promise((resolve, reject) => resolve(param))
  123. })
  124. it('acts as a pass-through for promise-based invocations', async () => {
  125. enableCallbackWarnings()
  126. promiseFunc = deprecate.promisify(promiseFunc)
  127. const actual = await promiseFunc(expected)
  128. expect(actual).to.equal(expected)
  129. expect(warnings).to.have.lengthOf(0)
  130. })
  131. it('only calls back an error if the callback is called with (err, data)', (done) => {
  132. enableCallbackWarnings()
  133. let erringPromiseFunc = () => new Promise((resolve, reject) => {
  134. reject(new Error('fail'))
  135. })
  136. erringPromiseFunc = deprecate.promisify(erringPromiseFunc)
  137. erringPromiseFunc((err, data) => {
  138. expect(data).to.be.an('undefined')
  139. expect(err).to.be.an.instanceOf(Error).with.property('message', 'fail')
  140. erringPromiseFunc(data => {
  141. expect(data).to.not.be.an.instanceOf(Error)
  142. expect(data).to.be.an('undefined')
  143. done()
  144. })
  145. })
  146. })
  147. it('warns exactly once for callback-based invocations', (done) => {
  148. enableCallbackWarnings()
  149. promiseFunc = deprecate.promisify(promiseFunc)
  150. let callbackCount = 0
  151. const invocationCount = 3
  152. const callback = (error, actual) => {
  153. expect(error).to.be.null()
  154. expect(actual).to.equal(expected)
  155. expect(warnings).to.have.lengthOf(1)
  156. expect(warnings[0]).to.include('promiseFunc')
  157. callbackCount += 1
  158. if (callbackCount === invocationCount) {
  159. done()
  160. }
  161. }
  162. for (let i = 0; i < invocationCount; i += 1) {
  163. promiseFunc(expected, callback)
  164. }
  165. })
  166. })
  167. })