Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(netlify): use Node.js for testing #158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
14 changes: 6 additions & 8 deletions packages/netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
],
"scripts": {
"build": "tsc",
"test-fn": "mocha --exit --timeout 20000 --file \"./test/setup.js\" test/functions/",
"test-static": "mocha --exit --timeout 20000 --file \"./test/setup.js\" test/static/",
"test": "pnpm test-fn && pnpm test-static",
"test:hosted": "mocha --exit --timeout 30000 test/hosted"
"test-fn": "astro-scripts test \"test/functions/*.test.js\"",
"test-static": "astro-scripts test \"test/static/*.test.js\"",
"test": "pnpm run test-fn && pnpm run test-static",
"test:hosted": "astro-scripts test \"test/hosted/*.test.js\""
},
"dependencies": {
"@astrojs/underscore-redirects": "^0.3.3",
Expand All @@ -48,16 +48,14 @@
"@netlify/edge-handler-types": "^0.34.1",
"@types/node": "^18.17.8",
"astro": "^4.3.5",
"chai": "^4.3.10",
"chai-jest-snapshot": "^2.0.0",
"cheerio": "1.0.0-rc.12",
"execa": "^8.0.1",
"fast-glob": "^3.3.1",
"mocha": "^10.2.0",
"strip-ansi": "^7.1.0",
"typescript": "^5.2.2",
"vite": "^4.5.0",
"@astrojs/test-utils": "workspace:*"
"@astrojs/test-utils": "workspace:*",
"astro-scripts": "workspace:*"
},
"astro": {
"external": true
Expand Down
15 changes: 8 additions & 7 deletions packages/netlify/test/functions/cookies.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadFixture } from '@astrojs/test-utils';
import { expect } from 'chai';
import { describe, it, before } from 'node:test';
import * as assert from 'node:assert/strict';

describe('Cookies', () => {
let fixture;
Expand All @@ -19,9 +20,9 @@ describe('Cookies', () => {
new Request('http://example.com/login', { method: 'POST', body: '{}' }),
{}
);
expect(resp.status).to.equal(301);
expect(resp.headers.get('location')).to.equal('/');
expect(resp.headers.getSetCookie()).to.eql(['foo=foo; HttpOnly', 'bar=bar; HttpOnly']);
assert.equal(resp.status,301);
assert.equal(resp.headers.get('location'),'/');
assert.deepEqual(resp.headers.getSetCookie(),['foo=foo; HttpOnly', 'bar=bar; HttpOnly']);
});

it('renders dynamic 404 page', async () => {
Expand All @@ -38,9 +39,9 @@ describe('Cookies', () => {
}),
{}
);
expect(resp.status).to.equal(404);
assert.equal(resp.status,404);
const text = await resp.text();
expect(text).to.contain('This is my custom 404 page');
expect(text).to.contain('x-test: bar');
assert.equal(text.includes('This is my custom 404 page'),true);
assert.equal(text.includes('x-test: bar'),true);
});
});
22 changes: 16 additions & 6 deletions packages/netlify/test/functions/edge-middleware.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadFixture } from '@astrojs/test-utils';
import { expect } from 'chai';
import { describe, it, before, after } from 'node:test';
import * as assert from 'node:assert/strict';

describe('Middleware', () => {
const root = new URL('./fixtures/middleware/', import.meta.url);
Expand All @@ -13,15 +14,19 @@ describe('Middleware', () => {
});

it('emits no edge function', async () => {
expect(fixture.pathExists('../.netlify/edge-functions/middleware/middleware.mjs')).to.be
.false;
assert.equal(fixture.pathExists('../.netlify/edge-functions/middleware/middleware.mjs'), false)
});

it('applies middleware to static files at build-time', async () => {
// prerendered page has middleware applied at build time
const prerenderedPage = await fixture.readFile('prerender/index.html');
expect(prerenderedPage).to.contain('<title>Middleware</title>');
assert.equal(prerenderedPage.includes('<title>Middleware</title>'),true);
});

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

describe('edgeMiddleware: true', () => {
Expand All @@ -36,12 +41,17 @@ describe('Middleware', () => {
const contents = await fixture.readFile(
'../.netlify/edge-functions/middleware/middleware.mjs'
);
expect(contents.includes('"Hello world"')).to.be.false;
assert.equal(contents.includes('"Hello world"'), false);
});

it.skip('does not apply middleware during prerendering', async () => {
const prerenderedPage = await fixture.readFile('prerender/index.html');
expect(prerenderedPage).to.contain('<title></title>');
assert.equal(prerenderedPage.includes('<title></title>'),true);
});

after(async () => {
process.env.EDGE_MIDDLEWARE = undefined;
await fixture.clean();
})
});
});
23 changes: 12 additions & 11 deletions packages/netlify/test/functions/image-cdn.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe } from 'node:test';
import { loadFixture } from '@astrojs/test-utils';
import { expect } from 'chai';
import { describe, it, after } from 'node:test';
import * as assert from 'node:assert/strict';

describe('Image CDN', () => {
const root = new URL('./fixtures/middleware/', import.meta.url);
Expand All @@ -11,20 +11,24 @@ describe('Image CDN', () => {
await fixture.build();

const astronautPage = await fixture.readFile('astronaut/index.html');
expect(astronautPage).contains(`src="/_astro/astronaut.`);
assert.equal(astronautPage.includes(`src="/_astro/astronaut.`),true);
});

});

