refactor(test): set default config in individual test

This commit is contained in:
MDLeom 2020-09-05 11:18:35 +00:00
parent 16cdf467d9
commit ce8c4a145b
No known key found for this signature in database
GPG Key ID: 06C236E63CBC68AA
9 changed files with 199 additions and 155 deletions

168
index.js
View File

@ -1,89 +1,81 @@
/* global hexo */ /* global hexo */
'use strict' 'use strict'
const minifyDefault = { enable: true } hexo.config.minify = Object.assign({
const htmlDefault = {
enable: true, enable: true,
priority: 10, html: {
verbose: false, enable: true,
exclude: [], priority: 10,
collapseBooleanAttributes: true, verbose: false,
collapseWhitespace: true, exclude: [],
// Ignore '<!-- more -->' https://hexo.io/docs/tag-plugins#Post-Excerpt collapseBooleanAttributes: true,
ignoreCustomComments: [/^\s*more/], collapseWhitespace: true,
removeComments: true, // Ignore '<!-- more -->' https://hexo.io/docs/tag-plugins#Post-Excerpt
removeEmptyAttributes: true, ignoreCustomComments: [/^\s*more/],
removeScriptTypeAttributes: true, removeComments: true,
removeStyleLinkTypeAttributes: true, removeEmptyAttributes: true,
minifyJS: true, removeScriptTypeAttributes: true,
minifyCSS: true, removeStyleLinkTypeAttributes: true,
globOptions: { basename: true } minifyJS: true,
} minifyCSS: true,
const cssDefault = { globOptions: { basename: true }
enable: true, },
priority: 10, css: {
verbose: false, enable: true,
exclude: ['*.min.css'], priority: 10,
level: 2, verbose: false,
globOptions: { basename: true } exclude: ['*.min.css'],
} level: 2,
const jsDefault = { globOptions: { basename: true }
enable: true, },
priority: 10, js: {
verbose: false, enable: true,
exclude: ['*.min.js'], priority: 10,
compress: {}, verbose: false,
mangle: true, exclude: ['*.min.js'],
output: {}, compress: {},
globOptions: { basename: true } mangle: true,
} output: {},
const svgDefault = { globOptions: { basename: true }
enable: true, },
priority: 10, svg: {
verbose: false, enable: true,
include: ['*.svg', '!*.min.svg'], priority: 10,
plugins: [], verbose: false,
globOptions: { basename: true } include: ['*.svg', '!*.min.svg'],
} plugins: [],
const gzipDefault = { globOptions: { basename: true }
enable: true, },
priority: 10, gzip: {
verbose: false, enable: true,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], priority: 10,
globOptions: { basename: true } verbose: false,
} include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
const brotliDefault = { globOptions: { basename: true }
enable: true, },
priority: 10, brotli: {
verbose: false, enable: true,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], priority: 10,
globOptions: { basename: true } verbose: false,
} include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
const xmlDefault = { globOptions: { basename: true }
enable: false, },
priority: 10, xml: {
verbose: false, enable: false,
include: ['*.xml', '!*.min.xml'], priority: 10,
removeComments: true, verbose: false,
globOptions: { basename: true } include: ['*.xml', '!*.min.xml'],
} removeComments: true,
const jsonDefault = { globOptions: { basename: true }
enable: false, },
priority: 10, json: {
verbose: false, enable: false,
include: ['*.json', '!*.min.json'], priority: 10,
globOptions: { basename: true } verbose: false,
} include: ['*.json', '!*.min.json'],
globOptions: { basename: true }
hexo.config.minify = Object.assign(minifyDefault, hexo.config.minify) }
hexo.config.minify.html = Object.assign(htmlDefault, hexo.config.minify.html) }, hexo.config.minify)
hexo.config.minify.css = Object.assign(cssDefault, hexo.config.minify.css)
hexo.config.minify.js = Object.assign(jsDefault, hexo.config.minify.js)
hexo.config.minify.svg = Object.assign(svgDefault, hexo.config.minify.svg)
hexo.config.minify.gzip = Object.assign(gzipDefault, hexo.config.minify.gzip)
hexo.config.minify.brotli = Object.assign(brotliDefault, hexo.config.minify.brotli)
hexo.config.minify.xml = Object.assign(xmlDefault, hexo.config.minify.xml)
hexo.config.minify.json = Object.assign(jsonDefault, hexo.config.minify.json)
if (hexo.config.minify.enable === true) { if (hexo.config.minify.enable === true) {
const filter = require('./lib/filter') const filter = require('./lib/filter')
@ -96,15 +88,3 @@ if (hexo.config.minify.enable === true) {
hexo.extend.filter.register('after_generate', filter.minifyXml, hexo.config.minify.xml.priority) hexo.extend.filter.register('after_generate', filter.minifyXml, hexo.config.minify.xml.priority)
hexo.extend.filter.register('after_generate', filter.minifyJson, hexo.config.minify.json.priority) hexo.extend.filter.register('after_generate', filter.minifyJson, hexo.config.minify.json.priority)
} }
module.exports = {
minifyDefault,
htmlDefault,
cssDefault,
jsDefault,
svgDefault,
gzipDefault,
brotliDefault,
xmlDefault,
jsonDefault
}

View File

@ -2,20 +2,26 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { brotliDefault } = require('../index')
const b = require('../lib/filter').brotliFn.bind(hexo)
const zlib = require('zlib') const zlib = require('zlib')
const { promisify } = require('util') const { promisify } = require('util')
const brotli = promisify(zlib.brotliCompress) const brotli = promisify(zlib.brotliCompress)
const unbrotli = promisify(zlib.brotliDecompress) const unbrotli = promisify(zlib.brotliDecompress)
const path = 'foo.txt'
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
describe('brotli', () => { describe('brotli', () => {
const hexo = new Hexo(__dirname)
const b = require('../lib/filter').brotliFn.bind(hexo)
const path = 'foo.txt'
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
beforeEach(() => { beforeEach(() => {
hexo.config.minify.brotli = Object.assign({}, brotliDefault) hexo.config.minify = {
brotli: {
enable: true,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}
}
hexo.route.set(path, input) hexo.route.set(path, input)
}) })

View File

@ -2,17 +2,24 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { cssDefault } = require('../index')
const c = require('../lib/filter').minifyCss.bind(hexo)
const CleanCSS = require('clean-css') const CleanCSS = require('clean-css')
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const path = 'foo.css'
describe('css', () => { describe('css', () => {
const hexo = new Hexo(__dirname)
const c = require('../lib/filter').minifyCss.bind(hexo)
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const path = 'foo.css'
beforeEach(() => { beforeEach(() => {
hexo.config.minify.css = Object.assign({}, cssDefault) hexo.config.minify = {
css: {
enable: true,
verbose: false,
exclude: ['*.min.css'],
level: 2,
globOptions: { basename: true }
}
}
}) })
test('default', async () => { test('default', async () => {

View File

@ -2,20 +2,26 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { gzipDefault } = require('../index')
const g = require('../lib/filter').gzipFn.bind(hexo)
const zlib = require('zlib') const zlib = require('zlib')
const { promisify } = require('util') const { promisify } = require('util')
const gzip = promisify(zlib.gzip) const gzip = promisify(zlib.gzip)
const unzip = promisify(zlib.unzip) const unzip = promisify(zlib.unzip)
const path = 'foo.txt'
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
describe('gzip', () => { describe('gzip', () => {
const hexo = new Hexo(__dirname)
const g = require('../lib/filter').gzipFn.bind(hexo)
const path = 'foo.txt'
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
beforeEach(() => { beforeEach(() => {
hexo.config.minify.gzip = Object.assign({}, gzipDefault) hexo.config.minify = {
gzip: {
enable: true,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}
}
hexo.route.set(path, input) hexo.route.set(path, input)
}) })

View File

@ -2,18 +2,34 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { htmlDefault } = require('../index')
const h = require('../lib/filter').minifyHtml.bind(hexo)
const Htmlminifier = require('html-minifier').minify const Htmlminifier = require('html-minifier').minify
const input = '<p id="">foo</p>'
const path = 'index.html'
const expected = Htmlminifier(input, htmlDefault)
describe('html', () => { describe('html', () => {
const hexo = new Hexo(__dirname)
const h = require('../lib/filter').minifyHtml.bind(hexo)
const input = '<p id="">foo</p>'
const path = 'index.html'
const defaultCfg = {
html: {
enable: true,
verbose: false,
exclude: [],
collapseBooleanAttributes: true,
collapseWhitespace: true,
ignoreCustomComments: [/^\s*more/],
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyJS: true,
minifyCSS: true,
globOptions: { basename: true }
}
}
const expected = Htmlminifier(input, defaultCfg.html)
beforeEach(() => { beforeEach(() => {
hexo.config.minify.html = Object.assign({}, htmlDefault) hexo.config.minify = JSON.parse(JSON.stringify(defaultCfg))
}) })
test('default', () => { test('default', () => {

View File

@ -2,23 +2,32 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { jsDefault } = require('../index')
const j = require('../lib/filter').minifyJs.bind(hexo)
const { minify: terserMinify } = require('terser') const { minify: terserMinify } = require('terser')
const input = 'var o = { "foo": 1, bar: 3 };'
const path = 'foo.js'
let expected = ''
describe('js', () => { describe('js', () => {
const hexo = new Hexo(__dirname)
const j = require('../lib/filter').minifyJs.bind(hexo)
const input = 'var o = { "foo": 1, bar: 3 };'
const path = 'foo.js'
let expected = ''
beforeAll(async () => { beforeAll(async () => {
const { code } = await terserMinify(input, { mangle: jsDefault.mangle }) const { code } = await terserMinify(input, { mangle: true })
expected = code expected = code
}) })
beforeEach(async () => { beforeEach(async () => {
hexo.config.minify.js = Object.assign({}, jsDefault) hexo.config.minify = {
js: {
enable: true,
verbose: false,
exclude: ['*.min.js'],
compress: {},
mangle: true,
output: {},
globOptions: { basename: true }
}
}
}) })
test('default', async () => { test('default', async () => {

View File

@ -2,17 +2,23 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { jsonDefault } = require('../index')
const jsonFn = require('../lib/filter').minifyJson.bind(hexo)
const path = 'foo.json'
const input = '{\n\t"vitae": "hendrerit",\n\t"tristique": [\n\t\t"primis",\n\t\t"quam"\n\t]\n}'
const expected = '{"vitae":"hendrerit","tristique":["primis","quam"]}'
describe('xml', () => { describe('xml', () => {
const hexo = new Hexo(__dirname)
const jsonFn = require('../lib/filter').minifyJson.bind(hexo)
const path = 'foo.json'
const input = '{\n\t"vitae": "hendrerit",\n\t"tristique": [\n\t\t"primis",\n\t\t"quam"\n\t]\n}'
const expected = '{"vitae":"hendrerit","tristique":["primis","quam"]}'
beforeEach(() => { beforeEach(() => {
hexo.config.minify.json = Object.assign({}, jsonDefault) hexo.config.minify = {
json: {
enable: false,
verbose: false,
include: ['*.json', '!*.min.json'],
globOptions: { basename: true }
}
}
// plugin is disabled by default // plugin is disabled by default
hexo.config.minify.json.enable = true hexo.config.minify.json.enable = true
hexo.route.set(path, input) hexo.route.set(path, input)

View File

@ -2,17 +2,24 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { svgDefault } = require('../index')
const s = require('../lib/filter').minifySvg.bind(hexo)
const Svgo = require('svgo') const Svgo = require('svgo')
const input = '<svg><rect x="1" y="2" width="3" height="4" id="a"/></svg>'
const path = 'foo.svg'
describe('svg', () => { describe('svg', () => {
const hexo = new Hexo(__dirname)
const s = require('../lib/filter').minifySvg.bind(hexo)
const input = '<svg><rect x="1" y="2" width="3" height="4" id="a"/></svg>'
const path = 'foo.svg'
beforeEach(() => { beforeEach(() => {
hexo.config.minify.svg = Object.assign({}, svgDefault) hexo.config.minify = {
svg: {
enable: true,
verbose: false,
include: ['*.svg', '!*.min.svg'],
plugins: [],
globOptions: { basename: true }
}
}
hexo.route.set(path, input) hexo.route.set(path, input)
}) })

View File

@ -2,17 +2,24 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo
const { xmlDefault } = require('../index')
const x = require('../lib/filter').minifyXml.bind(hexo)
const path = 'foo.xml'
const input = '<?xml version="1.0" encoding="utf-8"?>\n<feed xmlns="http://www.w3.org/2005/Atom">\n <!-- foo bar -->\n <title>foo</title>\n</feed>'
const expected = '<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>foo</title></feed>'
describe('xml', () => { describe('xml', () => {
const hexo = new Hexo(__dirname)
const x = require('../lib/filter').minifyXml.bind(hexo)
const path = 'foo.xml'
const input = '<?xml version="1.0" encoding="utf-8"?>\n<feed xmlns="http://www.w3.org/2005/Atom">\n <!-- foo bar -->\n <title>foo</title>\n</feed>'
const expected = '<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>foo</title></feed>'
beforeEach(() => { beforeEach(() => {
hexo.config.minify.xml = Object.assign({}, xmlDefault) hexo.config.minify = {
xml: {
enable: false,
verbose: false,
include: ['*.xml', '!*.min.xml'],
removeComments: true,
globOptions: { basename: true }
}
}
// plugin is disabled by default // plugin is disabled by default
hexo.config.minify.xml.enable = true hexo.config.minify.xml.enable = true
hexo.route.set(path, input) hexo.route.set(path, input)