hexo-yam/test/js.test.js

132 lines
2.9 KiB
JavaScript
Raw Normal View History

/* eslint-env jest */
'use strict'
const Hexo = require('hexo')
const { minify: jsMinify } = require('@swc/core')
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 () => {
const { code } = await jsMinify(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 }
}
}
})
test('default', async () => {
const result = await j(input, { path })
expect(result).toBeDefined()
expect(expected).toBeDefined()
expect(result).toBe(expected)
})
test('disable', async () => {
hexo.config.minify.js.enable = false
const result = await j(input, { path })
expect(result).toBeUndefined()
})
test('empty file', async () => {
const result = await j('', { path })
expect(result).toBeUndefined()
})
test('option', async () => {
const customOpt = {
mangle: {
properties: {}
}
}
hexo.config.minify.js = customOpt
const result = await j(input, { path })
const { code: expected } = await jsMinify(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
2020-09-05 11:58:20 +00:00
let expected
try {
await jsMinify(input, customOpt)
} catch (err) {
2020-09-05 11:58:20 +00:00
expected = err
}
2020-09-05 11:58:20 +00:00
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)
})
2020-05-25 08:36:21 +00:00
test('invalid string', async () => {
2020-05-25 08:36:21 +00:00
const invalid = 'console.log("\\");'
let expected
try {
await jsMinify(invalid)
} catch (err) {
expected = err
}
expect(expected).toBeDefined()
await expect(j(invalid, { path })).rejects.toThrow(`Path: ${path}\n${expected}`)
2020-05-25 08:36:21 +00:00
})
})