Skip to content

Commit

Permalink
Merge pull request #4157 from segayuu/refactor-test
Browse files Browse the repository at this point in the history
refactor(test): replace from rewire to sinon.stub()
  • Loading branch information
SukkaW authored Feb 25, 2020
2 parents b53ac97 + a34ad06 commit a71f08a
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 294 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"devDependencies": {
"@easyops/git-exec-and-restage": "^1.0.4",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"cheerio": "0.22.0",
"eslint": "^6.0.1",
"eslint-config-hexo": "^4.1.0",
Expand All @@ -78,7 +77,6 @@
"lint-staged": "^9.1.0",
"mocha": "^7.0.0",
"nyc": "^15.0.0",
"rewire": "^4.0.1",
"sinon": "^8.0.1"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('chai').use(require('chai-as-promised'));
require('chai');

describe('Hexo', () => {
require('./scripts/box');
Expand Down
56 changes: 26 additions & 30 deletions test/scripts/console/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
const { mkdirs, readFile, rmdir, unlink, writeFile } = require('hexo-fs');
const { join } = require('path');
const { load } = require('js-yaml');
const rewire = require('rewire');
const sinon = require('sinon');
const { stub } = require('sinon');

describe('config', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(join(__dirname, 'config_test'), {silent: true});
const config = require('../../../lib/plugins/console/config').bind(hexo);
const configModule = rewire('../../../lib/plugins/console/config');

before(async () => {
await mkdirs(hexo.base_dir);
Expand All @@ -22,45 +20,43 @@ describe('config', () => {
after(() => rmdir(hexo.base_dir));

it('read all config', async () => {
const spy = sinon.spy();
const _stub = stub(console, 'log');

await configModule.__with__({
console: {
log: spy
}
})(() => configModule.call(hexo, {_: []}));
try {
await config({_: []});
} finally {
_stub.restore();
}

spy.args[0][0].should.eql(hexo.config);
_stub.calledWith(hexo.config).should.be.true;
});

it('read config', async () => {
const spy = sinon.spy();
const _stub = stub(console, 'log');

await configModule.__with__({
console: {
log: spy
}
})(() => configModule.call(hexo, {_: ['title']}));
try {
await config({_: ['title']});
} finally {
_stub.restore();
}

spy.args[0][0].should.eql(hexo.config.title);
_stub.calledWith(hexo.config.title).should.be.true;
});

it('read nested config', () => {
const spy = sinon.spy();
it('read nested config', async () => {
const _stub = stub(console, 'log');

hexo.config.server = {
port: 12345
};
try {
hexo.config.server = {
port: 12345
};

return configModule.__with__({
console: {
log: spy
}
})(() => configModule.call(hexo, {_: ['server.port']})).then(() => {
spy.args[0][0].should.eql(hexo.config.server.port);
}).finally(() => {
await config({_: ['server.port']});
_stub.calledWith(hexo.config.server.port).should.be.true;
} finally {
delete hexo.config.server;
});
_stub.restore();
}
});

async function writeConfig(...args) {
Expand Down
11 changes: 9 additions & 2 deletions test/scripts/console/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { exists, mkdirs, readFile, rmdir, writeFile } = require('hexo-fs');
const { join } = require('path');
const { spy } = require('sinon');
const { spy, stub, match } = require('sinon');

describe('deploy', () => {
const Hexo = require('../../../lib/hexo');
Expand All @@ -24,7 +24,14 @@ describe('deploy', () => {
it('no deploy config', () => {
delete hexo.config.deploy;

should.not.exist(deploy({ test: true }));
const _stub = stub(console, 'log');

try {
should.not.exist(deploy({ test: true }));
_stub.calledWith(match('You should configure deployment settings in _config.yml first!')).should.eql(true);
} finally {
_stub.restore();
}
});

it('single deploy setting', async () => {
Expand Down
36 changes: 16 additions & 20 deletions test/scripts/console/list_categories.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const Promise = require('bluebird');
const sinon = require('sinon');
const expect = require('chai').expect;
const { stub, match } = require('sinon');
const { expect } = require('chai');

describe('Console list', () => {
const Hexo = require('../../../lib/hexo');
Expand All @@ -11,22 +11,19 @@ describe('Console list', () => {

const listCategories = require('../../../lib/plugins/console/list/category').bind(hexo);

before(() => {
const log = console.log;
sinon.stub(console, 'log').callsFake((...args) => {
return log.apply(log, args);
});
});
let logStub;

after(() => {
console.log.restore();
});
before(() => { logStub = stub(console, 'log'); });

afterEach(() => { logStub.reset(); });

after(() => { logStub.restore(); });

it('no categories', () => {
listCategories();
expect(console.log.calledWith(sinon.match('Name'))).be.true;
expect(console.log.calledWith(sinon.match('Posts'))).be.true;
expect(console.log.calledWith(sinon.match('No categories.'))).be.true;
expect(logStub.calledWith(match('Name'))).be.true;
expect(logStub.calledWith(match('Posts'))).be.true;
expect(logStub.calledWith(match('No categories.'))).be.true;
});

it('categories', () => {
Expand All @@ -42,13 +39,12 @@ describe('Console list', () => {
['baz']
], (tags, i) => posts[i].setCategories(tags))).then(() => {
hexo.locals.invalidate();
})
.then(() => {
}).then(() => {
listCategories();
expect(console.log.calledWith(sinon.match('Name'))).be.true;
expect(console.log.calledWith(sinon.match('Posts'))).be.true;
expect(console.log.calledWith(sinon.match('baz'))).be.true;
expect(console.log.calledWith(sinon.match('foo'))).be.true;
expect(logStub.calledWith(match('Name'))).be.true;
expect(logStub.calledWith(match('Posts'))).be.true;
expect(logStub.calledWith(match('baz'))).be.true;
expect(logStub.calledWith(match('foo'))).be.true;
});
});
});
53 changes: 26 additions & 27 deletions test/scripts/console/list_page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const sinon = require('sinon');
const expect = require('chai').expect;
const { stub, match } = require('sinon');
const { expect } = require('chai');

describe('Console list', () => {
const Hexo = require('../../../lib/hexo');
Expand All @@ -10,36 +10,35 @@ describe('Console list', () => {
const listPages = require('../../../lib/plugins/console/list/page').bind(hexo);

hexo.config.permalink = ':title/';
before(() => {
const log = console.log;
sinon.stub(console, 'log').callsFake((...args) => {
return log.apply(log, args);
});
});

after(() => {
console.log.restore();
});
let logStub;

before(() => { logStub = stub(console, 'log'); });

afterEach(() => { logStub.reset(); });

after(() => { logStub.restore(); });

it('no page', () => {
listPages();
expect(console.log.calledWith(sinon.match('Date'))).be.true;
expect(console.log.calledWith(sinon.match('Title'))).be.true;
expect(console.log.calledWith(sinon.match('Path'))).be.true;
expect(console.log.calledWith(sinon.match('No pages.'))).be.true;
expect(logStub.calledWith(match('Date'))).be.true;
expect(logStub.calledWith(match('Title'))).be.true;
expect(logStub.calledWith(match('Path'))).be.true;
expect(logStub.calledWith(match('No pages.'))).be.true;
});

it('page', () => Page.insert({
source: 'foo',
title: 'Hello World',
path: 'bar'
})
.then(() => {
it('page', () => {
return Page.insert({
source: 'foo',
title: 'Hello World',
path: 'bar'
}).then(() => {
listPages();
expect(console.log.calledWith(sinon.match('Date'))).be.true;
expect(console.log.calledWith(sinon.match('Title'))).be.true;
expect(console.log.calledWith(sinon.match('Path'))).be.true;
expect(console.log.calledWith(sinon.match('Hello World'))).be.true;
expect(console.log.calledWith(sinon.match('foo'))).be.true;
}));
expect(logStub.calledWith(match('Date'))).be.true;
expect(logStub.calledWith(match('Title'))).be.true;
expect(logStub.calledWith(match('Path'))).be.true;
expect(logStub.calledWith(match('Hello World'))).be.true;
expect(logStub.calledWith(match('foo'))).be.true;
});
});
});
47 changes: 22 additions & 25 deletions test/scripts/console/list_post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const sinon = require('sinon');
const expect = require('chai').expect;
const { stub, match } = require('sinon');
const { expect } = require('chai');

describe('Console list', () => {
const Hexo = require('../../../lib/hexo');
Expand All @@ -10,25 +10,22 @@ describe('Console list', () => {

const listPosts = require('../../../lib/plugins/console/list/post').bind(hexo);

before(() => {
const log = console.log;
sinon.stub(console, 'log').callsFake((...args) => {
return log.apply(log, args);
});
});
let logStub;

after(() => {
console.log.restore();
});
before(() => { logStub = stub(console, 'log'); });

afterEach(() => { logStub.reset(); });

after(() => { logStub.restore(); });

it('no post', () => {
listPosts();
expect(console.log.calledWith(sinon.match('Date'))).be.true;
expect(console.log.calledWith(sinon.match('Title'))).be.true;
expect(console.log.calledWith(sinon.match('Path'))).be.true;
expect(console.log.calledWith(sinon.match('Category'))).be.true;
expect(console.log.calledWith(sinon.match('Tags'))).be.true;
expect(console.log.calledWith(sinon.match('No posts.'))).be.true;
expect(logStub.calledWith(match('Date'))).be.true;
expect(logStub.calledWith(match('Title'))).be.true;
expect(logStub.calledWith(match('Path'))).be.true;
expect(logStub.calledWith(match('Category'))).be.true;
expect(logStub.calledWith(match('Tags'))).be.true;
expect(logStub.calledWith(match('No posts.'))).be.true;
});

it('post', () => {
Expand All @@ -43,15 +40,15 @@ describe('Console list', () => {
})
.then(() => {
listPosts();
expect(console.log.calledWith(sinon.match('Date'))).be.true;
expect(console.log.calledWith(sinon.match('Title'))).be.true;
expect(console.log.calledWith(sinon.match('Path'))).be.true;
expect(console.log.calledWith(sinon.match('Category'))).be.true;
expect(console.log.calledWith(sinon.match('Tags'))).be.true;
expect(logStub.calledWith(match('Date'))).be.true;
expect(logStub.calledWith(match('Title'))).be.true;
expect(logStub.calledWith(match('Path'))).be.true;
expect(logStub.calledWith(match('Category'))).be.true;
expect(logStub.calledWith(match('Tags'))).be.true;
for (let i = 0; i < posts.length; i++) {
expect(console.log.calledWith(sinon.match(posts[i].source))).be.true;
expect(console.log.calledWith(sinon.match(posts[i].slug))).be.true;
expect(console.log.calledWith(sinon.match(posts[i].title))).be.true;
expect(logStub.calledWith(match(posts[i].source))).be.true;
expect(logStub.calledWith(match(posts[i].slug))).be.true;
expect(logStub.calledWith(match(posts[i].title))).be.true;
}
});
});
Expand Down
Loading

0 comments on commit a71f08a

Please sign in to comment.