Skip to content

Commit e1f7178

Browse files
authored
test: improve coverage (#505)
1 parent ca317b2 commit e1f7178

File tree

3 files changed

+308
-26
lines changed

3 files changed

+308
-26
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"@types/minimist": "^1.2.5",
6262
"@types/mocha": "^10.0.6",
6363
"@types/node": "^18.11.8",
64-
"@types/proxyquire": "^1.3.31",
6564
"@types/rewire": "^2.5.30",
6665
"@types/sinon": "^17.0.3",
6766
"chai": "^4.3.4",
@@ -70,7 +69,6 @@
7069
"hexo-renderer-marked": "^6.0.0",
7170
"mocha": "^10.0.0",
7271
"nyc": "^15.1.0",
73-
"proxyquire": "^2.1.3",
7472
"rewire": "^6.0.0",
7573
"sinon": "^17.0.1",
7674
"ts-node": "^10.9.1",

test/scripts/console.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import chai from 'chai';
2+
import sinon from 'sinon';
3+
import Console from '../../lib/extend/console';
4+
chai.should();
5+
6+
describe('console', () => {
7+
it('register - name required', () => {
8+
const consoleExtend = new Console();
9+
try {
10+
// @ts-expect-error
11+
consoleExtend.register();
12+
} catch (err) {
13+
err.should.have.property('message', 'name is required');
14+
}
15+
});
16+
17+
it('register - name, invalid fn', () => {
18+
const consoleExtend = new Console();
19+
try {
20+
// @ts-expect-error
21+
consoleExtend.register('test', 'fn');
22+
} catch (err) {
23+
err.should.have.property('message', 'fn must be a function');
24+
}
25+
});
26+
27+
it('register - name, desc, invalid fn', () => {
28+
const consoleExtend = new Console();
29+
try {
30+
// @ts-expect-error
31+
consoleExtend.register('test', 'desc', 'fn');
32+
} catch (err) {
33+
err.should.have.property('message', 'fn must be a function');
34+
}
35+
});
36+
37+
it('register - name, options, fn', () => {
38+
const consoleExtend = new Console();
39+
const options = {};
40+
const fn = sinon.spy();
41+
consoleExtend.register('test', options, fn);
42+
const storeFn = consoleExtend.get('test');
43+
storeFn();
44+
fn.calledOnce.should.be.true;
45+
storeFn.options?.should.eql(options);
46+
storeFn.desc?.should.eql('');
47+
});
48+
49+
it('register - name, desc, fn', () => {
50+
const consoleExtend = new Console();
51+
const desc = 'desc';
52+
const fn = sinon.spy();
53+
consoleExtend.register('test', desc, fn);
54+
const storeFn = consoleExtend.get('test');
55+
storeFn();
56+
fn.calledOnce.should.be.true;
57+
storeFn.options?.should.deep.equal({});
58+
storeFn.desc?.should.eql(desc);
59+
});
60+
61+
it('register - name, fn', () => {
62+
const consoleExtend = new Console();
63+
const fn = sinon.spy();
64+
consoleExtend.register('test', fn);
65+
const storeFn = consoleExtend.get('test');
66+
storeFn();
67+
fn.calledOnce.should.be.true;
68+
storeFn.options?.should.deep.equal({});
69+
storeFn.desc?.should.eql('');
70+
});
71+
});

test/scripts/hexo.ts

Lines changed: 237 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import chai from 'chai';
22
import sinon from 'sinon';
3-
import proxyquire from 'proxyquire';
4-
import { writeFile, unlink, rmdir } from 'hexo-fs';
5-
import { join } from 'path';
3+
import rewire from 'rewire';
4+
import ConsoleExtend from '../../lib/extend/console';
65
chai.should();
76

87
require('chai').should();
@@ -12,47 +11,261 @@ describe('hexo', () => {
1211

1312
it('run help if no specified command', async () => {
1413
const spy = sinon.spy();
15-
const hexo = proxyquire('../../dist/hexo', {
16-
'./console'(ctx) {
17-
ctx.extend.console.register('help', spy);
14+
const hexo = rewire('../../dist/hexo');
15+
return hexo.__with__({
16+
console_1: {
17+
default: ctx => {
18+
ctx.extend.console.register('help', spy);
19+
}
1820
}
21+
})(async () => {
22+
// @ts-expect-error
23+
await hexo(cwd, {_: []});
24+
spy.calledOnce.should.be.true;
1925
});
20-
21-
await hexo(cwd, {_: []});
22-
spy.calledOnce.should.be.true;
2326
});
2427

2528
it('run specified command', async () => {
2629
const spy = sinon.spy();
27-
const hexo = proxyquire('../../dist/hexo', {
28-
'./console'(ctx) {
29-
ctx.extend.console.register('test', spy);
30+
const hexo = rewire('../../dist/hexo');
31+
return hexo.__with__({
32+
console_1: {
33+
default: ctx => {
34+
ctx.extend.console.register('test', spy);
35+
}
3036
}
37+
})(async () => {
38+
// @ts-expect-error
39+
await hexo(cwd, {_: ['test']});
40+
spy.calledOnce.should.be.true;
3141
});
32-
33-
await hexo(cwd, {_: ['test']});
34-
spy.calledOnce.should.be.true;
3542
});
3643

3744
it('run help if specified command not found', async () => {
3845
const spy = sinon.spy();
39-
const hexo = proxyquire('../../dist/hexo', {
40-
'./console'(ctx) {
41-
ctx.extend.console.register('help', spy);
46+
const hexo = rewire('../../dist/hexo');
47+
return hexo.__with__({
48+
console_1: {
49+
default: ctx => {
50+
ctx.extend.console.register('help', spy);
51+
}
52+
}
53+
})(async () => {
54+
// @ts-expect-error
55+
await hexo(cwd, {_: ['test']});
56+
spy.calledOnce.should.be.true;
57+
});
58+
});
59+
60+
it('path - number (issue hexo#4334)', async () => {
61+
let args;
62+
const hexo = rewire('../../dist/hexo');
63+
return hexo.__with__({
64+
find_pkg_1: {
65+
default: (_cwd, _args) => {
66+
args = _args;
67+
return Promise.resolve();
68+
}
69+
}
70+
})(async () => {
71+
process.argv = ['hexo', 'new', '--path', '123', 'test'];
72+
// @ts-expect-error
73+
hexo(null, null);
74+
args.path.should.eql('123');
75+
process.argv = [];
76+
});
77+
});
78+
79+
it('p - number (issue hexo#4334)', async () => {
80+
let args;
81+
const hexo = rewire('../../dist/hexo');
82+
return hexo.__with__({
83+
find_pkg_1: {
84+
default: (_cwd, _args) => {
85+
args = _args;
86+
return Promise.resolve();
87+
}
4288
}
89+
})(async () => {
90+
process.argv = ['hexo', 'new', '-p', '123', 'test'];
91+
// @ts-expect-error
92+
hexo(null, null);
93+
args.p.should.eql('123');
94+
process.argv = [];
4395
});
96+
});
4497

45-
await hexo(cwd, {_: ['test']});
46-
spy.calledOnce.should.be.true;
98+
it('slug - number (issue hexo#4334)', async () => {
99+
let args;
100+
const hexo = rewire('../../dist/hexo');
101+
return hexo.__with__({
102+
find_pkg_1: {
103+
default: (_cwd, _args) => {
104+
args = _args;
105+
return Promise.resolve();
106+
}
107+
}
108+
})(async () => {
109+
process.argv = ['hexo', 'new', '--slug', '123', 'test'];
110+
// @ts-expect-error
111+
hexo(null, null);
112+
args.slug.should.eql('123');
113+
process.argv = [];
114+
});
115+
});
116+
117+
it('s - number (issue hexo#4334)', async () => {
118+
let args;
119+
const hexo = rewire('../../dist/hexo');
120+
return hexo.__with__({
121+
find_pkg_1: {
122+
default: (_cwd, _args) => {
123+
args = _args;
124+
return Promise.resolve();
125+
}
126+
}
127+
})(async () => {
128+
process.argv = ['hexo', 'new', '-s', '123', 'test'];
129+
// @ts-expect-error
130+
hexo(null, null);
131+
args.s.should.eql('123');
132+
process.argv = [];
133+
});
47134
});
48135

49136
it('should call init() method');
50137

51-
it('should handle error properly');
138+
it('should handle HexoNotFoundError properly', () => {
139+
const spy = sinon.spy();
140+
const hexo = rewire('../../dist/hexo');
141+
const dummyPath = 'dummy';
142+
const dummyError = 'test';
143+
return hexo.__with__({
144+
find_pkg_1: {
145+
default: () => Promise.resolve(dummyPath)
146+
},
147+
loadModule: () => Promise.reject(new Error(dummyError)),
148+
context_1: {
149+
default: class Context {
150+
log: { error: typeof spy };
151+
constructor() {
152+
this.log = {
153+
error: spy
154+
};
155+
}
156+
}
157+
}
158+
})(async () => {
159+
// @ts-expect-error
160+
await hexo(cwd, {_: ['test']});
161+
spy.args[0][0].should.eql(dummyError);
162+
spy.args[1][0].should.eql('Local hexo loading failed in %s');
163+
spy.args[1][1].should.eql(`\x1B[35m${dummyPath}\x1B[39m`);
164+
spy.args[2][0].should.eql('Try running: \'rm -rf node_modules && npm install --force\'');
165+
process.exitCode?.should.eql(2);
166+
});
167+
});
52168

53-
it('should watch SIGINT signal');
169+
it('should handle other Error properly', () => {
170+
const spy = sinon.spy();
171+
const hexo = rewire('../../dist/hexo');
172+
const dummyPath = 'dummy';
173+
const dummyError = 'error';
174+
return hexo.__with__({
175+
find_pkg_1: {
176+
default: () => Promise.resolve(dummyPath)
177+
},
178+
loadModule: () => Promise.resolve(),
179+
console_1: {
180+
default: () => { /* empty */ }
181+
},
182+
context_1: {
183+
default: class Context {
184+
log: { error: typeof spy, fatal: typeof spy };
185+
constructor() {
186+
this.log = {
187+
error: spy,
188+
fatal: spy
189+
};
190+
}
191+
init() {
192+
throw new Error(dummyError);
193+
}
194+
}
195+
}
196+
})(async () => {
197+
// @ts-expect-error
198+
await hexo(cwd, {_: ['test']});
199+
spy.args[0][0].message.should.eql(dummyError);
200+
process.exitCode?.should.eql(2);
201+
});
202+
});
54203

55-
it('load hexo module in parent folder recursively');
204+
it('should watch SIGINT signal', () => {
205+
const spy = sinon.spy();
206+
const watchSpy = sinon.spy();
207+
const exitSpy = sinon.spy();
208+
const dummyPath = 'dummy';
209+
const hexo = rewire('../../dist/hexo');
210+
const processSpy = {
211+
on: process.on,
212+
emit: process.emit,
213+
exit: exitSpy
214+
};
215+
return hexo.__with__({
216+
find_pkg_1: {
217+
default: () => Promise.resolve(dummyPath)
218+
},
219+
loadModule: () => Promise.resolve(),
220+
console_1: {
221+
default: () => { /* empty */ }
222+
},
223+
process: processSpy,
224+
context_1: {
225+
default: class Context {
226+
log: { error: typeof spy, fatal: typeof spy, info: typeof spy };
227+
extend: {
228+
console: ConsoleExtend;
229+
};
230+
constructor() {
231+
this.log = {
232+
error: spy,
233+
fatal: spy,
234+
info: spy
235+
};
236+
this.extend = {
237+
console: new ConsoleExtend()
238+
};
239+
}
240+
init() {
241+
return Promise.resolve();
242+
}
243+
call() {
244+
return Promise.resolve(processSpy.emit('SIGINT'));
245+
}
246+
unwatch() {
247+
watchSpy();
248+
}
249+
exit() {
250+
return Promise.resolve();
251+
}
252+
}
253+
}
254+
})(async () => {
255+
// @ts-expect-error
256+
await hexo(cwd, {_: ['help']});
257+
[
258+
'Good bye',
259+
'See you again',
260+
'Farewell',
261+
'Have a nice day',
262+
'Bye!',
263+
'Catch you later'
264+
].includes(spy.args[0][0]).should.be.true;
265+
watchSpy.calledOnce.should.be.true;
266+
exitSpy.calledOnce.should.be.true;
267+
});
268+
});
56269

57-
it('display error message if failed to load hexo module');
270+
it('load hexo module in parent folder recursively');
58271
});

0 commit comments

Comments
 (0)