test: perf-loader

This commit is contained in:
Clark Du 2018-03-30 11:10:49 +08:00
parent c319a8c3d2
commit 84133d91c7
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
2 changed files with 34 additions and 3 deletions

View File

@ -8,7 +8,7 @@ import threadLoader from 'thread-loader'
export default class PerfLoader { export default class PerfLoader {
constructor(options) { constructor(options) {
this.options = options this.options = options
this.threadLoader = threadLoader
this.workerPools = { this.workerPools = {
js: { js: {
name: 'js', name: 'js',
@ -23,8 +23,8 @@ export default class PerfLoader {
} }
warmup() { warmup() {
threadLoader.warmup(this.workerPools.js, ['babel-loader', 'babel-preset-env']) this.threadLoader.warmup(this.workerPools.js, ['babel-loader', 'babel-preset-env'])
threadLoader.warmup(this.workerPools.css, ['css-loader']) this.threadLoader.warmup(this.workerPools.css, ['css-loader'])
} }
pool(poolName, _loaders) { pool(poolName, _loaders) {

View File

@ -1,4 +1,7 @@
import path from 'path'
import { Utils } from '../utils' import { Utils } from '../utils'
import PerfLoader from '../../lib/builder/webpack/utils/perf-loader'
describe('utils', () => { describe('utils', () => {
test('encodeHtml', () => { test('encodeHtml', () => {
@ -197,6 +200,34 @@ describe('utils', () => {
const chainedFn = Utils.chainFn(firstFn, secondFn) const chainedFn = Utils.chainFn(firstFn, secondFn)
expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 }) expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 })
}) })
test('performance loader', () => {
const perfLoader = new PerfLoader({
dev: true,
build: {
parallel: true,
cache: true
}
})
const js = { name: 'js', poolTimeout: Infinity }
const css = { name: 'css', poolTimeout: Infinity }
expect(perfLoader.workerPools).toMatchObject({ js, css })
const warmup = jest.fn()
perfLoader.threadLoader = { warmup }
perfLoader.warmup()
expect(warmup).toHaveBeenCalledTimes(2)
expect(warmup).toHaveBeenCalledWith(js, ['babel-loader', 'babel-preset-env'])
expect(warmup).toHaveBeenCalledWith(css, ['css-loader'])
const loaders = perfLoader.pool('js', { loader: 'test-perf-loader' })
const cacheDirectory = path.resolve('node_modules/.cache/cache-loader')
expect(loaders).toMatchObject([
{ loader: 'cache-loader', options: { cacheDirectory } },
{ loader: 'thread-loader', options: js },
{ loader: 'test-perf-loader' }
])
})
}) })
test('createRoutes should allow snake case routes', () => { test('createRoutes should allow snake case routes', () => {