|
| 1 | +'use strict'; |
| 2 | +const deprecateOptions = require('../../lib/utils').deprecateOptions; |
| 3 | +const chai = require('chai'); |
| 4 | +const expect = chai.expect; |
| 5 | +const sinonChai = require('sinon-chai'); |
| 6 | +require('mocha-sinon'); |
| 7 | +chai.use(sinonChai); |
| 8 | + |
| 9 | +const makeTestFunction = require('../tools/utils').makeTestFunction; |
| 10 | +const ensureCalledWith = require('../tools/utils').ensureCalledWith; |
| 11 | + |
| 12 | +describe('Deprecation Warnings', function() { |
| 13 | + let messages = []; |
| 14 | + const deprecatedOptions = ['maxScan', 'snapshot', 'fields']; |
| 15 | + const defaultMessage = ' is deprecated and will be removed in a later version.'; |
| 16 | + |
| 17 | + before(function() { |
| 18 | + if (process.emitWarning) { |
| 19 | + process.on('warning', warning => { |
| 20 | + messages.push(warning.message); |
| 21 | + }); |
| 22 | + } |
| 23 | + return; |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(function() { |
| 27 | + this.sinon.stub(console, 'error'); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(function() { |
| 31 | + messages.length = 0; |
| 32 | + }); |
| 33 | + |
| 34 | + describe('Mult functions with same options', function() { |
| 35 | + beforeEach(function() { |
| 36 | + const f1 = makeTestFunction({ |
| 37 | + name: 'f1', |
| 38 | + deprecatedOptions: deprecatedOptions, |
| 39 | + optionsIndex: 0 |
| 40 | + }); |
| 41 | + const f2 = makeTestFunction({ |
| 42 | + name: 'f2', |
| 43 | + deprecatedOptions: deprecatedOptions, |
| 44 | + optionsIndex: 0 |
| 45 | + }); |
| 46 | + f1({ maxScan: 5 }); |
| 47 | + f2({ maxScan: 5 }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('multiple functions with the same deprecated options should both warn', { |
| 51 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 52 | + test: function(done) { |
| 53 | + process.nextTick(() => { |
| 54 | + expect(messages).to.deep.equal([ |
| 55 | + 'f1 option [maxScan]' + defaultMessage, |
| 56 | + 'f2 option [maxScan]' + defaultMessage |
| 57 | + ]); |
| 58 | + expect(messages).to.have.a.lengthOf(2); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it('multiple functions with the same deprecated options should both warn', { |
| 65 | + metadata: { requires: { node: '<6.0.0' } }, |
| 66 | + test: function(done) { |
| 67 | + ensureCalledWith(console.error, [ |
| 68 | + 'f1 option [maxScan]' + defaultMessage, |
| 69 | + 'f2 option [maxScan]' + defaultMessage |
| 70 | + ]); |
| 71 | + expect(console.error).to.have.been.calledTwice; |
| 72 | + done(); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Empty options object', function() { |
| 78 | + beforeEach(function() { |
| 79 | + const f = makeTestFunction({ |
| 80 | + name: 'f', |
| 81 | + deprecatedOptions: deprecatedOptions, |
| 82 | + optionsIndex: 0 |
| 83 | + }); |
| 84 | + f({}); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not warn if empty options object passed in', { |
| 88 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 89 | + test: function(done) { |
| 90 | + process.nextTick(() => { |
| 91 | + expect(messages).to.have.a.lengthOf(0); |
| 92 | + done(); |
| 93 | + }); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('should not warn if empty options object passed in', { |
| 98 | + metadata: { requires: { node: '<6.0.0' } }, |
| 99 | + test: function(done) { |
| 100 | + expect(console.error).to.have.not.been.called; |
| 101 | + done(); |
| 102 | + } |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('Custom Message Handler', function() { |
| 107 | + beforeEach(function() { |
| 108 | + const customMsgHandler = (name, option) => { |
| 109 | + return 'custom msg for function ' + name + ' and option ' + option; |
| 110 | + }; |
| 111 | + |
| 112 | + const f = makeTestFunction({ |
| 113 | + name: 'f', |
| 114 | + deprecatedOptions: deprecatedOptions, |
| 115 | + optionsIndex: 0, |
| 116 | + msgHandler: customMsgHandler |
| 117 | + }); |
| 118 | + |
| 119 | + f({ maxScan: 5, snapshot: true, fields: 'hi' }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should use user-specified message handler', { |
| 123 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 124 | + test: function(done) { |
| 125 | + process.nextTick(() => { |
| 126 | + expect(messages).to.deep.equal([ |
| 127 | + 'custom msg for function f and option maxScan', |
| 128 | + 'custom msg for function f and option snapshot', |
| 129 | + 'custom msg for function f and option fields' |
| 130 | + ]); |
| 131 | + expect(messages).to.have.a.lengthOf(3); |
| 132 | + done(); |
| 133 | + }); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + it('should use user-specified message handler', { |
| 138 | + metadata: { requires: { node: '<6.0.0' } }, |
| 139 | + test: function(done) { |
| 140 | + ensureCalledWith(console.error, [ |
| 141 | + 'custom msg for function f and option maxScan', |
| 142 | + 'custom msg for function f and option snapshot', |
| 143 | + 'custom msg for function f and option fields' |
| 144 | + ]); |
| 145 | + expect(console.error).to.have.been.calledThrice; |
| 146 | + done(); |
| 147 | + } |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('Warn once', function() { |
| 152 | + beforeEach(function() { |
| 153 | + const f = makeTestFunction({ |
| 154 | + name: 'f', |
| 155 | + deprecatedOptions: deprecatedOptions, |
| 156 | + optionsIndex: 0 |
| 157 | + }); |
| 158 | + f({ maxScan: 5, fields: 'hi' }); |
| 159 | + f({ maxScan: 5, fields: 'hi' }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('each function should only warn once per deprecated option', { |
| 163 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 164 | + test: function(done) { |
| 165 | + process.nextTick(() => { |
| 166 | + expect(messages).to.deep.equal([ |
| 167 | + 'f option [maxScan]' + defaultMessage, |
| 168 | + 'f option [fields]' + defaultMessage |
| 169 | + ]); |
| 170 | + expect(messages).to.have.a.lengthOf(2); |
| 171 | + done(); |
| 172 | + }); |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + it('each function should only warn once per deprecated option', { |
| 177 | + metadata: { requires: { node: '<6.0.0' } }, |
| 178 | + test: function(done) { |
| 179 | + ensureCalledWith(console.error, [ |
| 180 | + 'f option [maxScan]' + defaultMessage, |
| 181 | + 'f option [fields]' + defaultMessage |
| 182 | + ]); |
| 183 | + expect(console.error).to.have.been.calledTwice; |
| 184 | + done(); |
| 185 | + } |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('Maintain functionality', function() { |
| 190 | + beforeEach(function() { |
| 191 | + const config = { |
| 192 | + name: 'f', |
| 193 | + deprecatedOptions: ['multiply', 'add'], |
| 194 | + optionsIndex: 0 |
| 195 | + }; |
| 196 | + |
| 197 | + const operateBy2 = (options, num) => { |
| 198 | + if (options.multiply === true) { |
| 199 | + return num * 2; |
| 200 | + } |
| 201 | + if (options.add === true) { |
| 202 | + return num + 2; |
| 203 | + } |
| 204 | + }; |
| 205 | + |
| 206 | + const f = deprecateOptions(config, operateBy2); |
| 207 | + |
| 208 | + const mult = f({ multiply: true }, 5); |
| 209 | + const add = f({ add: true }, 5); |
| 210 | + |
| 211 | + expect(mult).to.equal(10); |
| 212 | + expect(add).to.equal(7); |
| 213 | + }); |
| 214 | + |
| 215 | + it('wrapped functions should maintain original functionality', { |
| 216 | + metadata: { requires: { node: '>=6.0.0' } }, |
| 217 | + test: function(done) { |
| 218 | + process.nextTick(() => { |
| 219 | + expect(messages).to.deep.equal([ |
| 220 | + 'f option [multiply]' + defaultMessage, |
| 221 | + 'f option [add]' + defaultMessage |
| 222 | + ]); |
| 223 | + expect(messages).to.have.a.lengthOf(2); |
| 224 | + done(); |
| 225 | + }); |
| 226 | + } |
| 227 | + }); |
| 228 | + |
| 229 | + it('wrapped functions should maintain original functionality', { |
| 230 | + metadata: { requires: { node: '<6.0.0' } }, |
| 231 | + test: function(done) { |
| 232 | + ensureCalledWith(console.error, [ |
| 233 | + 'f option [multiply]' + defaultMessage, |
| 234 | + 'f option [add]' + defaultMessage |
| 235 | + ]); |
| 236 | + expect(console.error).to.have.been.calledTwice; |
| 237 | + done(); |
| 238 | + } |
| 239 | + }); |
| 240 | + }); |
| 241 | + |
| 242 | + it('optionsIndex pointing to undefined should not error', function(done) { |
| 243 | + const f = makeTestFunction({ |
| 244 | + name: 'f', |
| 245 | + deprecatedOptions: deprecatedOptions, |
| 246 | + optionsIndex: 0 |
| 247 | + }); |
| 248 | + expect(f).to.not.throw(); |
| 249 | + done(); |
| 250 | + }); |
| 251 | + |
| 252 | + it('optionsIndex not pointing to object should not error', function(done) { |
| 253 | + const f = makeTestFunction({ |
| 254 | + name: 'f', |
| 255 | + deprecatedOptions: deprecatedOptions, |
| 256 | + optionsIndex: 0 |
| 257 | + }); |
| 258 | + expect(() => f('not-an-object')).to.not.throw(); |
| 259 | + done(); |
| 260 | + }); |
| 261 | +}); |
0 commit comments