data.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import fs from 'node:fs/promises'
  2. import { fileURLToPath } from 'node:url'
  3. describe('static data file support in vite 3', () => {
  4. beforeAll(async () => {
  5. await goto('/data-loading/data')
  6. })
  7. test('render correct content', async () => {
  8. expect(await page.textContent('pre#basic')).toMatchInlineSnapshot(`
  9. "[
  10. {
  11. "a": true
  12. },
  13. {
  14. "b": true
  15. }
  16. ]"
  17. `)
  18. expect(await page.textContent('pre#content')).toMatchInlineSnapshot(`
  19. "[
  20. {
  21. "src": "---\\ntitle: bar\\n---\\n\\nHello\\n\\n---\\n\\nworld\\n",
  22. "html": "<p>Hello</p>\\n<hr>\\n<p>world</p>\\n",
  23. "frontmatter": {
  24. "title": "bar"
  25. },
  26. "excerpt": "<p>Hello</p>\\n",
  27. "url": "/data-loading/content/bar.html",
  28. "transformed": true
  29. },
  30. {
  31. "src": "---\\ntitle: foo\\n---\\n\\nHello\\n\\n---\\n\\nworld\\n",
  32. "html": "<p>Hello</p>\\n<hr>\\n<p>world</p>\\n",
  33. "frontmatter": {
  34. "title": "foo"
  35. },
  36. "excerpt": "<p>Hello</p>\\n",
  37. "url": "/data-loading/content/foo.html",
  38. "transformed": true
  39. }
  40. ]"
  41. `)
  42. })
  43. test.runIf(!process.env.VITE_TEST_BUILD)('hmr works', async () => {
  44. const a = fileURLToPath(new URL('./data/a.json', import.meta.url))
  45. const b = fileURLToPath(new URL('./data/b.json', import.meta.url))
  46. try {
  47. await fs.writeFile(a, JSON.stringify({ a: false }, null, 2) + '\n')
  48. await page.waitForFunction(
  49. () =>
  50. document.querySelector('pre#basic')?.textContent ===
  51. JSON.stringify([{ a: false }, { b: true }], null, 2),
  52. undefined,
  53. { timeout: 3000 }
  54. )
  55. } finally {
  56. await fs.writeFile(a, JSON.stringify({ a: true }, null, 2) + '\n')
  57. }
  58. let err = true
  59. try {
  60. await fs.unlink(b)
  61. await page.waitForFunction(
  62. () =>
  63. document.querySelector('pre#basic')?.textContent ===
  64. JSON.stringify([{ a: true }], null, 2),
  65. undefined,
  66. { timeout: 3000 }
  67. )
  68. err = false
  69. } finally {
  70. if (err) {
  71. await fs.writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n')
  72. }
  73. }
  74. try {
  75. await fs.writeFile(b, JSON.stringify({ b: false }, null, 2) + '\n')
  76. await page.waitForFunction(
  77. () =>
  78. document.querySelector('pre#basic')?.textContent ===
  79. JSON.stringify([{ a: true }, { b: false }], null, 2),
  80. undefined,
  81. { timeout: 3000 }
  82. )
  83. } finally {
  84. await fs.writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n')
  85. }
  86. })
  87. /*
  88. MODIFY a.json with { a: false }
  89. this should trigger a hmr update and the content should be updated to [{ a: false }, { b: true }]
  90. reset a.json
  91. DELETE b.json
  92. this should trigger a hmr update and the content should be updated to [{ a: true }]
  93. reset b.json if failed
  94. CREATE b.json with { b: false }
  95. this should trigger a hmr update and the content should be updated to [{ a: true }, { b: false }]
  96. reset b.json
  97. */
  98. })