describe('when running inside of netlify', () => {
it('enables Netlify Image CDN', async () => {
after(() => {
process.env.NETLIFY = undefined;
process.env.DISABLE_IMAGE_CDN = undefined;
});

it('enables Netlify Image CDN',async () => {
process.env.NETLIFY = 'true';
const fixture = await loadFixture({ root });
await fixture.build();

const astronautPage = await fixture.readFile('astronaut/index.html');
expect(astronautPage).contains(`src="/.netlify/image`);

process.env.NETLIFY = undefined;
assert.equal(astronautPage.includes(`src="/.netlify/image`),true);
});

it('respects image CDN opt-out', async () => {
Expand All @@ -34,10 +38,7 @@ describe('Image CDN', () => {
await fixture.build();

const astronautPage = await fixture.readFile('astronaut/index.html');
expect(astronautPage).contains(`src="/_astro/astronaut.`);

process.env.NETLIFY = undefined;
process.env.DISABLE_IMAGE_CDN = undefined;
assert.equal(astronautPage.includes(`src="/_astro/astronaut.`),true);
});
});
});
20 changes: 11 additions & 9 deletions packages/netlify/test/functions/redirects.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createServer } from 'http';
import { loadFixture } from '@astrojs/test-utils';
import { expect } from 'chai';
import { describe, it, before } from 'node:test';
import * as assert from 'node:assert/strict';

describe('SSR - Redirects', () => {
let fixture;
Expand All @@ -13,8 +14,9 @@ describe('SSR - Redirects', () => {
it('Creates a redirects file', async () => {
const redirects = await fixture.readFile('./_redirects');
const parts = redirects.split(/\s+/);
expect(parts).to.deep.equal(['', '/other', '/', '301', '']);
expect(redirects).to.matchSnapshot();
assert.deepEqual(parts,['', '/other', '/', '301', '']);
// Snapshots are not supported in Node.js test yet (https://github.com/nodejs/node/issues/48260)
assert.equal(redirects,'\n/other / 301\n');
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
});

it('Does not create .html files', async () => {
Expand All @@ -24,7 +26,7 @@ describe('SSR - Redirects', () => {
} catch {
hasErrored = true;
}
expect(hasErrored).to.equal(true, 'this file should not exist');
assert.equal(hasErrored,true, 'this file should not exist');
});

it('renders static 404 page', async () => {
Expand All @@ -34,10 +36,10 @@ describe('SSR - Redirects', () => {
);
const { default: handler } = await import(entryURL);
const resp = await handler(new Request('http://example.com/nonexistant-page'), {});
expect(resp.status).to.equal(404);
expect(resp.headers.get('content-type')).to.equal('text/html; charset=utf-8');
assert.equal(resp.status,404);
assert.equal(resp.headers.get('content-type'),'text/html; charset=utf-8');
const text = await resp.text();
expect(text).to.contain('This is my static 404 page');
assert.equal(text.includes('This is my static 404 page'),true);
});

it('does not pass through 404 request', async () => {
Expand All @@ -54,8 +56,8 @@ describe('SSR - Redirects', () => {
);
const { default: handler } = await import(entryURL);
const resp = await handler(new Request('http://localhost:5678/nonexistant-page'), {});
expect(resp.status).to.equal(404);
expect(testServerCalls).to.equal(0);
assert.equal(resp.status,404);
assert.equal(testServerCalls,0);
testServer.close();
});
});
13 changes: 0 additions & 13 deletions packages/netlify/test/functions/redirects.test.js.snap

This file was deleted.

7 changes: 4 additions & 3 deletions packages/netlify/test/hosted/hosted.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { describe, it, before } from 'node:test';
import * as assert from 'node:assert/strict';

const NETLIFY_TEST_URL = 'https://curious-boba-495d6d.netlify.app';

Expand All @@ -8,14 +9,14 @@ describe('Hosted Netlify Tests', () => {
`${NETLIFY_TEST_URL}/_image?href=%2F_astro%2Fpenguin.e9c64733.png&w=300&f=webp`
);

expect(image.status).to.equal(200);
assert.equal(image.status,200);
});

it('Server returns fresh content', async () => {
const responseOne = await fetch(`${NETLIFY_TEST_URL}/time`);

const responseTwo = await fetch(`${NETLIFY_TEST_URL}/time`);

expect(responseOne.body).to.not.equal(responseTwo.body);
assert.notEqual(responseOne.body,responseTwo.body);
});
});
12 changes: 0 additions & 12 deletions packages/netlify/test/setup.js

This file was deleted.

7 changes: 4 additions & 3 deletions packages/netlify/test/static/redirects.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadFixture } from '@astrojs/test-utils';
import { expect } from 'chai';
import { describe, it, before } from 'node:test';
import * as assert from 'node:assert/strict';

describe('SSG - Redirects', () => {
let fixture;
Expand All @@ -12,7 +13,7 @@ describe('SSG - Redirects', () => {
it('Creates a redirects file', async () => {
const redirects = await fixture.readFile('./_redirects');
const parts = redirects.split(/\s+/);
expect(parts).to.deep.equal([
assert.deepEqual(parts, [
'',

'/two',
Expand All @@ -28,6 +29,6 @@ describe('SSG - Redirects', () => {
'301',

'',
]);
])
});
});
Loading
Loading