image.test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as cheerio from 'cheerio';
  2. import assert from 'node:assert/strict';
  3. import { after, before, describe, it } from 'node:test';
  4. import { loadFixture } from './test-utils.js';
  5. describe('Image', () => {
  6. /** @type {import('../../../astro/test/test-utils.js').Fixture} */
  7. let fixture;
  8. before(async () => {
  9. fixture = await loadFixture({
  10. root: './fixtures/image/',
  11. });
  12. await fixture.build();
  13. });
  14. it('build successful', async () => {
  15. assert.ok(await fixture.readFile('../.vercel/output/static/index.html'));
  16. });
  17. it('has link to vercel in build with proper attributes', async () => {
  18. const html = await fixture.readFile('../.vercel/output/static/index.html');
  19. const $ = cheerio.load(html);
  20. const img = $('#basic-image img');
  21. assert.equal(img.attr('src').startsWith('/_vercel/image?url=_astr'), true);
  22. assert.equal(img.attr('loading'), 'lazy');
  23. assert.equal(img.attr('width'), '225');
  24. });
  25. it('has proper vercel config', async () => {
  26. const vercelConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
  27. assert.deepEqual(vercelConfig.images, {
  28. sizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
  29. domains: ['astro.build'],
  30. remotePatterns: [
  31. {
  32. protocol: 'https',
  33. hostname: '**.amazonaws.com',
  34. },
  35. ],
  36. });
  37. });
  38. describe('dev', () => {
  39. let devServer;
  40. before(async () => {
  41. devServer = await fixture.startDevServer();
  42. });
  43. after(async () => {
  44. await devServer.stop();
  45. });
  46. it('has link to local image in dev with proper attributes', async () => {
  47. const html = await fixture.fetch('/').then((res) => res.text());
  48. const $ = cheerio.load(html);
  49. const img = $('#basic-image img');
  50. assert.equal(img.attr('src').startsWith('/_image?href='), true);
  51. assert.equal(img.attr('loading'), 'lazy');
  52. assert.equal(img.attr('width'), '225');
  53. });
  54. it('supports SVGs', async () => {
  55. const html = await fixture.fetch('/').then((res) => res.text());
  56. const $ = cheerio.load(html);
  57. const img = $('#svg img');
  58. const src = img.attr('src');
  59. const res = await fixture.fetch(src);
  60. assert.equal(res.status, 200);
  61. assert.equal(res.headers.get('content-type'), 'image/svg+xml');
  62. });
  63. });
  64. });