|
1 | 1 | /* eslint-disable import/no-named-as-default-member */ |
2 | 2 | importassertfrom'node:assert'; |
3 | 3 | importchalkfrom'chalk'; |
4 | | -importsinonfrom'sinon'; |
| 4 | +importsinon,{typeSinonSpy}from'sinon'; |
5 | 5 | importdeprecatefrom'../src/util/deprecate.js'; |
6 | 6 |
|
| 7 | +typeSimpleObject={ |
| 8 | +foo:number; |
| 9 | +functionInObj:(someValue:number)=>number; |
| 10 | +}; |
| 11 | + |
7 | 12 | describe('deprecate()',()=>{ |
| 13 | +letfakeConsoleLog:SinonSpy; |
8 | 14 | beforeEach(()=>{ |
9 | | -sinon.spy(console,'log'); |
| 15 | +fakeConsoleLog=sinon.fake(); |
| 16 | +sinon.replace(console,'log',fakeConsoleLog); |
10 | 17 | }); |
11 | 18 |
|
12 | 19 | afterEach(()=>{ |
13 | | -console.log.restore(); |
| 20 | +sinon.restore(); |
14 | 21 | }); |
15 | 22 |
|
16 | | -it('log a message',()=>{ |
17 | | -constfunc=sinon.spy(); |
18 | | -constwrapped=deprecate('foo',func); |
19 | | -sinon.assert.notCalled(console.log); |
20 | | -wrapped('bar',2); |
21 | | -sinon.assert.calledWith(console.log,chalk.yellow('(!) ')+'foo'); |
22 | | -sinon.assert.calledWith(func,'bar',2); |
| 23 | +describe('deprecate a function',()=>{ |
| 24 | +letdeprecatedLogSpy:SinonSpy; |
| 25 | +beforeEach(()=>{ |
| 26 | +deprecatedLogSpy=sinon.fake(); |
| 27 | +sinon.replace(deprecate,'log',deprecatedLogSpy); |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(()=>{ |
| 31 | +sinon.restore(); |
| 32 | +}); |
| 33 | + |
| 34 | +it('the original function is still called',()=>{ |
| 35 | +constoriginalFunction=sinon.spy(); |
| 36 | +constdeprecatedFunction=deprecate('this is function deprecated',originalFunction); |
| 37 | + |
| 38 | +deprecatedFunction('baz',3); |
| 39 | +assert.ok( |
| 40 | +originalFunction.calledWith('baz',3), |
| 41 | +`original function called with (${originalFunction.lastCall.args[0]},${originalFunction.lastCall.args[1]})`, |
| 42 | +); |
| 43 | +}); |
| 44 | + |
| 45 | +it('a call to deprecate.log(msg) is added',()=>{ |
| 46 | +constoriginalFunction=sinon.spy(); |
| 47 | +constdeprecatedFunction=deprecate('this is function deprecated',originalFunction); |
| 48 | + |
| 49 | +originalFunction('bar',2); |
| 50 | +assert.ok(originalFunction.calledWith('bar',2),'original function not called with ("bar", 2)'); |
| 51 | +assert.ok(deprecatedLogSpy.notCalled); |
| 52 | + |
| 53 | +deprecatedFunction('baz',3); |
| 54 | +assert.ok(deprecatedLogSpy.calledWith('this is function deprecated')); |
| 55 | +}); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('.log',()=>{ |
| 59 | +it('logs the message in yellow, starting with "(!) "',()=>{ |
| 60 | +deprecate.log('this is the message'); |
| 61 | +assert.ok(fakeConsoleLog.calledWith(chalk.yellow('(!) ')+'this is the message')); |
| 62 | +}); |
23 | 63 | }); |
24 | 64 |
|
25 | 65 | describe('.object()',()=>{ |
26 | | -it('wrap an object and log a message',()=>{ |
27 | | -constdummy={ |
| 66 | +letdeprecatedLogSpy:SinonSpy; |
| 67 | +beforeEach(()=>{ |
| 68 | +deprecatedLogSpy=sinon.fake(); |
| 69 | +sinon.replace(deprecate,'log',deprecatedLogSpy); |
| 70 | +}); |
| 71 | + |
| 72 | +afterEach(()=>{ |
| 73 | +sinon.restore(); |
| 74 | +}); |
| 75 | + |
| 76 | +it('deprecates all functions/methods in the object',()=>{ |
| 77 | +typeSomeOtherObject=SimpleObject&{ |
| 78 | +anotherFunction:(someValue:string)=>string; |
| 79 | +}; |
| 80 | +constoriginalObject:SomeOtherObject={ |
28 | 81 | foo:1, |
29 | | -func:sinon.spy(), |
| 82 | +functionInObj(someValue:number):number{ |
| 83 | +returnsomeValue-1; |
| 84 | +}, |
| 85 | +anotherFunction(someValue:string):string{ |
| 86 | +return`${someValue} and${someValue}`; |
| 87 | +}, |
30 | 88 | }; |
31 | | -constwrapped=deprecate.object('<%= name %> is deprecated',dummy); |
32 | 89 |
|
33 | | -// Keep values |
34 | | -assert.equal(wrapped.foo,1); |
| 90 | +// TODO When deprecate.js is changed to .ts, DeprecatedObject will be defined in deprecate.ts as something like type DeprecatedObject<O> = O; |
| 91 | +constdeprecatedObject=deprecate.object('<%= name %> is deprecated',originalObject); |
| 92 | +//@ts-expect-error The method functionInObj() does exist on deprecatedObject. This should be a DeprecatedObject<SimpleObject>. When deprecate.js is changed to .ts, this can be implemented and no error will occur here. |
| 93 | +deprecatedObject.functionInObj(42); |
| 94 | +assert.ok( |
| 95 | +deprecatedLogSpy.calledWith('functionInObj is deprecated'), |
| 96 | +`last call with args:${deprecatedLogSpy.lastCall.args[0]}`, |
| 97 | +); |
35 | 98 |
|
36 | | -// Wrap methods |
37 | | -wrapped.func(2); |
38 | | -sinon.assert.calledWith(dummy.func,2); |
39 | | -sinon.assert.calledWith(console.log,chalk.yellow('(!) ')+'func is deprecated'); |
| 99 | +//@ts-expect-error The method anotherFunction() does exist on deprecatedObject. This should be a DeprecatedObject<SimpleObject>. When deprecate.js is changed to .ts, this can be implemented and no error will occur here. |
| 100 | +deprecatedObject.anotherFunction('something'); |
| 101 | +assert.ok( |
| 102 | +deprecatedLogSpy.calledWith('anotherFunction is deprecated'), |
| 103 | +`last call with args:${deprecatedLogSpy.lastCall.args[0]}`, |
| 104 | +); |
| 105 | +}); |
| 106 | + |
| 107 | +it('properties that are not functions are not changed',()=>{ |
| 108 | +constoriginalObject:SimpleObject={ |
| 109 | +foo:1, |
| 110 | +functionInObj(someValue:number):number{ |
| 111 | +returnsomeValue-1; |
| 112 | +}, |
| 113 | +}; |
| 114 | + |
| 115 | +constdeprecatedObject=deprecate.object('The function "<%= name %>" is deprecated',originalObject); |
| 116 | +//@ts-expect-error The property foo does exist on deprecatedObject. This should be a DeprecatedObject<SimpleObject>. When deprecate.js is changed to .ts, this can be implemented and no error will occur here. |
| 117 | +constfooValue=deprecatedObject.foo; |
| 118 | +assert.equal(fooValue,1); |
| 119 | +assert.ok(deprecatedLogSpy.notCalled); |
| 120 | +}); |
| 121 | + |
| 122 | +it('property getters and setters are not changed',()=>{ |
| 123 | +typeObjectWithGettersSetters=SimpleObject&{ |
| 124 | +getbar():number; |
| 125 | +setbar(someValue:number); |
| 126 | +}; |
| 127 | + |
| 128 | +constoriginalObject:ObjectWithGettersSetters={ |
| 129 | +foo:10, |
| 130 | +functionInObj(someValue:number):number{ |
| 131 | +returnsomeValue-1; |
| 132 | +}, |
| 133 | +getbar():number{ |
| 134 | +returnthis.foo*2; |
| 135 | +}, |
| 136 | +setbar(someValue:number){ |
| 137 | +this.foo=someValue/2; |
| 138 | +}, |
| 139 | +}; |
| 140 | + |
| 141 | +constdeprecatedObject=deprecate.object('The function "<%= name %>" is deprecated',originalObject); |
| 142 | +//@ts-expect-error The getter bar does exist on the object. This should be a DeprecatedObject<SimpleObject>. When deprecate.js is changed to .ts, this can be implemented and no error will occur here. |
| 143 | +deprecatedObject.bar; |
| 144 | +//@ts-expect-error The setter bar does exist on the object. This should be a DeprecatedObject<SimpleObject>. When deprecate.js is changed to .ts, this can be implemented and no error will occur here. |
| 145 | +deprecatedObject.bar=7; |
| 146 | + |
| 147 | +assert.ok(deprecatedLogSpy.notCalled); |
| 148 | +}); |
| 149 | + |
| 150 | +it('deprecation message can be a template',()=>{ |
| 151 | +constoriginalObject:SimpleObject={ |
| 152 | +foo:1, |
| 153 | +functionInObj(someValue:number):number{ |
| 154 | +returnsomeValue-1; |
| 155 | +}, |
| 156 | +}; |
| 157 | + |
| 158 | +constdeprecatedObject=deprecate.object('The function "<%= name %>" is deprecated',originalObject); |
| 159 | + |
| 160 | +//@ts-expect-error The method functionInObj() does exist on deprecatedObject. This should be a DeprecatedObject<SimpleObject>. When deprecate.js is changed to .ts, this can be implemented and no error will occur here. |
| 161 | +deprecatedObject.functionInObj(42); |
| 162 | + |
| 163 | +assert.ok( |
| 164 | +deprecatedLogSpy.calledWith('The function "functionInObj" is deprecated'), |
| 165 | +`last call with args:${deprecatedLogSpy.lastCall.args[0]}`, |
| 166 | +); |
40 | 167 | }); |
41 | 168 | }); |
42 | 169 |
|
43 | 170 | describe('.property()',()=>{ |
44 | | -it('wrap a property getter and log a message',()=>{ |
45 | | -constdummy={ |
| 171 | +letdeprecatedLogSpy:SinonSpy; |
| 172 | +beforeEach(()=>{ |
| 173 | +deprecatedLogSpy=sinon.fake(); |
| 174 | +sinon.replace(deprecate,'log',deprecatedLogSpy); |
| 175 | +}); |
| 176 | + |
| 177 | +afterEach(()=>{ |
| 178 | +sinon.restore(); |
| 179 | +}); |
| 180 | + |
| 181 | +it('the deprecated message shows when a property is accessed',()=>{ |
| 182 | +constoriginalObject={ |
46 | 183 | foo:1, |
47 | 184 | }; |
48 | | -deprecate.property('foo is deprecated',dummy,'foo'); |
| 185 | +deprecate.property('foo property is deprecated',originalObject,'foo'); |
| 186 | + |
| 187 | +// Value is not affected; it remains the same |
| 188 | +assert.equal(originalObject.foo,1); |
| 189 | + |
| 190 | +assert.ok( |
| 191 | +deprecatedLogSpy.calledWith('foo property is deprecated'), |
| 192 | +`deprecatedLogSpy called with (${deprecatedLogSpy.lastCall.args[0]})`, |
| 193 | +); |
| 194 | +}); |
| 195 | + |
| 196 | +it('property getters and setters are deprecated',()=>{ |
| 197 | +typeObjectWithGettersSetters=SimpleObject&{ |
| 198 | +getbar():number; |
| 199 | +setbar(someValue:number); |
| 200 | +}; |
| 201 | + |
| 202 | +constoriginalObject:ObjectWithGettersSetters={ |
| 203 | +foo:10, |
| 204 | +functionInObj(someValue:number):number{ |
| 205 | +returnsomeValue-1; |
| 206 | +}, |
| 207 | +getbar():number{ |
| 208 | +returnthis.foo*2; |
| 209 | +}, |
| 210 | +setbar(someValue:number){ |
| 211 | +this.foo=someValue/2; |
| 212 | +}, |
| 213 | +}; |
49 | 214 |
|
50 | | -// Keep values |
51 | | -assert.equal(dummy.foo,1); |
| 215 | +deprecate.property('bar is deprecated',originalObject,'bar'); |
| 216 | +originalObject.bar; |
| 217 | +assert.ok( |
| 218 | +deprecatedLogSpy.calledWith('bar is deprecated'), |
| 219 | +`deprecatedLogSpy called with (${deprecatedLogSpy.lastCall.args[0]})`, |
| 220 | +); |
52 | 221 |
|
53 | | -// Wrap methods |
54 | | -sinon.assert.calledWith(console.log,chalk.yellow('(!) ')+'foo is deprecated'); |
| 222 | +originalObject.bar=7; |
| 223 | +assert.ok( |
| 224 | +deprecatedLogSpy.calledWith('bar is deprecated'), |
| 225 | +`deprecatedLogSpy called with (${deprecatedLogSpy.lastCall.args[0]})`, |
| 226 | +); |
55 | 227 | }); |
56 | 228 | }); |
57 | 229 | }); |