Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0add881

Browse files
Test cleanup: deprecate.test.ts (#1489)
* update node versions* add SinonSpy, SimpleObject types* replace console.log with a SinonSpy via sinon.fake so nothing is actually written to the console* create a separate test for deprecate.log* function: spy for .log; create 2 separate tests: single-responsibility, make behavior clear.* object: separate tests to make behavior clear; spy for .log;* property: separate tests to make behavior clear (esp. getter/setter); spy for .log;* added TODO for changing deprecate.js to .ts; cleaned up comments
1 parent7b28f4e commit0add881

File tree

1 file changed

+199
-27
lines changed

1 file changed

+199
-27
lines changed

‎test/deprecate.test.ts‎

Lines changed: 199 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,229 @@
11
/* eslint-disable import/no-named-as-default-member */
22
importassertfrom'node:assert';
33
importchalkfrom'chalk';
4-
importsinonfrom'sinon';
4+
importsinon,{typeSinonSpy}from'sinon';
55
importdeprecatefrom'../src/util/deprecate.js';
66

7+
typeSimpleObject={
8+
foo:number;
9+
functionInObj:(someValue:number)=>number;
10+
};
11+
712
describe('deprecate()',()=>{
13+
letfakeConsoleLog:SinonSpy;
814
beforeEach(()=>{
9-
sinon.spy(console,'log');
15+
fakeConsoleLog=sinon.fake();
16+
sinon.replace(console,'log',fakeConsoleLog);
1017
});
1118

1219
afterEach(()=>{
13-
console.log.restore();
20+
sinon.restore();
1421
});
1522

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+
});
2363
});
2464

2565
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={
2881
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+
},
3088
};
31-
constwrapped=deprecate.object('<%= name %> is deprecated',dummy);
3289

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+
);
3598

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+
);
40167
});
41168
});
42169

43170
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={
46183
foo:1,
47184
};
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+
};
49214

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+
);
52221

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+
);
55227
});
56228
});
57229
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp