2019-09-04 21:13:37 +03:00
|
|
|
import { expect } from 'chai';
|
2024-10-03 07:36:15 +03:00
|
|
|
|
2023-10-10 18:50:47 +03:00
|
|
|
import * as deprecate from '../lib/common/deprecate';
|
2018-06-19 18:22:08 +03:00
|
|
|
|
2019-05-21 01:40:49 +03:00
|
|
|
describe('deprecate', () => {
|
2020-01-30 23:12:35 +03:00
|
|
|
let throwing: boolean;
|
|
|
|
|
2017-10-27 03:35:33 +03:00
|
|
|
beforeEach(() => {
|
2020-01-30 23:12:35 +03:00
|
|
|
throwing = process.throwDeprecation;
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(null);
|
2016-03-25 23:03:49 +03:00
|
|
|
process.throwDeprecation = true;
|
|
|
|
});
|
2016-02-17 02:09:35 +03:00
|
|
|
|
2020-01-30 23:12:35 +03:00
|
|
|
afterEach(() => {
|
|
|
|
process.throwDeprecation = throwing;
|
|
|
|
});
|
|
|
|
|
2017-11-14 22:56:16 +03:00
|
|
|
it('allows a deprecation handler function to be specified', () => {
|
2019-09-04 21:13:37 +03:00
|
|
|
const messages: string[] = [];
|
2017-11-14 04:21:57 +03:00
|
|
|
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(message => {
|
2017-11-14 22:56:16 +03:00
|
|
|
messages.push(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
deprecate.log('this is deprecated');
|
2018-06-19 18:22:08 +03:00
|
|
|
expect(messages).to.deep.equal(['this is deprecated']);
|
2017-11-14 22:56:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a deprecation handler after one is set', () => {
|
|
|
|
const messages = [];
|
|
|
|
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(message => {
|
2017-11-14 22:56:16 +03:00
|
|
|
messages.push(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
deprecate.log('this is deprecated');
|
2019-05-21 01:40:49 +03:00
|
|
|
expect(deprecate.getHandler()).to.be.a('function');
|
2017-11-14 22:56:16 +03:00
|
|
|
});
|
|
|
|
|
2018-05-29 16:40:48 +03:00
|
|
|
it('renames a property', () => {
|
|
|
|
let msg;
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(m => { msg = m; });
|
2018-05-29 16:40:48 +03:00
|
|
|
|
2018-09-11 21:16:49 +03:00
|
|
|
const oldProp = 'dingyOldName';
|
|
|
|
const newProp = 'shinyNewName';
|
2018-05-29 16:40:48 +03:00
|
|
|
|
|
|
|
let value = 0;
|
2019-09-04 21:13:37 +03:00
|
|
|
const o: Record<string, number> = { [newProp]: value };
|
|
|
|
expect(o).to.not.have.property(oldProp);
|
|
|
|
expect(o).to.have.property(newProp).that.is.a('number');
|
2018-05-29 16:40:48 +03:00
|
|
|
|
2018-09-11 21:16:49 +03:00
|
|
|
deprecate.renameProperty(o, oldProp, newProp);
|
|
|
|
o[oldProp] = ++value;
|
2018-05-29 16:40:48 +03:00
|
|
|
|
2018-06-19 18:22:08 +03:00
|
|
|
expect(msg).to.be.a('string');
|
2018-09-11 21:16:49 +03:00
|
|
|
expect(msg).to.include(oldProp);
|
|
|
|
expect(msg).to.include(newProp);
|
2018-05-29 16:40:48 +03:00
|
|
|
|
2019-09-04 21:13:37 +03:00
|
|
|
expect(o).to.have.property(newProp).that.is.equal(value);
|
|
|
|
expect(o).to.have.property(oldProp).that.is.equal(value);
|
2018-09-11 21:16:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('doesn\'t deprecate a property not on an object', () => {
|
2019-09-04 21:13:37 +03:00
|
|
|
const o: any = {};
|
2018-09-11 21:16:49 +03:00
|
|
|
|
|
|
|
expect(() => {
|
2018-09-13 01:13:22 +03:00
|
|
|
deprecate.removeProperty(o, 'iDoNotExist');
|
|
|
|
}).to.throw(/iDoNotExist/);
|
2018-05-29 16:40:48 +03:00
|
|
|
});
|
|
|
|
|
2018-06-27 09:47:01 +03:00
|
|
|
it('deprecates a property of an object', () => {
|
|
|
|
let msg;
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(m => { msg = m; });
|
2018-06-27 09:47:01 +03:00
|
|
|
|
2018-09-11 21:16:49 +03:00
|
|
|
const prop = 'itMustGo';
|
2018-10-02 04:56:31 +03:00
|
|
|
const o = { [prop]: 0 };
|
2018-09-11 21:16:49 +03:00
|
|
|
|
|
|
|
deprecate.removeProperty(o, prop);
|
2018-06-27 09:47:01 +03:00
|
|
|
|
2018-09-11 21:16:49 +03:00
|
|
|
const temp = o[prop];
|
2018-06-27 09:47:01 +03:00
|
|
|
|
2018-09-11 21:16:49 +03:00
|
|
|
expect(temp).to.equal(0);
|
2018-06-27 09:47:01 +03:00
|
|
|
expect(msg).to.be.a('string');
|
2018-09-11 21:16:49 +03:00
|
|
|
expect(msg).to.include(prop);
|
2018-06-27 09:47:01 +03:00
|
|
|
});
|
|
|
|
|
2020-02-27 01:03:52 +03:00
|
|
|
it('deprecates a property of an but retains the existing accessors and setters', () => {
|
|
|
|
let msg;
|
|
|
|
deprecate.setHandler(m => { msg = m; });
|
|
|
|
|
|
|
|
const prop = 'itMustGo';
|
|
|
|
let i = 1;
|
|
|
|
const o = {
|
|
|
|
get itMustGo () {
|
|
|
|
return i;
|
|
|
|
},
|
|
|
|
set itMustGo (thing) {
|
|
|
|
i = thing + 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
deprecate.removeProperty(o, prop);
|
|
|
|
|
|
|
|
expect(o[prop]).to.equal(1);
|
|
|
|
expect(msg).to.be.a('string');
|
|
|
|
expect(msg).to.include(prop);
|
|
|
|
o[prop] = 2;
|
|
|
|
expect(o[prop]).to.equal(3);
|
|
|
|
});
|
|
|
|
|
2019-02-08 20:50:11 +03:00
|
|
|
it('warns exactly once when a function is deprecated with no replacement', () => {
|
|
|
|
let msg;
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(m => { msg = m; });
|
2019-02-08 20:50:11 +03:00
|
|
|
|
|
|
|
function oldFn () { return 'hello'; }
|
2019-07-09 04:01:35 +03:00
|
|
|
const deprecatedFn = deprecate.removeFunction(oldFn, 'oldFn');
|
2019-02-08 20:50:11 +03:00
|
|
|
deprecatedFn();
|
|
|
|
|
|
|
|
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;
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(m => { msg = m; });
|
2019-02-08 20:50:11 +03:00
|
|
|
|
|
|
|
function oldFn () { return 'hello'; }
|
2020-10-08 04:01:23 +03:00
|
|
|
const deprecatedFn = deprecate.renameFunction(oldFn, 'newFn');
|
2019-02-08 20:50:11 +03:00
|
|
|
deprecatedFn();
|
|
|
|
|
|
|
|
expect(msg).to.be.a('string');
|
|
|
|
expect(msg).to.include('oldFn');
|
|
|
|
expect(msg).to.include('newFn');
|
|
|
|
});
|
|
|
|
|
2018-09-13 01:13:22 +03:00
|
|
|
it('warns only once per item', () => {
|
2019-09-04 21:13:37 +03:00
|
|
|
const messages: string[] = [];
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(message => messages.push(message));
|
2018-09-13 01:13:22 +03:00
|
|
|
|
|
|
|
const key = 'foo';
|
|
|
|
const val = 'bar';
|
2018-10-02 04:56:31 +03:00
|
|
|
const o = { [key]: val };
|
2018-09-13 01:13:22 +03:00
|
|
|
deprecate.removeProperty(o, key);
|
|
|
|
|
|
|
|
for (let i = 0; i < 3; ++i) {
|
|
|
|
expect(o[key]).to.equal(val);
|
|
|
|
expect(messages).to.have.length(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-29 16:58:02 +03:00
|
|
|
it('warns if deprecated property is already set', () => {
|
|
|
|
let msg;
|
2019-05-21 01:40:49 +03:00
|
|
|
deprecate.setHandler(m => { msg = m; });
|
2018-05-29 16:58:02 +03:00
|
|
|
|
2018-09-11 21:16:49 +03:00
|
|
|
const oldProp = 'dingyOldName';
|
|
|
|
const newProp = 'shinyNewName';
|
2018-05-29 16:58:02 +03:00
|
|
|
|
2019-09-04 21:13:37 +03:00
|
|
|
const o: Record<string, number> = { [oldProp]: 0 };
|
2018-09-11 21:16:49 +03:00
|
|
|
deprecate.renameProperty(o, oldProp, newProp);
|
2018-05-29 16:58:02 +03:00
|
|
|
|
2018-06-19 18:22:08 +03:00
|
|
|
expect(msg).to.be.a('string');
|
2018-09-11 21:16:49 +03:00
|
|
|
expect(msg).to.include(oldProp);
|
|
|
|
expect(msg).to.include(newProp);
|
2018-05-29 16:58:02 +03:00
|
|
|
});
|
|
|
|
|
2017-11-14 22:56:16 +03:00
|
|
|
it('throws an exception if no deprecation handler is specified', () => {
|
2018-06-19 18:22:08 +03:00
|
|
|
expect(() => {
|
2017-11-14 22:56:16 +03:00
|
|
|
deprecate.log('this is deprecated');
|
2018-06-19 18:22:08 +03:00
|
|
|
}).to.throw(/this is deprecated/);
|
2017-11-14 22:56:16 +03:00
|
|
|
});
|
2018-12-10 19:13:09 +03:00
|
|
|
|
2019-08-14 23:42:55 +03:00
|
|
|
describe('moveAPI', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
deprecate.setHandler(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call the original method', () => {
|
|
|
|
const warnings = [];
|
|
|
|
deprecate.setHandler(warning => warnings.push(warning));
|
|
|
|
|
|
|
|
let called = false;
|
|
|
|
const fn = () => {
|
|
|
|
called = true;
|
|
|
|
};
|
|
|
|
const deprecated = deprecate.moveAPI(fn, 'old', 'new');
|
|
|
|
deprecated();
|
|
|
|
expect(called).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should log the deprecation warning once', () => {
|
2019-09-04 21:13:37 +03:00
|
|
|
const warnings: string[] = [];
|
2019-08-14 23:42:55 +03:00
|
|
|
deprecate.setHandler(warning => warnings.push(warning));
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2019-08-14 23:42:55 +03:00
|
|
|
const deprecated = deprecate.moveAPI(() => null, 'old', 'new');
|
|
|
|
deprecated();
|
|
|
|
expect(warnings).to.have.lengthOf(1);
|
|
|
|
deprecated();
|
|
|
|
expect(warnings).to.have.lengthOf(1);
|
|
|
|
expect(warnings[0]).to.equal('\'old\' is deprecated and will be removed. Please use \'new\' instead.');
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|