Skip to content

Commit

Permalink
test: improve coverage (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon authored May 18, 2024
1 parent ca317b2 commit e1f7178
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 26 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@types/minimist": "^1.2.5",
"@types/mocha": "^10.0.6",
"@types/node": "^18.11.8",
"@types/proxyquire": "^1.3.31",
"@types/rewire": "^2.5.30",
"@types/sinon": "^17.0.3",
"chai": "^4.3.4",
Expand All @@ -70,7 +69,6 @@
"hexo-renderer-marked": "^6.0.0",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"rewire": "^6.0.0",
"sinon": "^17.0.1",
"ts-node": "^10.9.1",
Expand Down
71 changes: 71 additions & 0 deletions test/scripts/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import chai from 'chai';
import sinon from 'sinon';
import Console from '../../lib/extend/console';
chai.should();

describe('console', () => {
it('register - name required', () => {
const consoleExtend = new Console();
try {
// @ts-expect-error
consoleExtend.register();
} catch (err) {
err.should.have.property('message', 'name is required');
}
});

it('register - name, invalid fn', () => {
const consoleExtend = new Console();
try {
// @ts-expect-error
consoleExtend.register('test', 'fn');
} catch (err) {
err.should.have.property('message', 'fn must be a function');
}
});

it('register - name, desc, invalid fn', () => {
const consoleExtend = new Console();
try {
// @ts-expect-error
consoleExtend.register('test', 'desc', 'fn');
} catch (err) {
err.should.have.property('message', 'fn must be a function');
}
});

it('register - name, options, fn', () => {
const consoleExtend = new Console();
const options = {};
const fn = sinon.spy();
consoleExtend.register('test', options, fn);
const storeFn = consoleExtend.get('test');
storeFn();
fn.calledOnce.should.be.true;
storeFn.options?.should.eql(options);
storeFn.desc?.should.eql('');
});

it('register - name, desc, fn', () => {
const consoleExtend = new Console();
const desc = 'desc';
const fn = sinon.spy();
consoleExtend.register('test', desc, fn);
const storeFn = consoleExtend.get('test');
storeFn();
fn.calledOnce.should.be.true;
storeFn.options?.should.deep.equal({});
storeFn.desc?.should.eql(desc);
});

it('register - name, fn', () => {
const consoleExtend = new Console();
const fn = sinon.spy();
consoleExtend.register('test', fn);
const storeFn = consoleExtend.get('test');
storeFn();
fn.calledOnce.should.be.true;
storeFn.options?.should.deep.equal({});
storeFn.desc?.should.eql('');
});
});
261 changes: 237 additions & 24 deletions test/scripts/hexo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import chai from 'chai';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
import { writeFile, unlink, rmdir } from 'hexo-fs';
import { join } from 'path';
import rewire from 'rewire';
import ConsoleExtend from '../../lib/extend/console';
chai.should();

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

it('run help if no specified command', async () => {
const spy = sinon.spy();
const hexo = proxyquire('../../dist/hexo', {
'./console'(ctx) {
ctx.extend.console.register('help', spy);
const hexo = rewire('../../dist/hexo');
return hexo.__with__({
console_1: {
default: ctx => {
ctx.extend.console.register('help', spy);
}
}
})(async () => {
// @ts-expect-error
await hexo(cwd, {_: []});
spy.calledOnce.should.be.true;
});

await hexo(cwd, {_: []});
spy.calledOnce.should.be.true;
});

it('run specified command', async () => {
const spy = sinon.spy();
const hexo = proxyquire('../../dist/hexo', {
'./console'(ctx) {
ctx.extend.console.register('test', spy);
const hexo = rewire('../../dist/hexo');
return hexo.__with__({
console_1: {
default: ctx => {
ctx.extend.console.register('test', spy);
}
}
})(async () => {
// @ts-expect-error
await hexo(cwd, {_: ['test']});
spy.calledOnce.should.be.true;
});

await hexo(cwd, {_: ['test']});
spy.calledOnce.should.be.true;
});

it('run help if specified command not found', async () => {
const spy = sinon.spy();
const hexo = proxyquire('../../dist/hexo', {
'./console'(ctx) {
ctx.extend.console.register('help', spy);
const hexo = rewire('../../dist/hexo');
return hexo.__with__({
console_1: {
default: ctx => {
ctx.extend.console.register('help', spy);
}
}
})(async () => {
// @ts-expect-error
await hexo(cwd, {_: ['test']});
spy.calledOnce.should.be.true;
});
});

it('path - number (issue hexo#4334)', async () => {
let args;
const hexo = rewire('../../dist/hexo');
return hexo.__with__({
find_pkg_1: {
default: (_cwd, _args) => {
args = _args;
return Promise.resolve();
}
}
})(async () => {
process.argv = ['hexo', 'new', '--path', '123', 'test'];
// @ts-expect-error
hexo(null, null);
args.path.should.eql('123');
process.argv = [];
});
});

it('p - number (issue hexo#4334)', async () => {
let args;
const hexo = rewire('../../dist/hexo');
return hexo.__with__({
find_pkg_1: {
default: (_cwd, _args) => {
args = _args;
return Promise.resolve();
}
}
})(async () => {
process.argv = ['hexo', 'new', '-p', '123', 'test'];
// @ts-expect-error
hexo(null, null);
args.p.should.eql('123');
process.argv = [];
});
});

await hexo(cwd, {_: ['test']});
spy.calledOnce.should.be.true;
it('slug - number (issue hexo#4334)', async () => {
let args;
const hexo = rewire('../../dist/hexo');
return hexo.__with__({
find_pkg_1: {
default: (_cwd, _args) => {
args = _args;
return Promise.resolve();
}
}
})(async () => {
process.argv = ['hexo', 'new', '--slug', '123', 'test'];
// @ts-expect-error
hexo(null, null);
args.slug.should.eql('123');
process.argv = [];
});
});

it('s - number (issue hexo#4334)', async () => {
let args;
const hexo = rewire('../../dist/hexo');
return hexo.__with__({
find_pkg_1: {
default: (_cwd, _args) => {
args = _args;
return Promise.resolve();
}
}
})(async () => {
process.argv = ['hexo', 'new', '-s', '123', 'test'];
// @ts-expect-error
hexo(null, null);
args.s.should.eql('123');
process.argv = [];
});
});

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

it('should handle error properly');
it('should handle HexoNotFoundError properly', () => {
const spy = sinon.spy();
const hexo = rewire('../../dist/hexo');
const dummyPath = 'dummy';
const dummyError = 'test';
return hexo.__with__({
find_pkg_1: {
default: () => Promise.resolve(dummyPath)
},
loadModule: () => Promise.reject(new Error(dummyError)),
context_1: {
default: class Context {
log: { error: typeof spy };
constructor() {
this.log = {
error: spy
};
}
}
}
})(async () => {
// @ts-expect-error
await hexo(cwd, {_: ['test']});
spy.args[0][0].should.eql(dummyError);
spy.args[1][0].should.eql('Local hexo loading failed in %s');
spy.args[1][1].should.eql(`\x1B[35m${dummyPath}\x1B[39m`);
spy.args[2][0].should.eql('Try running: \'rm -rf node_modules && npm install --force\'');
process.exitCode?.should.eql(2);
});
});

it('should watch SIGINT signal');
it('should handle other Error properly', () => {
const spy = sinon.spy();
const hexo = rewire('../../dist/hexo');
const dummyPath = 'dummy';
const dummyError = 'error';
return hexo.__with__({
find_pkg_1: {
default: () => Promise.resolve(dummyPath)
},
loadModule: () => Promise.resolve(),
console_1: {
default: () => { /* empty */ }
},
context_1: {
default: class Context {
log: { error: typeof spy, fatal: typeof spy };
constructor() {
this.log = {
error: spy,
fatal: spy
};
}
init() {
throw new Error(dummyError);
}
}
}
})(async () => {
// @ts-expect-error
await hexo(cwd, {_: ['test']});
spy.args[0][0].message.should.eql(dummyError);
process.exitCode?.should.eql(2);
});
});

it('load hexo module in parent folder recursively');
it('should watch SIGINT signal', () => {
const spy = sinon.spy();
const watchSpy = sinon.spy();
const exitSpy = sinon.spy();
const dummyPath = 'dummy';
const hexo = rewire('../../dist/hexo');
const processSpy = {
on: process.on,
emit: process.emit,
exit: exitSpy
};
return hexo.__with__({
find_pkg_1: {
default: () => Promise.resolve(dummyPath)
},
loadModule: () => Promise.resolve(),
console_1: {
default: () => { /* empty */ }
},
process: processSpy,
context_1: {
default: class Context {
log: { error: typeof spy, fatal: typeof spy, info: typeof spy };
extend: {
console: ConsoleExtend;
};
constructor() {
this.log = {
error: spy,
fatal: spy,
info: spy
};
this.extend = {
console: new ConsoleExtend()
};
}
init() {
return Promise.resolve();
}
call() {
return Promise.resolve(processSpy.emit('SIGINT'));
}
unwatch() {
watchSpy();
}
exit() {
return Promise.resolve();
}
}
}
})(async () => {
// @ts-expect-error
await hexo(cwd, {_: ['help']});
[
'Good bye',
'See you again',
'Farewell',
'Have a nice day',
'Bye!',
'Catch you later'
].includes(spy.args[0][0]).should.be.true;
watchSpy.calledOnce.should.be.true;
exitSpy.calledOnce.should.be.true;
});
});

it('display error message if failed to load hexo module');
it('load hexo module in parent folder recursively');
});

0 comments on commit e1f7178

Please sign in to comment.