Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
test(node): add image tests (#459)
Browse files Browse the repository at this point in the history
* chore(node): add image tests

* Format

* Format
  • Loading branch information
ascorbic authored Dec 3, 2024
1 parent 3d94421 commit 26976ad
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
5 changes: 4 additions & 1 deletion packages/node/test/fixtures/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
"astro": "^5.0.0",
"@astrojs/node": "workspace:*"
},
"peerDependencies": {
"sharp": "^0.33.5"
},
"scripts": {
"build": "astro build",
"preview": "astro preview"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
import { Image } from "astro:assets";
import penguin from "../assets/some_penguin.png";
export const prerender = false;
---

<Image src={penguin} alt="Penguins" width={50} />
<Image src="https://images.unsplash.com/photo-1707229190979-f2d99f7823a8?q=80&w=800&auto=format&fit=crop" alt="Cornwall" width={400} height={300} />
6 changes: 0 additions & 6 deletions packages/node/test/fixtures/image/src/pages/index.astro

This file was deleted.

32 changes: 25 additions & 7 deletions packages/node/test/image.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { inferRemoteSize } from 'astro/assets/utils/inferRemoteSize.js';
import * as cheerio from 'cheerio';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';

// Temporary skip until we figure out the "Could not find Sharp" issue as `sharp` is bundled
describe.skip('Image endpoint', () => {
describe('Image endpoint', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devPreview;
Expand All @@ -14,6 +15,9 @@ describe.skip('Image endpoint', () => {
root: './fixtures/image/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
image: {
domains: ['images.unsplash.com'],
},
});
await fixture.build();
devPreview = await fixture.preview();
Expand All @@ -23,14 +27,28 @@ describe.skip('Image endpoint', () => {
await devPreview.stop();
});

it('it returns images', async () => {
it('it returns local images', async () => {
const res = await fixture.fetch('/');
assert.equal(res.status, 200);
const html = await res.text();
const $ = cheerio.load(html);

const resImage = await fixture.fetch(
'/_image?href=/_astro/some_penguin.97ef5f92.png&w=50&f=webp'
);
const img = $('img[alt=Penguins]').attr('src');
const size = await inferRemoteSize(`http://localhost:4321${img}`);
assert.equal(size.format, 'webp');
assert.equal(size.width, 50);
assert.equal(size.height, 33);
});

assert.equal(resImage.status, 200);
it('it returns remote images', async () => {
const res = await fixture.fetch('/');
assert.equal(res.status, 200);
const html = await res.text();
const $ = cheerio.load(html);
const img = $('img[alt=Cornwall]').attr('src');
const size = await inferRemoteSize(`http://localhost:4321${img}`);
assert.equal(size.format, 'webp');
assert.equal(size.width, 400);
assert.equal(size.height, 300);
});
});
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 26976ad

Please sign in to comment.