0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

chore(@astrojs/vercel): migrate tests to node:test ()

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
Ming-jun Lu 2024-02-13 21:23:07 +08:00 committed by GitHub
parent a326124f5a
commit 37c75108e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 100 additions and 81 deletions

View file

@ -46,8 +46,9 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "mocha --exit --timeout 20000 --file \"./test/setup.js\" test/ --ignore test/hosted --ignore **/*.nodetest.js",
"test:hosted": "mocha --exit --timeout 30000 test/hosted"
"test": "astro-scripts test --timeout 50000 \"test/**/!(hosted|edge-middleware).test.js\" && pnpm run test:edge-middleware",
"test:hosted": "astro-scripts test --timeout 30000 \"test/hosted/*.test.js\"",
"test:edge-middleware": "mocha --exit --timeout 20000 --file \"./test/setup.js\" \"test/edge-middleware.test.js\""
},
"dependencies": {
"@astrojs/internal-helpers": "workspace:*",

View file

@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
const VERCEL_TEST_URL = 'https://astro-vercel-image-test.vercel.app';
@ -8,6 +9,6 @@ describe('Hosted Vercel Tests', () => {
VERCEL_TEST_URL + '/_image?href=%2F_astro%2Fpenguin.e9c64733.png&w=300&f=webp'
);
expect(image.status).to.equal(200);
assert.equal(image.status, 200);
});
});

View file

