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
5 changes: 3 additions & 2 deletions packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
"dev": "astro-scripts dev \"src/**/*.ts\"",
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"test": "astro-scripts test --timeout 60000 \"test/**/!(hosted).test.js\"",
"test:hosted": "astro-scripts test --timeout 30000 \"test/hosted/*.test.js\""
"test": "astro-scripts test --timeout 60000 \"test/**/!(hosted).test.ts\"",
"test:hosted": "astro-scripts test --timeout 30000 \"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
@@ -1,15 +1,14 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import { type Fixture, loadFixture, getVercelConfig } from './test-utils.ts';

describe('Vercel edge middleware', () => {
/** @type {import('./test-utils.js').Fixture} */
let build;
let build: Fixture;
before(async () => {
build = await loadFixture({
root: './fixtures/middleware-with-edge-file/',
});
await build.build();
await build.build({});
});

it('an edge function is created', async () => {
Expand All @@ -22,8 +21,7 @@ describe('Vercel edge middleware', () => {
});

it('deployment config points to the middleware edge function', async () => {
const contents = await build.readFile('../.vercel/output/config.json');
const { routes } = JSON.parse(contents);
const { routes } = await getVercelConfig(build);
assert.equal(
routes.some((route) => route.dest === '_middleware'),
true,
Expand All @@ -35,7 +33,7 @@ describe('Vercel edge middleware', () => {
'../.vercel/output/functions/_middleware.func/middleware.mjs',
build.config.outDir,
);
const module = await import(entry);
const module = await import(entry.href);
const request = new Request('http://example.com/foo');
const response = await module.default(request, {});
assert.equal(response.headers.get('set-cookie'), 'foo=bar');
Expand All @@ -47,10 +45,10 @@ describe('Vercel edge middleware', () => {
'../.vercel/output/functions/_middleware.func/middleware.mjs',
build.config.outDir,
);
const module = await import(entry);
const module = await import(entry.href);

const originalFetch = globalThis.fetch;
let captured;
let captured: RequestInit | undefined;
globalThis.fetch = async (_url, opts) => {
captured = opts;
return new Response('ok', { status: 200 });
Expand All @@ -62,6 +60,7 @@ describe('Vercel edge middleware', () => {
headers: { 'Content-Type': 'application/json' },
});
await module.default(request, {});
assert.ok(captured, 'fetch was called');
assert.equal(captured.method, 'POST', 'forwards the HTTP method');
assert.ok(captured.body, 'forwards the request body');
} finally {
Expand All @@ -74,7 +73,7 @@ describe('Vercel edge middleware', () => {
const fixture = await loadFixture({
root: './fixtures/middleware-with-edge-file/',
});
await fixture.build();
await fixture.build({});
const _contents = await fixture.readFile(
// this is abysmal...
'../.vercel/output/functions/render.func/www/withastro/astro/packages/vercel/test/fixtures/middleware-with-edge-file/dist/middleware.mjs',
Expand All @@ -89,7 +88,7 @@ describe('Vercel edge middleware', () => {
const fixture = await loadFixture({
root: './fixtures/middleware-without-edge-file/',
});
await fixture.build();
await fixture.build({});
const _contents = await fixture.readFile(
// this is abysmal...
'../.vercel/output/functions/render.func/www/withastro/astro/packages/vercel/test/fixtures/middleware-without-edge-file/dist/middleware.mjs',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import { type Fixture, type DevServer, loadFixture } from './test-utils.ts';

describe('Image', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let fixture: Fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/image/',
});
await fixture.build();
await fixture.build({});
});

it('build successful', async () => {
Expand All @@ -23,7 +22,7 @@ describe('Image', () => {
const $ = cheerio.load(html);
const img = $('#basic-image img');

assert.equal(img.attr('src').startsWith('/_vercel/image?url=_astr'), true);
assert.equal(img.attr('src')!.startsWith('/_vercel/image?url=_astr'), true);
assert.equal(img.attr('loading'), 'lazy');
assert.equal(img.attr('width'), '225');
});
Expand All @@ -33,12 +32,12 @@ describe('Image', () => {
const $ = cheerio.load(html);
const img = $('#small-source img');
const widths = img
.attr('srcset')
.attr('srcset')!
.split(', ')
.map((entry) => entry.split(' ')[1]);
assert.deepEqual(widths, ['640w'], 'uses valid widths in srcset');

const url = new URL(img.attr('src'), 'http://localhost');
const url = new URL(img.attr('src')!, 'http://localhost');
assert.equal(url.searchParams.get('w'), '640', 'uses valid width in src');

assert.equal(img.attr('width'), '225', 'uses requested width in img attribute');
Expand All @@ -48,7 +47,7 @@ describe('Image', () => {
const html = await fixture.readFile('../.vercel/output/static/index.html');
const $ = cheerio.load(html);
const img = $('#densities-test img');
const srcset = img.attr('srcset');
const srcset = img.attr('srcset')!;

// Extract widths from srcset (format: "url 1x", "url 1.5x", etc)
const descriptors = srcset.split(', ').map((entry) => entry.split(' ')[1]);
Expand All @@ -57,7 +56,7 @@ describe('Image', () => {
const urls = srcset.split(', ').map((entry) => entry.split(' ')[0]);
const widthsFromUrls = urls.map((url) => {
const urlObj = new URL(url, 'http://localhost');
return Number.parseInt(urlObj.searchParams.get('w'), 10);
return Number.parseInt(urlObj.searchParams.get('w')!, 10);
});

// The configured sizes are [640, 750, 828, 1080, 1200, 1920, 2048, 3840]
Expand Down Expand Up @@ -101,7 +100,7 @@ describe('Image', () => {
});

describe('dev', () => {
let devServer;
let devServer: DevServer;

before(async () => {
devServer = await fixture.startDevServer();
Expand All @@ -116,7 +115,7 @@ describe('Image', () => {
const $ = cheerio.load(html);
const img = $('#basic-image img');

assert.equal(img.attr('src').startsWith('/_image?href='), true);
assert.equal(img.attr('src')!.startsWith('/_image?href='), true);
assert.equal(img.attr('loading'), 'lazy');
assert.equal(img.attr('width'), '225');
});
Expand All @@ -125,7 +124,7 @@ describe('Image', () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);
const img = $('#svg img');
const src = img.attr('src');
const src = img.attr('src')!;

const res = await fixture.fetch(src);
assert.equal(res.status, 200);
Expand All @@ -137,7 +136,7 @@ describe('Image', () => {
const $ = cheerio.load(html);
const img = $('#responsive img');
const widths = img
.attr('srcset')
.attr('srcset')!
.split(', ')
.map((entry) => entry.split(' ')[1]);
assert.deepEqual(widths, ['640w', '750w', '828w', '1080w', '1200w', '1920w']);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import { loadFixture } from './test-utils.ts';

describe('Assets generated by integrations', () => {
it('moves static assets generated by integrations to the correct location: static output', async () => {
const fixture = await loadFixture({
root: './fixtures/integration-assets/',
});
await fixture.build();
await fixture.build({});
const sitemap = await fixture.readFile('../.vercel/output/static/sitemap-index.xml');
assert(sitemap.includes('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'));
});
Expand All @@ -17,7 +17,7 @@ describe('Assets generated by integrations', () => {
root: './fixtures/integration-assets/',
output: 'server',
});
await fixture.build();
await fixture.build({});
const sitemap = await fixture.readFile('../.vercel/output/static/sitemap-index.xml');
assert(sitemap.includes('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import { type Fixture, loadFixture } from './test-utils.ts';

describe('ISR', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let fixture: Fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/isr/',
});
await fixture.build();
await fixture.build({});
});

it('generates expected prerender config', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import { type Fixture, loadFixture } from './test-utils.ts';

describe('maxDuration', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let fixture: Fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/max-duration/',
});
await fixture.build();
await fixture.build({});
});

it('makes it to vercel function configuration', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import { type Fixture, loadFixture } from './test-utils.ts';

async function loadFunctionModule(fixture, functionName) {
async function loadFunctionModule(fixture: Fixture, functionName: string) {
const functionConfig = JSON.parse(
await fixture.readFile(`../.vercel/output/functions/${functionName}.func/.vc-config.json`),
);
Expand All @@ -11,20 +11,19 @@ async function loadFunctionModule(fixture, functionName) {
fixture.config.outDir,
);

return import(functionEntry);
return import(functionEntry.href);
}

describe('Vercel serverless path override security', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let fixture: Fixture;

before(async () => {
process.env.PRERENDER = true;
process.env.PRERENDER = 'true';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we should change this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. process.env's type is Record<string, string | undefined>, so process.env.PRERENDER = true cannot be a valid way.
  2. I dug into process.env.PRERENDER after seeing your comment. It seems that process.env.PRERENDER is not used anymore. It was used here, but this usage has been removed since Astro 4.14.0 (see changeset file). Thus, I guess removing all process.env.PRERENDER = ... from the testing code should not break the test.

fixture = await loadFixture({
root: './fixtures/serverless-with-dynamic-routes/',
output: 'server',
});
await fixture.build();
await fixture.build({});
});

it('ignores untrusted x_astro_path query param on _render', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import { type Fixture, loadFixture, getVercelConfig } from './test-utils.ts';

describe('prerendered error pages routing', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let fixture: Fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/prerendered-error-pages/',
});
await fixture.build();
await fixture.build({});
});

it('falls back to 404.html', async () => {
const deploymentConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
const deploymentConfig = await getVercelConfig(fixture);
assert.deepEqual(
deploymentConfig.routes.find((r) => r.status === 404),
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
import { type Fixture, loadFixture } from './test-utils.ts';

describe('Redirects Serverless', () => {
/** @type {import('astro/test/test-utils.js').Fixture} */
let fixture;
let fixture: Fixture;

before(async () => {
fixture = await loadFixture({
Expand All @@ -14,7 +13,7 @@ describe('Redirects Serverless', () => {
'/other': '/subpage',
},
});
await fixture.build();
await fixture.build({});
});

it('does not create .html files', async () => {
Expand Down
Loading
Loading