Skip to content

Commit

Permalink
test: analyzeBundleSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Maiquu committed Jul 11, 2023
1 parent 0da655b commit 24eb937
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"changelogen": "^0.5.4",
"eslint": "^8.44.0",
"eslint-define-config": "^1.21.0",
"fast-folder-size": "^2.1.0",
"jsdom": "^22.1.0",
"nuxt": "^3.6.2",
"quasar": "^2.12.2",
Expand Down
27 changes: 25 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/__snapshots__/analyzeBundleSize.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Analyze Bundle Size > bundle size snapshots > e7a375265d7bbc05627f6431e3306dc1a25a91f0.client 1`] = `436139`;

exports[`Analyze Bundle Size > bundle size snapshots > e7a375265d7bbc05627f6431e3306dc1a25a91f0.nitro 1`] = `3917094`;
40 changes: 40 additions & 0 deletions test/analyzeBundleSize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { fileURLToPath } from 'node:url'
import { promisify } from 'node:util'
import { createHash } from 'node:crypto'
import { createReadStream } from 'node:fs'
import { finished } from 'node:stream/promises'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { setup, useTestContext } from '@nuxt/test-utils'
import folderSizeCallback from 'fast-folder-size'

const folderSize = promisify(folderSizeCallback)

describe('Analyze Bundle Size', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
server: false,
})

it('bundle size snapshots', async () => {
const ctx = useTestContext()
const { buildDir } = ctx.nuxt!.options

const lockFile = fileURLToPath(new URL('../pnpm-lock.yaml', import.meta.url))
const lockHash = await createFileHash(lockFile)

const clientBundle = await folderSize(join(buildDir, 'dist/client'))
const nitroBundle = await folderSize(join(buildDir, 'output'))

expect(clientBundle).toMatchSnapshot(`${lockHash}.client`)
expect(nitroBundle).toMatchSnapshot(`${lockHash}.nitro`)
})
})

async function createFileHash(path: string) {
const hash = createHash('sha1')
const stream = createReadStream(path)
stream.pipe(hash)
await finished(stream)
return hash.digest('hex')
}

0 comments on commit 24eb937

Please sign in to comment.