hexo-yam/test/js.test.js

139 lines
3.3 KiB
JavaScript

/* eslint-env jest */
'use strict'
const Hexo = require('hexo')
const { minify: terserMinify } = require('terser')
describe('js', () => {
const hexo = new Hexo(__dirname)
const j = require('../lib/js').minifyJs.bind(hexo)
const jm = require('../lib/js').minifyJsWithMap.bind(hexo)
const input = 'var o = { "foo": 1, bar: 3 };'
const path = 'foo.js'
let expected = ''
beforeAll(async () => {
const { code } = await terserMinify(input, { mangle: true })
expected = code
})
beforeEach(async () => {
hexo.config.minify = {
js: {
enable: true,
verbose: false,
exclude: ['*.min.js'],
compress: {},
mangle: true,
output: {},
globOptions: { basename: true }
}
}
hexo.route.set(path, input)
})
test('default', async () => {
const result = await j(input, { path })
expect(result).toBeDefined()
expect(expected).toBeDefined()
expect(result).toBe(expected)
})
test('default with map', async () => {
await jm()
const output = hexo.route.get(path)
let result = ''
output.on('data', (chunk) => (result += chunk))
output.on('end', () => {
expect(result).toBeDefined()
expect(expected).toBeDefined()
expect(result).toBe(expected + '\n//# sourceMappingURL=foo.js.map')
})
})
test('empty file', async () => {
const result = await j('', { path })
expect(result).toBe('')
})
test('empty file with map', async () => {
hexo.route.set(path, '')
const result = await jm()
expect(result).toBeDefined()
expect(result[0]).toBeUndefined()
})
test('option', async () => {
const customOpt = {
mangle: {
properties: true
}
}
hexo.config.minify.js = customOpt
const result = await j(input, { path })
const { code: expected } = await terserMinify(input, customOpt)
expect(result).toBe(expected)
})
test('option - verbose', async () => {
hexo.config.minify.js.verbose = true
hexo.log.log = jest.fn()
await j(input, { path })
expect(hexo.log.log.mock.calls[0][0]).toContain(`js: ${path}`)
})
// test('option - invalid', async () => {
// const customOpt = {
// mangle: {
// foo: 'bar'
// }
// }
// hexo.config.minify.js = customOpt
// let expected
// try {
// await terserMinify(input, customOpt).rejects
// } catch (err) {
// expected = err
// }
// expect(expected).toBeDefined()
// await expect(j(input, { path })).rejects.toThrow(`Path: ${path}\n${expected}`)
// })
test('exclude - *.min.js', async () => {
const result = await j(input, { path: 'foo/bar.min.js' })
expect(result).toBe(input)
})
test('exclude - basename', async () => {
const exclude = '*baz.js'
hexo.config.minify.js.exclude = exclude
const result = await j(input, { path: 'foo/barbaz.js' })
expect(result).toBe(input)
})
test('exclude - slash in pattern', async () => {
const exclude = '**/lectus/**/*.js'
hexo.config.minify.js.exclude = exclude
const result = await j(input, { path: 'eleifend/lectus/nullam/dapibus/netus.js' })
expect(result).toBe(input)
})
// test('invalid string', async () => {
// const invalid = 'console.log("\\");'
// await expect(j(invalid, { path })).rejects.toThrow(`Path: ${path}\nSyntaxError`)
// })
})