Nuxt/test/unit/async-config.size-limit.test.js
Sébastien Chopin 09878cd6dc
feat(vue-app): add $nuxt.refresh() (#6194)
* feat(vue-app): add $nuxt.refreshPageData()

* hotifx: lint

* chore: use getMatchedComponentsInstance util

* fix: rename to refreshPage

* fix: rename to refresh

* feat: add $nuxt.$context and handle error

* feat: set $nuxt.context

* hotfix: test
2019-08-07 15:12:02 +02:00

42 lines
1.3 KiB
JavaScript

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)
await nuxt.ready()
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!
expect(responseSizeKilobytes).toBeLessThanOrEqual(196)
})
})