2018-11-07 22:34:14 +00:00
|
|
|
import cheerio from 'cheerio'
|
|
|
|
import fetch from 'node-fetch'
|
|
|
|
import { getPort, loadFixture, Nuxt } from '../utils'
|
|
|
|
|
|
|
|
let port
|
|
|
|
let nuxt = null
|
|
|
|
const url = route => 'http://localhost:' + port + route
|
|
|
|
let responseSizes
|
|
|
|
|
|
|
|
describe('size-limit test', () => {
|
|
|
|
beforeAll(async () => {
|
|
|
|
const options = await loadFixture('async-config')
|
|
|
|
nuxt = new Nuxt(options)
|
|
|
|
port = await getPort()
|
|
|
|
await nuxt.server.listen(port, '0.0.0.0')
|
|
|
|
|
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
|
|
|
// Get all script URLs from the HTML
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
const scriptsUrls = $('script[src]')
|
|
|
|
.map((_, el) => $(el).attr('src'))
|
|
|
|
.get()
|
|
|
|
.map(url)
|
|
|
|
const resourceUrls = [url('/'), ...scriptsUrls]
|
|
|
|
|
|
|
|
// Fetch all resources and get their size (bytes)
|
|
|
|
responseSizes = await Promise.all(resourceUrls.map(async (url) => {
|
|
|
|
const response = await fetch(url).then(res => res.text())
|
|
|
|
return response.length
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should stay within the size boundaries', () => {
|
|
|
|
const responseSizeBytes = responseSizes.reduce((bytes, responseLength) => bytes + responseLength, 0)
|
|
|
|
const responseSizeKilobytes = Math.ceil(responseSizeBytes / 1024)
|
|
|
|
// Without gzip!
|
2018-12-19 17:46:18 +00:00
|
|
|
expect(responseSizeKilobytes).toBeLessThanOrEqual(171)
|
2018-11-07 22:34:14 +00:00
|
|
|
})
|
|
|
|
})
|