browserPackaging.test.ts 1.7 KB

12345678910111213141516171819202122
  1. // @vitest-environment node
  2. import {expect,it} from 'vitest'
  3. import {build} from 'esbuild'
  4. import {mkdtemp,rm,readdir,readFile} from 'node:fs/promises'
  5. import {tmpdir} from 'node:os'
  6. import {join} from 'node:path'
  7. import {execFileSync} from 'node:child_process'
  8. // @ts-expect-error Build helper is plain JavaScript.
  9. import {browserRuntimeExternals,bundleBrowserRuntime} from '../../scripts/bundle-browser-runtime.mjs'
  10. it('imports the relocated browser runtime without the repository node_modules or copied Chromium caches',async()=>{
  11. const directory=await mkdtemp(join(tmpdir(),'yaoyao-browser-package-'))
  12. try{
  13. await build({stdin:{contents:'const {chromium}=await import("playwright"); console.log(JSON.stringify({path:chromium.executablePath(),version:(await import("playwright/package.json",{with:{type:"json"}})).default.version}))',resolveDir:process.cwd(),sourcefile:'browser-probe.mjs'},outfile:join(directory,'browser-probe.mjs'),bundle:true,platform:'node',format:'esm',target:'node24',external:browserRuntimeExternals})
  14. await bundleBrowserRuntime(directory)
  15. const result=JSON.parse(execFileSync(process.execPath,[join(directory,'browser-probe.mjs')],{cwd:directory,encoding:'utf8'}))
  16. const manifest=JSON.parse(await readFile(new URL('../../package.json',import.meta.url),'utf8'))
  17. expect(result.version).toBe(manifest.dependencies.playwright);expect(result.path).toContain('chromium')
  18. expect(await readdir(join(directory,'node_modules'))).toEqual(['playwright','playwright-core'])
  19. expect(await readdir(join(directory,'node_modules','playwright-core'))).not.toContain('.local-browsers')
  20. }finally{await rm(directory,{recursive:true,force:true})}
  21. },20000)