cli-optimization.spec.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // @ts-check
  2. const { test, expect } = require("@playwright/test");
  3. test("optimized scripts run", async ({ page }) => {
  4. await page.goto("http://localhost:8989");
  5. // Expect the page to load the script after optimizations have been applied. The script
  6. // should add an editor to the page that shows a main function
  7. const main = page.locator("#main");
  8. await expect(main).toContainText("hi");
  9. // Expect the page to contain an image with the id "some_image"
  10. const image = page.locator("#some_image");
  11. await expect(image).toBeVisible();
  12. // Get the image src
  13. const src = await image.getAttribute("src");
  14. // Expect the page to contain an image with the id "some_image_with_the_same_url"
  15. const image2 = page.locator("#some_image_with_the_same_url");
  16. await expect(image2).toBeVisible();
  17. // Get the image src
  18. const src2 = await image2.getAttribute("src");
  19. // Expect the urls to be different
  20. expect(src).not.toEqual(src2);
  21. // Expect the page to contain an image with the id "some_image_without_hash"
  22. const image3 = page.locator("#some_image_without_hash");
  23. await expect(image3).toBeVisible();
  24. // Get the image src
  25. const src3 = await image3.getAttribute("src");
  26. // Expect the src to be without a hash
  27. expect(src3).toEqual("/assets/toasts.avif");
  28. });
  29. test("unused external assets are bundled", async ({ page }) => {
  30. await page.goto("http://localhost:8989");
  31. // Assert http://localhost:8989/assets/toasts.png is found even though it is not used in the page
  32. const response = await page.request.get(
  33. "http://localhost:8989/assets/toasts.png"
  34. );
  35. // Expect the response to be ok
  36. expect(response.status()).toBe(200);
  37. // make sure the response is an image
  38. expect(response.headers()["content-type"]).toBe("image/png");
  39. });