Skip to content

Commit

Permalink
chore(@astrojs/vercel): migrate tests to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
mingjunlu committed Feb 12, 2024
1 parent 09a6273 commit d4aa9d1
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 81 deletions.
5 changes: 3 additions & 2 deletions packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
5 changes: 3 additions & 2 deletions packages/integrations/vercel/test/hosted/hosted.test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
});
});
23 changes: 12 additions & 11 deletions packages/integrations/vercel/test/image.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -14,23 +15,23 @@ 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 () => {
const html = await fixture.readFile('../.vercel/output/static/index.html');
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: [
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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');
});
});
});
7 changes: 4 additions & 3 deletions packages/integrations/vercel/test/isr.test.js
Original file line number Diff line number Diff line change
@@ -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} */
Expand All @@ -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'],
Expand All @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions packages/integrations/vercel/test/max-duration.test.js
Original file line number Diff line number Diff line change
@@ -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} */
Expand All @@ -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);
});
});
7 changes: 4 additions & 3 deletions packages/integrations/vercel/test/no-output.test.js
Original file line number Diff line number Diff line change
@@ -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} */
Expand All @@ -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"/);
});
});
Original file line number Diff line number Diff line change
@@ -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} */
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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');
});
});
33 changes: 17 additions & 16 deletions packages/integrations/vercel/test/redirects.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
11 changes: 6 additions & 5 deletions packages/integrations/vercel/test/serverless-prerender.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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;
);
});
});

Expand All @@ -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'));
});
});
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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')
);
});
});
7 changes: 4 additions & 3 deletions packages/integrations/vercel/test/speed-insights.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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/)
});
});

Expand All @@ -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/);
});
});
});
7 changes: 4 additions & 3 deletions packages/integrations/vercel/test/split.test.js
Original file line number Diff line number Diff line change
@@ -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} */
Expand All @@ -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);
});
});

0 comments on commit d4aa9d1

Please sign in to comment.