Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/integrations/netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"test": "pnpm run test-fn && pnpm run test-static && pnpm run test:dev",
"test-fn": "astro-scripts test \"test/functions/*.test.js\"",
"test:dev": "astro-scripts test \"test/development/*.test.js\"",
"test-static": "astro-scripts test \"test/static/*.test.js\"",
"test:hosted": "astro-scripts test \"test/hosted/*.test.js\""
"test-fn": "astro-scripts test \"test/functions/*.test.ts\"",
"test:dev": "astro-scripts test \"test/development/*.test.ts\"",
"test-static": "astro-scripts test \"test/static/*.test.ts\"",
"test:hosted": "astro-scripts test \"test/hosted/*.test.ts\"",
"typecheck:tests": "tsc --build tsconfig.test.json"
},
"dependencies": {
"@astrojs/internal-helpers": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import assert from 'node:assert/strict';
import { after, afterEach, before, describe, it } from 'node:test';

import * as cheerio from 'cheerio';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import netlifyAdapter from '../../dist/index.js';
import { type DevServer, type Fixture, loadFixture } from '../test-utils.ts';

describe('Netlify primitives', () => {
describe('Development', () => {
/** @type {import('../../../../astro/test/test-utils').Fixture} */
let fixture;
let devServer;
let fixture: Fixture;
let devServer: DevServer;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/primitives/', import.meta.url),
Expand Down Expand Up @@ -101,7 +100,7 @@ describe('Netlify primitives', () => {
}
} finally {
await cdnDisabledServer.stop();
process.env.DISABLE_IMAGE_CDN = undefined;
delete process.env.DISABLE_IMAGE_CDN;
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import { type Fixture, loadFixture } from '../test-utils.ts';

describe('Cookies', { timeout: 120000 }, () => {
let fixture;
let fixture: Fixture;

before(async () => {
fixture = await loadFixture({ root: new URL('./fixtures/cookies/', import.meta.url) });
Expand All @@ -15,7 +15,7 @@ describe('Cookies', { timeout: 120000 }, () => {
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const { default: handler } = await import(entryURL.href);
const resp = await handler(
new Request('http://example.com/login', { method: 'POST', body: '{}' }),
{},
Expand All @@ -30,10 +30,10 @@ describe('Cookies', { timeout: 120000 }, () => {
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const { default: handler } = await import(entryURL.href);
const resp = await handler(new Request('http://example.com/partitioned'), {});
assert.equal(resp.status, 200);
const cookie = resp.headers.getSetCookie()[0];
const cookie = resp.headers.getSetCookie()[0]!;
assert.ok(cookie.includes('Partitioned'), 'Cookie should include Partitioned attribute');
});

Expand All @@ -42,7 +42,7 @@ describe('Cookies', { timeout: 120000 }, () => {
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const { default: handler } = await import(entryURL.href);
const resp = await handler(
new Request('http://example.com/nonexistant-page', {
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import { type Fixture, loadFixture } from '../test-utils.ts';

describe('Middleware', { timeout: 120000 }, () => {
const root = new URL('./fixtures/middleware/', import.meta.url);

describe('middlewareMode: classic', () => {
let fixture;
let fixture: Fixture;
before(async () => {
process.env.EDGE_MIDDLEWARE = 'false';
fixture = await loadFixture({ root });
Expand All @@ -27,13 +27,13 @@ describe('Middleware', { timeout: 120000 }, () => {
});

after(async () => {
process.env.EDGE_MIDDLEWARE = undefined;
delete process.env.EDGE_MIDDLEWARE;
await fixture.clean();
});
});

describe('middlewareMode: edge', () => {
let fixture;
let fixture: Fixture;
before(async () => {
process.env.EDGE_MIDDLEWARE = 'true';
fixture = await loadFixture({ root });
Expand All @@ -53,7 +53,7 @@ describe('Middleware', { timeout: 120000 }, () => {
});

after(async () => {
process.env.EDGE_MIDDLEWARE = undefined;
delete process.env.EDGE_MIDDLEWARE;
await fixture.clean();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { remotePatternToRegex } from '@astrojs/netlify';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import imageService from '../../dist/image-service.js';
import { loadFixture, SpyIntegrationLogger } from '../test-utils.ts';
import type { ImageTransform } from 'astro';

async function getURL(options: ImageTransform) {
return await imageService.getURL(
options,
// @ts-expect-error The second argument is not used in the current
// implementation of `imageService.getURL`, but we need to pass it
// to satisfy the type signature.
{},
);
}

describe('Image CDN', { timeout: 120000 }, () => {
const root = new URL('./fixtures/middleware/', import.meta.url);

describe('configuration', () => {
after(() => {
process.env.DISABLE_IMAGE_CDN = undefined;
delete process.env.DISABLE_IMAGE_CDN;
});

it('enables Netlify Image CDN', async () => {
Expand All @@ -31,15 +42,17 @@ describe('Image CDN', { timeout: 120000 }, () => {
});

describe('remote image config', () => {
let regexes;
let regexes: RegExp[];

before(async () => {
const fixture = await loadFixture({ root });
await fixture.build();

const config = await fixture.readFile('../.netlify/v1/config.json');
if (config) {
regexes = JSON.parse(config).images.remote_images.map((pattern) => new RegExp(pattern));
regexes = JSON.parse(config).images.remote_images.map(
(pattern: string) => new RegExp(pattern),
);
}
});

Expand All @@ -48,7 +61,7 @@ describe('Image CDN', { timeout: 120000 }, () => {
});

it('generates correct config for domains', async () => {
const domain = regexes[0];
const domain = regexes[0]!;
assert.equal(domain.test('https://example.net/image.jpg'), true);
assert.equal(
domain.test('https://www.example.net/image.jpg'),
Expand All @@ -61,7 +74,7 @@ describe('Image CDN', { timeout: 120000 }, () => {
true,
'subpath should match',
);
const subdomain = regexes[1];
const subdomain = regexes[1]!;
assert.equal(
subdomain.test('https://secret.example.edu/image.jpg'),
true,
Expand All @@ -75,7 +88,7 @@ describe('Image CDN', { timeout: 120000 }, () => {
});

it('generates correct config for remotePatterns', async () => {
const patterns = regexes[2];
const patterns = regexes[2]!;
assert.equal(patterns.test('https://example.org/images/1.jpg'), true, 'should match domain');
assert.equal(
patterns.test('https://www.example.org/images/2.jpg'),
Expand All @@ -94,10 +107,8 @@ describe('Image CDN', { timeout: 120000 }, () => {
);
});

it('warns when remotepatterns generates an invalid regex', async (t) => {
const logger = {
warn: t.mock.fn(),
};
it('warns when remotepatterns generates an invalid regex', async () => {
const logger = new SpyIntegrationLogger();
const regex = remotePatternToRegex(
{
hostname: '*.examp[le.org',
Expand All @@ -106,18 +117,19 @@ describe('Image CDN', { timeout: 120000 }, () => {
logger,
);
assert.strictEqual(regex, undefined);
const calls = logger.warn.mock.calls;
assert.strictEqual(calls.length, 1);
assert.strictEqual(logger.messages.length, 1);
const message = logger.messages[0];
assert.equal(message.level, 'warn');
assert.equal(
calls[0].arguments[0],
message.message,
'Could not generate a valid regex from the remotePattern "{"hostname":"*.examp[le.org","pathname":"/images/*"}". Please check the syntax.',
);
});
});

describe('fit parameter', () => {
it('includes fit parameter in image URL', () => {
const url = imageService.getURL({
it('includes fit parameter in image URL', async () => {
const url = await getURL({
src: 'images/astronaut.jpg',
width: 300,
height: 400,
Expand All @@ -127,7 +139,7 @@ describe('Image CDN', { timeout: 120000 }, () => {
assert.ok(url.includes('fit=cover'), `Expected fit=cover in URL, got: ${url}`);
});

it('maps Astro fit values to Netlify equivalents', () => {
it('maps Astro fit values to Netlify equivalents', async () => {
const cases = [
['contain', 'contain'],
['cover', 'cover'],
Expand All @@ -137,7 +149,7 @@ describe('Image CDN', { timeout: 120000 }, () => {
['scale-down', 'contain'],
];
for (const [astroFit, netlifyFit] of cases) {
const url = imageService.getURL({
const url = await getURL({
src: 'img.jpg',
width: 100,
height: 100,
Expand All @@ -150,8 +162,8 @@ describe('Image CDN', { timeout: 120000 }, () => {
}
});

it('omits fit parameter when fit is none or unset', () => {
const withNone = imageService.getURL({
it('omits fit parameter when fit is none or unset', async () => {
const withNone = await getURL({
src: 'img.jpg',
width: 100,
height: 100,
Expand All @@ -162,7 +174,7 @@ describe('Image CDN', { timeout: 120000 }, () => {
`Expected no fit param for fit="none", got: ${withNone}`,
);

const withoutFit = imageService.getURL({ src: 'img.jpg', width: 100, height: 100 });
const withoutFit = await getURL({ src: 'img.jpg', width: 100, height: 100 });
assert.ok(
!withoutFit.includes('fit='),
`Expected no fit param when unset, got: ${withoutFit}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { after, before, describe, it } from 'node:test';
import netlify from '@astrojs/netlify';
import * as cheerio from 'cheerio';
import { globSync } from 'tinyglobby';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import { type Fixture, loadFixture } from '../test-utils.ts';

describe('Included vite assets files', { timeout: 120000 }, () => {
let fixture;
let fixture: Fixture;

const root = new URL('./fixtures/includes/', import.meta.url);
const expectedCwd = new URL('.netlify/v1/functions/ssr/packages/integrations/netlify/', root);
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Included vite assets files', { timeout: 120000 }, () => {
});

describe('Included files', { timeout: 120000 }, () => {
let fixture;
let fixture: Fixture;

const root = new URL('./fixtures/includes/', import.meta.url);
const expectedCwd = new URL(
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('Included files', { timeout: 120000 }, () => {
'./fixtures/includes/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const { default: handler } = await import(entryURL.href);
const resp = await handler(new Request('http://example.com/?file=include-this.txt'), {});
const html = await resp.text();
const $ = cheerio.load(html);
Expand All @@ -102,7 +102,7 @@ describe('Included files', { timeout: 120000 }, () => {
'.netlify/v1/functions/ssr/node_modules/.pnpm/cowsay@1.6.0/node_modules/cowsay/cows/happy-whale.cow',
root,
);
assert.ok(existsSync(expected, 'Expected excluded file to exist in default build'));
assert.ok(existsSync(expected), 'Expected excluded file to exist in default build');
});

after(async () => {
Expand All @@ -111,7 +111,7 @@ describe('Included files', { timeout: 120000 }, () => {
});

describe('Excluded files', { timeout: 120000 }, () => {
let fixture;
let fixture: Fixture;

const root = new URL('./fixtures/includes/', import.meta.url);
const expectedCwd = new URL(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { before, describe, it } from 'node:test';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import { type Fixture, loadFixture } from '../test-utils.ts';

describe('SSR - Redirects', { timeout: 120000 }, () => {
let fixture;
let fixture: Fixture;

before(async () => {
fixture = await loadFixture({ root: new URL('./fixtures/redirects/', import.meta.url) });
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('SSR - Redirects', { timeout: 120000 }, () => {
'./fixtures/redirects/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const { default: handler } = await import(entryURL.href);
const resp = await handler(new Request('http://example.com/nonexistant-page'), {});
assert.equal(resp.status, 404);
assert.equal(resp.headers.get('content-type'), 'text/html; charset=utf-8');
Expand All @@ -53,7 +53,7 @@ describe('SSR - Redirects', { timeout: 120000 }, () => {
'./fixtures/redirects/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const { default: handler } = await import(entryURL.href);
const resp = await handler(new Request('http://localhost:5678/nonexistant-page'), {});
assert.equal(resp.status, 404);
assert.equal(testServerCalls, 0);
Expand Down
Loading
Loading