Files
cypress/tooling/packherd/test/packherd.spec.ts
T
Ryan Manuel b0c0eaa508 feat: introduce v8 snapshots to improve startup performance (#24295)
Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
Co-authored-by: Zach Bloomquist <git@chary.us>
Co-authored-by: Tyler Biethman <tbiethman@users.noreply.github.com>
Co-authored-by: Matt Henkes <mjhenkes@gmail.com>
Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>
Co-authored-by: Matt Schile <mschile@cypress.io>
Co-authored-by: Mark Noonan <mark@cypress.io>
Co-authored-by: Zachary Williams <ZachJW34@gmail.com>
Co-authored-by: Ben M <benm@cypress.io>
Co-authored-by: Zachary Williams <zachjw34@gmail.com>
Co-authored-by: astone123 <adams@cypress.io>
Co-authored-by: Bill Glesias <bglesias@gmail.com>
Co-authored-by: Emily Rohrbough <emilyrohrbough@yahoo.com>
Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
Co-authored-by: Adam Stone <adams@cypress.io>
Co-authored-by: Blue F <blue@cypress.io>
Co-authored-by: GitStart <1501599+gitstart@users.noreply.github.com>
Co-authored-by: Mike Plummer <mike-plummer@users.noreply.github.com>
Co-authored-by: Jordan <jordan@jpdesigning.com>
Co-authored-by: Sam Goodger <turbo@tailz.dev>
Co-authored-by: Colum Ferry <cferry09@gmail.com>
Co-authored-by: Stokes Player <stokes@cypress.io>
Co-authored-by: Vilhelm Melkstam <vilhelm.melkstam@gmail.com>
Co-authored-by: amehta265 <65267668+amehta265@users.noreply.github.com>
2022-10-31 20:20:27 -05:00

87 lines
3.2 KiB
TypeScript

import {
CreateBundle,
CreateBundleOpts,
CreateBundleResult,
CreateBundleOutputFile,
packherd,
} from '../src/packherd'
import { expect } from 'chai'
import type{ Metafile } from 'esbuild'
import Fixtures from '@tooling/system-tests'
import * as FixturesScaffold from '@tooling/system-tests/lib/dep-installer'
import path from 'path'
const MINIMAL_PROJECT = 'v8-snapshot/minimal'
const pathRelativeToCwd = (projectBaseDir, ...pathComponentsRelativeToProjectBaseDir) => {
return path.relative(process.cwd(), path.join(projectBaseDir, ...pathComponentsRelativeToProjectBaseDir)).split(path.sep).join(path.posix.sep)
}
describe('Packherd', () => {
let projectBaseDir
before(async () => {
Fixtures.remove()
await FixturesScaffold.scaffoldCommonNodeModules()
projectBaseDir = await Fixtures.scaffoldProject(MINIMAL_PROJECT)
await FixturesScaffold.scaffoldProjectNodeModules({ project: MINIMAL_PROJECT, updateLockFile: false, forceCopyDependencies: true })
})
it('resolves paths relative to entry and creates entry content', async () => {
const entryFile = require.resolve(path.join(projectBaseDir, 'entry.js'))
const { meta, bundle } = await packherd({ entryFile })
expect(Object.keys(meta.inputs)).to.deep.equal([
pathRelativeToCwd(projectBaseDir, 'node_modules', 'isobject', 'index.cjs.js'),
pathRelativeToCwd(projectBaseDir, 'node_modules', 'tmpfile', 'index.js'),
pathRelativeToCwd(projectBaseDir, 'entry.js'),
])
expect(meta.inputs[pathRelativeToCwd(projectBaseDir, 'node_modules', 'isobject', 'index.cjs.js')].bytes).to.be.gte(200)
expect(meta.inputs[pathRelativeToCwd(projectBaseDir, 'node_modules', 'tmpfile', 'index.js')].bytes).to.be.gte(800)
expect(meta.inputs[pathRelativeToCwd(projectBaseDir, 'entry.js')].bytes).to.be.gte(100)
expect(meta.inputs[pathRelativeToCwd(projectBaseDir, 'entry.js')].imports).to.deep.equal([
{
kind: 'require-call',
path: pathRelativeToCwd(projectBaseDir, 'node_modules', 'isobject', 'index.cjs.js'),
},
{
kind: 'require-call',
path: pathRelativeToCwd(projectBaseDir, 'node_modules', 'tmpfile', 'index.js'),
},
])
expect(bundle.length).to.be.gte(1700)
})
it('create custom bundle', async () => {
const bundleStub: CreateBundleOutputFile = {
contents: Buffer.from('// Unused bundle content', 'utf8'),
}
const metafile: Metafile = ({
inputs: {
'test/fixtures/minimal/node_modules/foo/foo.js': { bytes: 111 },
'test/fixtures/minimal/lib/bar.js': { bytes: 1 },
'test/fixtures/minimal/node_modules/baz/baz.js': { bytes: 222 },
},
} as unknown) as Metafile
const createBundle: CreateBundle = (_opts: CreateBundleOpts) => {
const result: CreateBundleResult = {
warnings: [],
outputFiles: [bundleStub],
metafile,
}
return Promise.resolve(result)
}
const entryFile = path.relative(projectBaseDir, require.resolve(path.join(projectBaseDir, 'entry.js')))
const { meta, bundle } = await packherd({ entryFile, createBundle })
expect(meta).to.deep.equal(metafile)
expect(bundle).to.equal(bundleStub.contents)
})
})