test: add test for modern bundle size (#6302)

This commit is contained in:
Xin Du (Clark) 2019-08-27 00:05:29 +01:00 committed by GitHub
parent c4c51e3faf
commit 61ef86e015
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 35 deletions

View File

@ -37,7 +37,6 @@
"@vue/test-utils": "^1.0.0-beta.29", "@vue/test-utils": "^1.0.0-beta.29",
"babel-eslint": "^10.0.3", "babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0", "babel-jest": "^24.9.0",
"cheerio": "^1.0.0-rc.3",
"codecov": "^3.5.0", "codecov": "^3.5.0",
"consola": "^2.10.1", "consola": "^2.10.1",
"cross-spawn": "^6.0.5", "cross-spawn": "^6.0.5",

View File

@ -7,7 +7,8 @@ const createData = async () => {
}, },
head: { head: {
title: 'Async Config!' title: 'Async Config!'
} },
modern: 'server'
} }
} }

View File

@ -1,41 +1,67 @@
import cheerio from 'cheerio'
import fetch from 'node-fetch'
import { getPort, loadFixture, Nuxt } from '../utils'
let port import { resolve } from 'path'
let nuxt = null import zlib from 'zlib'
const url = route => 'http://localhost:' + port + route import fs from 'fs-extra'
let responseSizes import pify from 'pify'
describe('size-limit test', () => { const gzip = pify(zlib.gzip)
beforeAll(async () => { const brotli = pify(zlib.brotliCompress)
const options = await loadFixture('async-config') const compressSize = (input, compressor) => compressor(input).then(data => data.length)
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort() const distDir = resolve(__dirname, '../fixtures/async-config/.nuxt/dist')
await nuxt.server.listen(port, '0.0.0.0')
const { html } = await nuxt.server.renderRoute('/') const getResourcesSize = async (mode) => {
// Get all script URLs from the HTML const { all } = await import(resolve(distDir, 'server', `${mode}.manifest.json`))
const $ = cheerio.load(html) const resources = all.filter(filename => filename.endsWith('.js'))
const scriptsUrls = $('script[src]') const sizes = { uncompressed: 0, gzip: 0, brotli: 0 }
.map((_, el) => $(el).attr('src')) for (const resource of resources) {
.get() const file = resolve(distDir, 'client', resource)
.map(url) const stat = await fs.stat(file)
const resourceUrls = [url('/'), ...scriptsUrls] sizes.uncompressed += stat.size / 1024
const fileContent = await fs.readFile(file)
sizes.gzip += await compressSize(fileContent, gzip) / 1024
sizes.brotli += await compressSize(fileContent, brotli) / 1024
}
return sizes
}
// Fetch all resources and get their size (bytes) describe('nuxt basic resources size limit', () => {
responseSizes = await Promise.all(resourceUrls.map(async (url) => { expect.extend({
const response = await fetch(url).then(res => res.text()) toBeWithinSize (received, size) {
return response.length const maxSize = size * 1.05
})) const minSize = size * 0.95
const pass = received >= minSize && received <= maxSize
return {
pass,
message: () =>
`expected ${received} to be within range ${minSize} - ${maxSize}`
}
}
}) })
it('should stay within the size boundaries', () => { it('should stay within the size limit range in legacy mode', async () => {
const responseSizeBytes = responseSizes.reduce((bytes, responseLength) => bytes + responseLength, 0) const legacyResourcesSize = await getResourcesSize('client')
const responseSizeKilobytes = Math.ceil(responseSizeBytes / 1024)
// Without gzip! const LEGACY_JS_RESOURCES_KB_SIZE = 194
expect(responseSizeKilobytes).toBeLessThanOrEqual(196) expect(legacyResourcesSize.uncompressed).toBeWithinSize(LEGACY_JS_RESOURCES_KB_SIZE)
const LEGACY_JS_RESOURCES_GZIP_KB_SIZE = 66
expect(legacyResourcesSize.gzip).toBeWithinSize(LEGACY_JS_RESOURCES_GZIP_KB_SIZE)
const LEGACY_JS_RESOURCES_BROTLI_KB_SIZE = 58
expect(legacyResourcesSize.brotli).toBeWithinSize(LEGACY_JS_RESOURCES_BROTLI_KB_SIZE)
})
it('should stay within the size limit range in modern mode', async () => {
const modernResourcesSize = await getResourcesSize('modern')
const MODERN_JS_RESOURCES_KB_SIZE = 172
expect(modernResourcesSize.uncompressed).toBeWithinSize(MODERN_JS_RESOURCES_KB_SIZE)
const MODERN_JS_RESOURCES_GZIP_KB_SIZE = 59
expect(modernResourcesSize.gzip).toBeWithinSize(MODERN_JS_RESOURCES_GZIP_KB_SIZE)
const MODERN_JS_RESOURCES_BROTLI_KB_SIZE = 52
expect(modernResourcesSize.brotli).toBeWithinSize(MODERN_JS_RESOURCES_BROTLI_KB_SIZE)
}) })
}) })

View File

@ -3125,7 +3125,7 @@ check-types@^8.0.3:
resolved "https://registry.npmjs.org/check-types/-/check-types-8.0.3.tgz#3356cca19c889544f2d7a95ed49ce508a0ecf552" resolved "https://registry.npmjs.org/check-types/-/check-types-8.0.3.tgz#3356cca19c889544f2d7a95ed49ce508a0ecf552"
integrity sha512-YpeKZngUmG65rLudJ4taU7VLkOCTMhNl/u4ctNC56LQS/zJTyNH0Lrtwm1tfTsbLlwvlfsA2d1c8vCf/Kh2KwQ== integrity sha512-YpeKZngUmG65rLudJ4taU7VLkOCTMhNl/u4ctNC56LQS/zJTyNH0Lrtwm1tfTsbLlwvlfsA2d1c8vCf/Kh2KwQ==
cheerio@^1.0.0-rc.2, cheerio@^1.0.0-rc.3: cheerio@^1.0.0-rc.2:
version "1.0.0-rc.3" version "1.0.0-rc.3"
resolved "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.3.tgz#094636d425b2e9c0f4eb91a46c05630c9a1a8bf6" resolved "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.3.tgz#094636d425b2e9c0f4eb91a46c05630c9a1a8bf6"
integrity sha512-0td5ijfUPuubwLUu0OBoe98gZj8C/AA+RW3v67GPlGOrvxWjZmBXiBCRU+I8VEiNyJzjth40POfHiz2RB3gImA== integrity sha512-0td5ijfUPuubwLUu0OBoe98gZj8C/AA+RW3v67GPlGOrvxWjZmBXiBCRU+I8VEiNyJzjth40POfHiz2RB3gImA==