@ -1,5 +1,6 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Image', () => {
@ -14,7 +15,7 @@ describe('Image', () => {
});
it('build successful', async () => {
expect(await fixture.readFile('../.vercel/output/static/index.html')).to.be.ok;
assert.ok(await fixture.readFile('../.vercel/output/static/index.html'));
});
it('has link to vercel in build with proper attributes', async () => {
@ -22,15 +23,15 @@ describe('Image', () => {
const $ = cheerio.load(html);
const img = $('#basic-image img');
expect(img.attr('src').startsWith('/_vercel/image?url=_astr')).to.be.true;
expect(img.attr('loading')).to.equal('lazy');
expect(img.attr('width')).to.equal('225');
assert.equal(img.attr('src').startsWith('/_vercel/image?url=_astr'), true);
assert.equal(img.attr('loading'), 'lazy');
assert.equal(img.attr('width'), '225');
});
it('has proper vercel config', async () => {
const vercelConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
expect(vercelConfig.images).to.deep.equal({
assert.deepEqual(vercelConfig.images, {
sizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
domains: ['astro.build'],
remotePatterns: [
@ -58,9 +59,9 @@ describe('Image', () => {
const $ = cheerio.load(html);
const img = $('#basic-image img');
expect(img.attr('src').startsWith('/_image?href=')).to.be.true;
expect(img.attr('loading')).to.equal('lazy');
expect(img.attr('width')).to.equal('225');
assert.equal(img.attr('src').startsWith('/_image?href='), true);
assert.equal(img.attr('loading'), 'lazy');
assert.equal(img.attr('width'), '225');
});
it('supports SVGs', async () => {
@ -70,8 +71,8 @@ describe('Image', () => {
const src = img.attr('src');
const res = await fixture.fetch(src);
expect(res.status).to.equal(200);
expect(res.headers.get('content-type')).to.equal('image/svg+xml');
assert.equal(res.status, 200);
assert.equal(res.headers.get('content-type'), 'image/svg+xml');
});
});
});

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('ISR', () => {
/** @type {import('./test-utils.js').Fixture} */
@ -16,7 +17,7 @@ describe('ISR', () => {
const vcConfig = JSON.parse(
await fixture.readFile('../.vercel/output/functions/_isr.prerender-config.json')
);
expect(vcConfig).to.deep.include({
assert.deepEqual(vcConfig, {
expiration: 120,
bypassToken: '1c9e601d-9943-4e7c-9575-005556d774a8',
allowQuery: ['x_astro_path'],
@ -27,7 +28,7 @@ describe('ISR', () => {
it('generates expected routes', async () => {
const deploymentConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
// the first two are /_astro/*, and filesystem routes
expect(deploymentConfig.routes.slice(2)).to.deep.equal([
assert.deepEqual(deploymentConfig.routes.slice(2), [
{
src: '^/two$',
dest: '_render',

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('maxDuration', () => {
/** @type {import('./test-utils.js').Fixture} */
@ -16,6 +17,6 @@ describe('maxDuration', () => {
const vcConfig = JSON.parse(
await fixture.readFile('../.vercel/output/functions/_render.func/.vc-config.json')
);
expect(vcConfig).to.deep.include({ maxDuration: 60 });
assert.equal(vcConfig.maxDuration, 60);
});
});

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('Missing output config', () => {
/** @type {import('./test-utils').Fixture} */
@ -18,7 +19,7 @@ describe('Missing output config', () => {
} catch (err) {
error = err;
}
expect(error).to.not.be.equal(undefined);
expect(error.message).to.include('output: "server"');
assert.notEqual(error, undefined);
assert.match(error.message, /output: "server"/);
});
});

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('prerendered error pages routing', () => {
/** @type {import('./test-utils.js').Fixture} */
@ -14,7 +15,7 @@ describe('prerendered error pages routing', () => {
it('falls back to 404.html', async () => {
const deploymentConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
expect(deploymentConfig.routes.at(-1)).to.deep.include({
assert.deepEqual(deploymentConfig.routes.at(-1), {
src: '/.*',
dest: '/404.html',
status: 404,

View file

@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Redirects Serverless', () => {
@ -23,6 +24,6 @@ describe('Redirects Serverless', () => {
} catch {
hasErrored = true;
}
expect(hasErrored).to.equal(true, 'this file should not exist');
assert.equal(hasErrored, true, 'this file should not exist');
});
});

View file

@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Redirects', () => {
@ -34,46 +35,46 @@ describe('Redirects', () => {
const config = await getConfig();
const oneRoute = config.routes.find((r) => r.src === '/one');
expect(oneRoute.headers.Location).to.equal('/');
expect(oneRoute.status).to.equal(301);
assert.equal(oneRoute.headers.Location, '/');
assert.equal(oneRoute.status, 301);
const twoRoute = config.routes.find((r) => r.src === '/two');
expect(twoRoute.headers.Location).to.equal('/');
expect(twoRoute.status).to.equal(301);
assert.equal(twoRoute.headers.Location, '/');
assert.equal(twoRoute.status, 301);
const threeRoute = config.routes.find((r) => r.src === '/three');
expect(threeRoute.headers.Location).to.equal('/');
expect(threeRoute.status).to.equal(302);
assert.equal(threeRoute.headers.Location, '/');
assert.equal(threeRoute.status, 302);
});
it('define redirects for static files', async () => {
const config = await getConfig();
const staticRoute = config.routes.find((r) => r.src === '/Basic/http-2-0.html');
expect(staticRoute).to.not.be.undefined;
expect(staticRoute.headers.Location).to.equal('/posts/http2');
expect(staticRoute.status).to.equal(301);
assert.notEqual(staticRoute, undefined)
assert.equal(staticRoute.headers.Location, '/posts/http2');
assert.equal(staticRoute.status, 301);
});
it('defines dynamic routes', async () => {
const config = await getConfig();
const blogRoute = config.routes.find((r) => r.src.startsWith('/blog'));
expect(blogRoute).to.not.be.undefined;
expect(blogRoute.headers.Location.startsWith('/team/articles')).to.equal(true);
expect(blogRoute.status).to.equal(301);
assert.notEqual(blogRoute, undefined);
assert.equal(blogRoute.headers.Location.startsWith('/team/articles'), true);
assert.equal(blogRoute.status, 301);
});
it('define trailingSlash redirect for sub pages', async () => {
const config = await getConfig();
const subpathRoute = config.routes.find((r) => r.src === '/subpage');
expect(subpathRoute).to.not.be.undefined;
expect(subpathRoute.headers.Location).to.equal('/subpage/');
assert.notEqual(subpathRoute, undefined);
assert.equal(subpathRoute.headers.Location, '/subpage/');
});
it('does not define trailingSlash redirect for root page', async () => {
const config = await getConfig();
expect(config.routes.find((r) => r.src === '/')).to.be.undefined;
assert.equal(config.routes.find((r) => r.src === '/'), undefined);
});
});

View file

@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Serverless prerender', () => {
@ -14,16 +15,16 @@ describe('Serverless prerender', () => {
});
it('build successful', async () => {
expect(await fixture.readFile('../.vercel/output/static/index.html')).to.be.ok;
assert.ok(await fixture.readFile('../.vercel/output/static/index.html'));
});
// TODO: The path here seems to be inconsistent?
it.skip('includeFiles work', async () => {
expect(
assert.ok(
await fixture.readFile(
'../.vercel/output/functions/render.func/packages/integrations/vercel/test/fixtures/serverless-prerender/dist/middleware.mjs'
)
).to.be.ok;
);
});
});
@ -41,6 +42,6 @@ describe('Serverless hybrid rendering', () => {
});
it('build successful', async () => {
expect(await fixture.readFile('../.vercel/output/static/index.html')).to.be.ok;
assert.ok(await fixture.readFile('../.vercel/output/static/index.html'));
});
});

View file

@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Serverless with dynamic routes', () => {
@ -15,11 +16,12 @@ describe('Serverless with dynamic routes', () => {
});
it('build successful', async () => {
expect(await fixture.readFile('../.vercel/output/static/index.html')).to.be.ok;
expect(
assert.ok(await fixture.readFile('../.vercel/output/static/index.html'));
assert.ok(
await fixture.readFile('../.vercel/output/functions/[id]/index.astro.func/.vc-config.json')
).to.be.ok;
expect(await fixture.readFile('../.vercel/output/functions/api/[id].js.func/.vc-config.json'))
.to.be.ok;
);
assert.ok(
await fixture.readFile('../.vercel/output/functions/api/[id].js.func/.vc-config.json')
);
});
});

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('Vercel Speed Insights', () => {
describe('output: server', () => {
@ -19,7 +20,7 @@ describe('Vercel Speed Insights', () => {
const bundle = await fixture.readFile(`../.vercel/output/static/_astro/${page}`);
expect(bundle).to.contain('https://vitals.vercel-analytics.com/v1/vitals');
assert.match(bundle, /https:\/\/vitals.vercel-analytics.com\/v1\/vitals/)
});
});
@ -40,7 +41,7 @@ describe('Vercel Speed Insights', () => {
const bundle = await fixture.readFile(`../.vercel/output/static/_astro/${page}`);
expect(bundle).to.contain('https://vitals.vercel-analytics.com/v1/vitals');
assert.match(bundle, /https:\/\/vitals.vercel-analytics.com\/v1\/vitals/);
});
});
});

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('build: split', () => {
/** @type {import('./test-utils').Fixture} */
@ -15,12 +16,12 @@ describe('build: split', () => {
it('creates separate functions for each page', async () => {
const files = await fixture.readdir('../.vercel/output/functions/');
expect(files.length).to.equal(3);
assert.equal(files.length, 3);
});
it('creates the route definitions in the config.json', async () => {
const json = await fixture.readFile('../.vercel/output/config.json');
const config = JSON.parse(json);
expect(config.routes).to.have.a.lengthOf(5);
assert.equal(config.routes.length, 5);
});
});

View file

@ -1,4 +1,5 @@
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Static Assets', () => {
@ -32,39 +33,40 @@ describe('Static Assets', () => {
async function checkValidCacheControl(assets) {
const config = await getConfig();
const theAssets = assets ?? (await getAssets());
const route = config.routes.find((r) => r.src === `^/${assets ?? getAssets()}/(.*)$`);
expect(route.headers['cache-control']).to.equal(VALID_CACHE_CONTROL);
expect(route.continue).to.equal(true);
const route = config.routes.find((r) => r.src === `^/${theAssets}/(.*)$`);
assert.equal(route.headers['cache-control'], VALID_CACHE_CONTROL);
assert.equal(route.continue, true);
}
describe('static adapter', async () => {
const { default: vercel } = await import('@astrojs/vercel/static');
describe('static adapter', () => {
it('has cache control', async () => {
const { default: vercel } = await import('@astrojs/vercel/static');
await build({ adapter: vercel() });
checkValidCacheControl();
await checkValidCacheControl();
});
it('has cache control other assets', async () => {
const { default: vercel } = await import('@astrojs/vercel/static');
const assets = '_foo';
await build({ adapter: vercel(), assets });
checkValidCacheControl(assets);
await checkValidCacheControl(assets);
});
});
describe('serverless adapter', async () => {
const { default: vercel } = await import('@astrojs/vercel/serverless');
describe('serverless adapter', () => {
it('has cache control', async () => {
const { default: vercel } = await import('@astrojs/vercel/serverless');
await build({ output: 'server', adapter: vercel() });
checkValidCacheControl();
await checkValidCacheControl();
});
it('has cache control other assets', async () => {
const { default: vercel } = await import('@astrojs/vercel/serverless');
const assets = '_foo';
await build({ output: 'server', adapter: vercel(), assets });
checkValidCacheControl(assets);
await checkValidCacheControl(assets);
});
});
});

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('static routing', () => {
/** @type {import('./test-utils.js').Fixture} */
@ -15,7 +16,7 @@ describe('static routing', () => {
it('falls back to 404.html', async () => {
const deploymentConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
// change the index if necesseary
expect(deploymentConfig.routes[2]).to.deep.include({
assert.deepEqual(deploymentConfig.routes[2], {
src: '/.*',
dest: '/404.html',
status: 404,

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('streaming', () => {
/** @type {import('./test-utils.js').Fixture} */
@ -16,6 +17,6 @@ describe('streaming', () => {
const vcConfig = JSON.parse(
await fixture.readFile('../.vercel/output/functions/_render.func/.vc-config.json')
);
expect(vcConfig).to.deep.include({ supportsResponseStreaming: true });
assert.equal(vcConfig.supportsResponseStreaming, true);
});
});

View file

@ -1,5 +1,6 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
describe('Vercel Web Analytics', () => {
describe('output: static', () => {
@ -18,8 +19,8 @@ describe('Vercel Web Analytics', () => {
const pageOne = await fixture.readFile('../.vercel/output/static/one/index.html');
const pageTwo = await fixture.readFile('../.vercel/output/static/two/index.html');
expect(pageOne).to.contain('/_vercel/insights/script.js');
expect(pageTwo).to.contain('/_vercel/insights/script.js');
assert.match(pageOne, /\/_vercel\/insights\/script.js/);
assert.match(pageTwo, /\/_vercel\/insights\/script.js/);
});
});
});