diff --git a/package.json b/package.json index 471e3eb4..ed599639 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "license": "MIT", "devDependencies": { "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "eslint": "^6.0.1", "eslint-config-hexo": "^3.0.0", "html-tag-validator": "^1.5.0", diff --git a/test/scripts/cache_stream.js b/test/scripts/cache_stream.js index 6fab3f1b..3e80262e 100644 --- a/test/scripts/cache_stream.js +++ b/test/scripts/cache_stream.js @@ -1,5 +1,7 @@ 'use strict'; +require('chai').should(); + const { Readable } = require('stream'); describe('CacheStream', () => { diff --git a/test/scripts/camel_case_keys.js b/test/scripts/camel_case_keys.js index e8b0ba6a..d936e525 100644 --- a/test/scripts/camel_case_keys.js +++ b/test/scripts/camel_case_keys.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('camelCaseKeys', () => { const camelCaseKeys = require('../../lib/camel_case_keys'); @@ -17,11 +17,7 @@ describe('camelCaseKeys', () => { }); it('obj must be an object', () => { - try { - camelCaseKeys(); - } catch (err) { - err.should.have.property('message', 'obj must be an object!'); - } + camelCaseKeys.should.throw('obj must be an object!'); }); it('setter', () => { @@ -48,12 +44,7 @@ describe('camelCaseKeys', () => { }); it('do nothing if the key is camelCase', () => { - const result = camelCaseKeys({ - fooBar: 'test' - }); - - result.should.eql({ - fooBar: 'test' - }); + const result = camelCaseKeys({ fooBar: 'test' }); + result.should.eql({ fooBar: 'test' }); }); }); diff --git a/test/scripts/color.js b/test/scripts/color.js index 898777f7..3f943fb3 100644 --- a/test/scripts/color.js +++ b/test/scripts/color.js @@ -1,5 +1,7 @@ 'use strict'; +require('chai').should(); + describe('color', () => { const Color = require('../../lib/color'); diff --git a/test/scripts/escape_diacritic.js b/test/scripts/escape_diacritic.js index 60751910..d8579254 100644 --- a/test/scripts/escape_diacritic.js +++ b/test/scripts/escape_diacritic.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('escapeDiacritic', () => { const escapeDiacritic = require('../../lib/escape_diacritic'); @@ -10,10 +10,6 @@ describe('escapeDiacritic', () => { }); it('str must be a string', () => { - try { - escapeDiacritic(); - } catch (err) { - err.should.have.property('message', 'str must be a string!'); - } + escapeDiacritic.should.throw('str must be a string!'); }); }); diff --git a/test/scripts/escape_html.js b/test/scripts/escape_html.js index 0207fc6f..25d64841 100644 --- a/test/scripts/escape_html.js +++ b/test/scripts/escape_html.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('escapeHTML', () => { const escapeHTML = require('../../lib/escape_html'); @@ -10,10 +10,6 @@ describe('escapeHTML', () => { }); it('str must be a string', () => { - try { - escapeHTML(); - } catch (err) { - err.should.have.property('message', 'str must be a string!'); - } + escapeHTML.should.throw('str must be a string!'); }); }); diff --git a/test/scripts/escape_regexp.js b/test/scripts/escape_regexp.js index 7c3c5116..d9ad11b7 100644 --- a/test/scripts/escape_regexp.js +++ b/test/scripts/escape_regexp.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('escapeRegExp', () => { const escapeRegExp = require('../../lib/escape_regexp'); @@ -10,10 +10,6 @@ describe('escapeRegExp', () => { }); it('str must be a string', () => { - try { - escapeRegExp(); - } catch (err) { - err.should.have.property('message', 'str must be a string!'); - } + escapeRegExp.should.throw('str must be a string!'); }); }); diff --git a/test/scripts/hash.js b/test/scripts/hash.js index 820eb254..eac02297 100644 --- a/test/scripts/hash.js +++ b/test/scripts/hash.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); const crypto = require('crypto'); function sha1(content) { @@ -28,17 +28,17 @@ describe('hash', () => { stream.read().should.eql(sha1(content)); }); - it('createSha1Hash', function() { - var _sha1 = hash.createSha1Hash(); - var content = '123456'; + it('createSha1Hash', () => { + const _sha1 = hash.createSha1Hash(); + const content = '123456'; _sha1.update(content); _sha1.digest().should.eql(sha1(content)); }); - it('createSha1Hash - streamMode', function() { - var content1 = '123456'; - var content2 = '654321'; - var stream = hash.createSha1Hash(); + it('createSha1Hash - streamMode', () => { + const content1 = '123456'; + const content2 = '654321'; + const stream = hash.createSha1Hash(); // explicit convert stream.write(Buffer.from(content1)); // implicit convert diff --git a/test/scripts/highlight.js b/test/scripts/highlight.js index 3fb23900..3e01edde 100644 --- a/test/scripts/highlight.js +++ b/test/scripts/highlight.js @@ -46,27 +46,14 @@ function code(str, lang) { } const lines = data.value.split('\n'); - let result = codeStart; - for (let i = 0, len = lines.length; i < len; i++) { - result += `${lines[i]}
`; - } - - result += codeEnd; - - return result; + return lines.reduce((prev, current) => { + return `${prev}${current}
`; + }, codeStart) + codeEnd; } -function assertResult(result) { - let expected = start; - - for (let i = 1, len = arguments.length; i < len; i++) { - expected += arguments[i]; - } - - expected += end; - - result.should.eql(expected); +function assertResult(result, ...args) { + result.should.eql(start + args.join('') + end); } function validateHtmlAsync(str, done) { @@ -89,11 +76,7 @@ describe('highlight', () => { }); it('str must be a string', () => { - try { - highlight(); - } catch (err) { - err.should.have.property('message', 'str must be a string!'); - } + highlight.should.throw('str must be a string!'); }); it('gutter: false', done => { diff --git a/test/scripts/html_tag.js b/test/scripts/html_tag.js index 67b2470b..b719d33b 100644 --- a/test/scripts/html_tag.js +++ b/test/scripts/html_tag.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('htmlTag', () => { const htmlTag = require('../../lib/html_tag'); diff --git a/test/scripts/pattern.js b/test/scripts/pattern.js index a21010d9..d2e99635 100644 --- a/test/scripts/pattern.js +++ b/test/scripts/pattern.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('Pattern', () => { const Pattern = require('../../lib/pattern'); @@ -60,11 +60,9 @@ describe('Pattern', () => { }); it('rule is required', () => { - try { + (() => { // eslint-disable-next-line no-new new Pattern(); - } catch (err) { - err.should.have.property('message', 'rule must be a function, a string or a regular expression.'); - } + }).should.throw('rule must be a function, a string or a regular expression.'); }); }); diff --git a/test/scripts/permalink.js b/test/scripts/permalink.js index dbad6837..4ce6a6e2 100644 --- a/test/scripts/permalink.js +++ b/test/scripts/permalink.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('Permalink', () => { const Permalink = require('../../lib/permalink'); diff --git a/test/scripts/slugize.js b/test/scripts/slugize.js index e4ddde89..d1db5442 100644 --- a/test/scripts/slugize.js +++ b/test/scripts/slugize.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('slugize', () => { const slugize = require('../../lib/slugize'); @@ -42,10 +42,6 @@ describe('slugize', () => { }); it('str must be a string', () => { - try { - slugize(); - } catch (err) { - err.should.have.property('message', 'str must be a string!'); - } + slugize.should.throw('str must be a string!'); }); }); diff --git a/test/scripts/spawn.js b/test/scripts/spawn.js index 947c3d9b..7d21e909 100644 --- a/test/scripts/spawn.js +++ b/test/scripts/spawn.js @@ -1,8 +1,8 @@ 'use strict'; -const should = require('chai').should(); -const pathFn = require('path'); -const fs = require('fs'); +require('chai').use(require('chai-as-promised')).should(); +const { join } = require('path'); +const { writeFile, unlink } = require('fs'); const rewire = require('rewire'); const isWindows = process.platform === 'win32'; @@ -11,34 +11,30 @@ const catCommand = isWindows ? 'type' : 'cat'; describe('spawn', () => { const spawn = require('../../lib/spawn'); const CacheStream = require('../../lib/cache_stream'); - const fixturePath = pathFn.join(__dirname, 'spawn_test.txt'); + const fixturePath = join(__dirname, 'spawn_test.txt'); const fixture = 'test content'; before(done => { - fs.writeFile(fixturePath, fixture, done); + writeFile(fixturePath, fixture, done); }); after(done => { - fs.unlink(fixturePath, done); + unlink(fixturePath, done); }); - it('default', () => spawn(catCommand, [fixturePath]).then(content => { - content.should.eql(fixture); - })); + it('default', () => spawn(catCommand, [fixturePath]).should.become(fixture)); it('command is required', () => { spawn.should.throw('command is required!'); }); - it('error', () => spawn(catCommand, ['nothing']).then(() => should.fail(`expected spawn(${catCommand}, ['nothing']) to throw an error`), err => { + it('error', () => { + const promise = spawn(catCommand, ['nothing']); if (isWindows) { - err.message.trim().should.eql('spawn type ENOENT'); - err.code.should.eql('ENOENT'); - } else { - err.message.trim().should.eql('cat: nothing: No such file or directory'); - err.code.should.eql(1); + return promise.should.rejectedWith('spawn type ENOENT').and.eventually.have.property('code', 'ENOENT'); } - })); + return promise.should.rejectedWith('cat: nothing: No such file or directory').and.eventually.have.property('code', 1); + }); it('verbose - stdout', () => { const spawn = rewire('../../lib/spawn'); @@ -48,10 +44,7 @@ describe('spawn', () => { spawn.__set__('process', Object.assign({}, process, { stdout: stdoutCache, - stderr: stderrCache, - removeListener: () => {}, - on: () => {}, - exit: () => {} + stderr: stderrCache })); return spawn('echo', [content], { @@ -75,32 +68,29 @@ describe('spawn', () => { stdout: stdoutCache, stderr: stderrCache, removeListener: () => {}, - on: () => {}, - exit: () => {} + on: () => {} })); return spawn(catCommand, ['nothing'], { verbose: true - }).then(() => should.fail(`expected spawn(${catCommand}, ['nothing'], {verbose: true}) to throw an error`), () => { + }).should.rejected.then(() => { const stderrResult = stderrCache.getCache(); if (isWindows) { // utf8 support in windows shell (cmd.exe) is difficult. Buffer.byteLength(stderrResult, 'hex').should.least(1); } else { - stderrResult.toString('utf8').trim().should.eql('cat: nothing: No such file or directory'); + stderrResult.toString('utf8').should.with.match(/^cat: nothing: No such file or directory\n?$/); } }); }); - it('custom encoding', () => spawn(catCommand, [fixturePath], {encoding: 'hex'}).then(content => { - content.should.eql(Buffer.from(fixture).toString('hex')); - })); + it('custom encoding', () => { + return spawn(catCommand, [fixturePath], {encoding: 'hex'}).should.become(Buffer.from(fixture).toString('hex')); + }); - it('encoding = null', () => spawn(catCommand, [fixturePath], {encoding: null}).then(content => { - content.should.eql(Buffer.from(fixture)); - })); + it('encoding = null', () => { + return spawn(catCommand, [fixturePath], {encoding: null}).should.become(Buffer.from(fixture)); + }); - it('stdio = inherit', () => spawn('echo', ['something'], { - stdio: 'inherit' - })); + it('stdio = inherit', () => spawn('echo', ['something'], { stdio: 'inherit' })); }); diff --git a/test/scripts/truncate.js b/test/scripts/truncate.js index a5f633fe..67a1cb2d 100644 --- a/test/scripts/truncate.js +++ b/test/scripts/truncate.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('truncate', () => { const truncate = require('../../lib/truncate'); @@ -31,10 +31,6 @@ describe('truncate', () => { }); it('str must be a string', () => { - try { - truncate(); - } catch (err) { - err.should.have.property('message', 'str must be a string!'); - } + truncate.should.throw('str must be a string!'); }); }); diff --git a/test/scripts/word_wrap.js b/test/scripts/word_wrap.js index 96a52d89..0da8242f 100644 --- a/test/scripts/word_wrap.js +++ b/test/scripts/word_wrap.js @@ -1,6 +1,6 @@ 'use strict'; -const should = require('chai').should(); // eslint-disable-line +require('chai').should(); describe('wordWrap', () => { const wordWrap = require('../../lib/word_wrap'); @@ -23,10 +23,6 @@ describe('wordWrap', () => { }); it('str must be a string', () => { - try { - wordWrap(); - } catch (err) { - err.should.have.property('message', 'str must be a string!'); - } + wordWrap.should.throw('str must be a string!'); }); });