init.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import fs from 'fs-extra'
  2. import getPort from 'get-port'
  3. import { nanoid } from 'nanoid'
  4. import path from 'node:path'
  5. import { fileURLToPath, URL } from 'node:url'
  6. import { chromium } from 'playwright-chromium'
  7. import { createServer, scaffold, ScaffoldThemeType } from 'vitepress'
  8. const tempDir = fileURLToPath(new URL('./.temp', import.meta.url))
  9. const getTempRoot = () => path.join(tempDir, nanoid())
  10. const browser = await chromium.launch({
  11. headless: !process.env.DEBUG,
  12. args: process.env.CI
  13. ? ['--no-sandbox', '--disable-setuid-sandbox']
  14. : undefined
  15. })
  16. const page = await browser.newPage()
  17. const themes = [
  18. ScaffoldThemeType.Default,
  19. ScaffoldThemeType.DefaultCustom,
  20. ScaffoldThemeType.Custom
  21. ]
  22. const usingTs = [false, true]
  23. const variations = themes.flatMap((theme) =>
  24. usingTs.map(
  25. (useTs) => [`${theme}${useTs ? ' + ts' : ''}`, { theme, useTs }] as const
  26. )
  27. )
  28. afterAll(async () => {
  29. await page.close()
  30. await browser.close()
  31. await fs.remove(tempDir)
  32. })
  33. test.each(variations)('init %s', async (_, { theme, useTs }) => {
  34. const root = getTempRoot()
  35. await fs.remove(root)
  36. scaffold({ root, theme, useTs, injectNpmScripts: false })
  37. const port = await getPort()
  38. const server = await createServer(root, { port })
  39. await server.listen()
  40. async function goto(path: string) {
  41. await page.goto(`http://localhost:${port}${path}`)
  42. await page.waitForSelector('#app div')
  43. }
  44. try {
  45. await goto('/')
  46. expect(await page.textContent('h1')).toMatch('My Awesome Project')
  47. await page.click('a[href="/markdown-examples.html"]')
  48. await page.waitForFunction('document.querySelector("pre code")')
  49. expect(await page.textContent('h1')).toMatch('Markdown Extension Examples')
  50. await goto('/')
  51. expect(await page.textContent('h1')).toMatch('My Awesome Project')
  52. await page.click('a[href="/api-examples.html"]')
  53. await page.waitForFunction('document.querySelector("pre code")')
  54. expect(await page.textContent('h1')).toMatch('Runtime API Examples')
  55. // teardown
  56. } finally {
  57. await server.close()
  58. }
  59. })