Skip to content

Commit 6634f7e

Browse files
authored
test: improve coverage (#5638)
* test: improve coverage * fix * fix * chore: skipLibCheck temporarily * fix
1 parent 9e696d3 commit 6634f7e

File tree

8 files changed

+132
-13
lines changed

8 files changed

+132
-13
lines changed

lib/hexo/router.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ interface Data {
88
modified: boolean;
99
}
1010

11-
declare module 'stream' {
12-
export default class _Stream extends Stream {
13-
readable: boolean;
14-
}
15-
}
16-
1711
class RouteStream extends Readable {
1812
public _data: any;
1913
public _ended: boolean;
@@ -58,7 +52,7 @@ class RouteStream extends Readable {
5852
this._ended = true;
5953

6054
data().then(data => {
61-
if (data instanceof Stream && data.readable) {
55+
if (data instanceof Stream && (data as Stream.Readable).readable) {
6256
data.on('data', d => {
6357
this.push(d);
6458
});

test/scripts/console/generate.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type OriginalReturn = ReturnType<typeof generateConsole>;
1212
describe('generate', () => {
1313
let hexo: Hexo, generate: (...args: OriginalParams) => OriginalReturn;
1414

15-
beforeEach(async () => {
15+
beforeEach(async function() {
16+
this.timeout(5000);
1617
hexo = new Hexo(join(__dirname, 'generate_test'), {silent: true});
1718
generate = generateConsole.bind(hexo);
1819

@@ -179,7 +180,8 @@ describe('generate', () => {
179180
result.should.eql(content);
180181

181182
// Stop watching
182-
await hexo.unwatch();
183+
hexo.unwatch();
184+
await BluebirdPromise.delay(300);
183185
});
184186

185187
it('deploy', async () => {
@@ -204,14 +206,15 @@ describe('generate', () => {
204206
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'b'),
205207
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'c')
206208
]);
209+
await BluebirdPromise.delay(300);
207210
await generate();
208211

209212
// Update source file
210213
await BluebirdPromise.all([
211214
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'bb'),
212215
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'cc')
213216
]);
214-
217+
await BluebirdPromise.delay(300);
215218
// Generate again
216219
await generate();
217220

test/scripts/helpers/gravatar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('gravatar', () => {
66
return crypto.createHash('md5').update(str).digest('hex');
77
}
88

9-
const gravatar = gravatarHelper as any;
9+
const gravatar = gravatarHelper;
1010

1111
const email = 'abc@abc.com';
1212
const hash = md5(email);

test/scripts/hexo/hexo.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,45 @@ describe('Hexo', () => {
153153

154154
it('load() - theme', async () => await testLoad(join(hexo.theme_dir, 'source')));
155155

156+
157+
it('load() - load database', async () => {
158+
hexo._dbLoaded = false;
159+
const dbPath = hexo.database.options.path;
160+
161+
const fixture = {
162+
meta: {
163+
version: 1,
164+
warehouse: require('warehouse').version
165+
},
166+
models: {
167+
PostTag: [
168+
{ _id: 'cuid111111111111111111113', post_id: 'cuid111111111111111111111', tag_id: 'cuid111111111111111111112' }
169+
],
170+
Tag: [
171+
{ _id: 'cuid111111111111111111112', name: 'foo' }
172+
],
173+
Post: [
174+
{ _id: 'cuid111111111111111111111', source: 'test', slug: 'test' }
175+
]
176+
}
177+
};
178+
await writeFile(dbPath, JSON.stringify(fixture));
179+
await hexo.load();
180+
// check Model
181+
hexo.model('PostTag').toArray({lean: true}).length.should.eql(fixture.models.PostTag.length);
182+
hexo.model('Tag').toArray({lean: true}).length.should.eql(fixture.models.Tag.length);
183+
hexo.model('Post').toArray({lean: true}).length.should.eql(fixture.models.Post.length);
184+
hexo._binaryRelationIndex.post_tag.keyIndex.size.should.eql(1);
185+
hexo._binaryRelationIndex.post_tag.valueIndex.size.should.eql(1);
186+
await unlink(dbPath);
187+
// clean up
188+
await hexo.model('PostTag').removeById('cuid111111111111111111113');
189+
await hexo.model('Tag').removeById('cuid111111111111111111112');
190+
await hexo.model('Post').removeById('cuid111111111111111111111');
191+
hexo._binaryRelationIndex.post_tag.keyIndex.clear();
192+
hexo._binaryRelationIndex.post_tag.valueIndex.clear();
193+
});
194+
156195
// Issue #3964
157196
it('load() - merge theme config - deep clone', async () => {
158197
const hexo = new Hexo(__dirname, { silent: true });

test/scripts/hexo/load_plugins.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Hexo from '../../../lib/hexo';
44
import loadPlugins from '../../../lib/hexo/load_plugins';
55
import BluebirdPromise from 'bluebird';
66
import chai from 'chai';
7+
import { spy } from 'sinon';
78
const should = chai.should();
89

910
describe('Load plugins', () => {
@@ -93,6 +94,20 @@ describe('Load plugins', () => {
9394
});
9495
});
9596

97+
it('fail to load plugins', () => {
98+
const logSpy = spy();
99+
hexo.log.error = logSpy;
100+
const name = 'hexo-plugin-test';
101+
const path = join(hexo.plugin_dir, name, 'index.js');
102+
return BluebirdPromise.all([
103+
createPackageFile(name),
104+
writeFile(path, 'throw new Error("test")')
105+
]).then(() => loadPlugins(hexo)).then(() => {
106+
logSpy.args[0][1].should.contains('Plugin load failed: %s');
107+
logSpy.args[0][2].should.contains('hexo-plugin-test');
108+
});
109+
});
110+
96111
it('load async plugins', () => {
97112
const name = 'hexo-async-plugin-test';
98113
const path = join(hexo.plugin_dir, name, 'index.js');
@@ -217,6 +232,20 @@ describe('Load plugins', () => {
217232
return unlink(path);
218233
});
219234

235+
it('fail to load scripts', async () => {
236+
const logSpy = spy();
237+
hexo.log.error = logSpy;
238+
const path = join(hexo.script_dir, 'test.js');
239+
240+
writeFile(path, 'throw new Error("test")');
241+
await loadPlugins(hexo);
242+
243+
logSpy.args[0][1].should.contains('Script load failed: %s');
244+
logSpy.args[0][2].should.contains('test.js');
245+
return unlink(path);
246+
});
247+
248+
220249
it('load theme scripts', () => {
221250
const path = join(hexo.theme_script_dir, 'test.js');
222251

test/scripts/hexo/post.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ describe('Post', () => {
666666
].join('\n'));
667667

668668
const path = join(hexo.source_dir, '_posts', 'fooo.md');
669-
const data = await post.create({
669+
await post.create({
670670
title: 'fooo',
671671
layout: 'draft',
672672
tags: customTags,
@@ -1486,6 +1486,16 @@ describe('Post', () => {
14861486
data.content.should.contains('1');
14871487
data.content.should.contains('22222');
14881488

1489+
content = '{{ 1 }} \n `{% custom %}` 22222 `{% endcustom }`';
1490+
data = await post.render('', {
1491+
content,
1492+
engine: 'markdown'
1493+
});
1494+
data.content.should.contains('1');
1495+
data.content.should.contains('&#123;% custom %&#125;');
1496+
data.content.should.contains('22222');
1497+
data.content.should.contains('&#123;% endcustom &#125;');
1498+
14891499
// lost two characters
14901500
content = '{{ 1 }} \n `{#` \n 22222';
14911501
data = await post.render('', {

test/scripts/processors/post.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { join } from 'path';
3-
import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs';
3+
import { exists, mkdirs, rmdir, unlink, writeFile } from 'hexo-fs';
44
import BluebirdPromise from 'bluebird';
55
import defaultConfig from '../../../lib/hexo/default_config';
66
import Hexo from '../../../lib/hexo';
@@ -358,6 +358,49 @@ describe('post', () => {
358358
]);
359359
});
360360

361+
it('post - type: create - post_asset_folder enabled without asset', async () => {
362+
hexo.config.post_asset_folder = true;
363+
364+
const fooPath = join(hexo.source_dir, '_posts', 'foo');
365+
if (await exists(fooPath)) {
366+
await rmdir(fooPath);
367+
}
368+
369+
const body = [
370+
'title: "Hello world"',
371+
'date: 2006-01-02 15:04:05',
372+
'updated: 2014-12-13 01:02:03',
373+
'---',
374+
'The quick brown fox jumps over the lazy dog'
375+
].join('\n');
376+
377+
const file = newFile({
378+
path: 'foo.html',
379+
published: true,
380+
type: 'create',
381+
renderable: true
382+
});
383+
384+
await writeFile(file.source, body);
385+
await process(file);
386+
const post = Post.findOne({ source: file.path });
387+
388+
post.title.should.eql('Hello world');
389+
post.date.format(dateFormat).should.eql('2006-01-02 15:04:05');
390+
post.updated.format(dateFormat).should.eql('2014-12-13 01:02:03');
391+
post._content.should.eql('The quick brown fox jumps over the lazy dog');
392+
post.source.should.eql(file.path);
393+
post.raw.should.eql(body);
394+
post.slug.should.eql('foo');
395+
post.published.should.be.true;
396+
397+
hexo.config.post_asset_folder = false;
398+
return BluebirdPromise.all([
399+
post.remove(),
400+
unlink(file.source)
401+
]);
402+
});
403+
361404
it('post - type: update', async () => {
362405
const body = [
363406
'title: "New world"',

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"sourceMap": true,
66
"outDir": "dist",
77
"declaration": true,
8+
"skipLibCheck": true,
89
"esModuleInterop": true,
910
"types": [
1011
"node",

0 commit comments

Comments
 (0)