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

View File

@ -1,8 +1,9 @@
/* global hexo */
'use strict'
const minifyDefault = { enable: true }
const htmlDefault = {
hexo.config.minify = Object.assign({
enable: true,
html: {
enable: true,
priority: 10,
verbose: false,
@ -18,16 +19,16 @@ const htmlDefault = {
minifyJS: true,
minifyCSS: true,
globOptions: { basename: true }
}
const cssDefault = {
},
css: {
enable: true,
priority: 10,
verbose: false,
exclude: ['*.min.css'],
level: 2,
globOptions: { basename: true }
}
const jsDefault = {
},
js: {
enable: true,
priority: 10,
verbose: false,
@ -36,54 +37,45 @@ const jsDefault = {
mangle: true,
output: {},
globOptions: { basename: true }
}
const svgDefault = {
},
svg: {
enable: true,
priority: 10,
verbose: false,
include: ['*.svg', '!*.min.svg'],
plugins: [],
globOptions: { basename: true }
}
const gzipDefault = {
},
gzip: {
enable: true,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}
const brotliDefault = {
},
brotli: {
enable: true,
priority: 10,
verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}
const xmlDefault = {
},
xml: {
enable: false,
priority: 10,
verbose: false,
include: ['*.xml', '!*.min.xml'],
removeComments: true,
globOptions: { basename: true }
}
const jsonDefault = {
},
json: {
enable: false,
priority: 10,
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.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)
}, hexo.config.minify)
if (hexo.config.minify.enable === true) {
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.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'
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 { promisify } = require('util')
const brotli = promisify(zlib.brotliCompress)
const unbrotli = promisify(zlib.brotliDecompress)
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'
describe('brotli', () => {
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)
})

View File

@ -2,17 +2,24 @@
'use strict'
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')
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'
describe('css', () => {
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 () => {

View File

@ -2,20 +2,26 @@
'use strict'
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 { promisify } = require('util')
const gzip = promisify(zlib.gzip)
const unzip = promisify(zlib.unzip)
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'
describe('gzip', () => {
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)
})

View File

@ -2,18 +2,34 @@
'use strict'
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 input = '<p id="">foo</p>'
const path = 'index.html'
const expected = Htmlminifier(input, htmlDefault)
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(() => {
hexo.config.minify.html = Object.assign({}, htmlDefault)
hexo.config.minify = JSON.parse(JSON.stringify(defaultCfg))
})
test('default', () => {

View File

@ -2,23 +2,32 @@
'use strict'
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')
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 = ''
describe('js', () => {
beforeAll(async () => {
const { code } = await terserMinify(input, { mangle: jsDefault.mangle })
const { code } = await terserMinify(input, { mangle: true })
expected = code
})
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 () => {

View File

@ -2,17 +2,23 @@
'use strict'
const Hexo = require('hexo')
describe('xml', () => {
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', () => {
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
hexo.config.minify.json.enable = true
hexo.route.set(path, input)

View File

@ -2,17 +2,24 @@
'use strict'
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')
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'
describe('svg', () => {
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)
})

View File

@ -2,17 +2,24 @@
'use strict'
const Hexo = require('hexo')
describe('xml', () => {
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', () => {
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
hexo.config.minify.xml.enable = true
hexo.route.set(path, input)