0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

Improve undici fetch failed errors in tests (#6960)

This commit is contained in:
Bjorn Lu 2023-05-02 22:10:33 +08:00 committed by GitHub
parent a695e44aed
commit 5371efe429
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 139 additions and 255 deletions

View file

@ -3,11 +3,11 @@ import { loadFixture } from './test-utils.js';
import * as cheerio from 'cheerio';
describe('getStaticPaths - build calls', () => {
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
/** @type {import('./test-utils').Fixture} */
let fixture;
const fixture = await loadFixture({
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-get-static-paths/',
site: 'https://mysite.dev/',
base: '/blog',
@ -15,10 +15,22 @@ describe('getStaticPaths - build calls', () => {
await fixture.build();
});
afterEach(() => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
});
it('is only called once during build', () => {
// useless expect; if build() throws in setup then this test fails
expect(true).to.equal(true);
});
it('Astro.url sets the current pathname', async () => {
const html = await fixture.readFile('/food/tacos/index.html');
const $ = cheerio.load(html);
expect($('#url').text()).to.equal('/blog/food/tacos/');
});
});
describe('getStaticPaths - dev calls', () => {
@ -26,11 +38,16 @@ describe('getStaticPaths - dev calls', () => {
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-get-static-paths/',
site: 'https://mysite.dev/',
});
devServer = await fixture.startDevServer();
});
afterEach(() => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({ root: './fixtures/astro-get-static-paths/' });
devServer = await fixture.startDevServer();
});
after(async () => {
@ -47,24 +64,8 @@ describe('getStaticPaths - dev calls', () => {
res = await fixture.fetch('/c');
expect(res.status).to.equal(200);
});
});
describe('getStaticPaths - 404 behavior', () => {
let fixture;
let devServer;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({ root: './fixtures/astro-get-static-paths/' });
devServer = await fixture.startDevServer();
});
after(async () => {
devServer.stop();
});
describe('404 behavior', () => {
it('resolves 200 on matching static path - named params', async () => {
const res = await fixture.fetch('/pizza/provolone-sausage');
expect(res.status).to.equal(200);
@ -86,21 +87,7 @@ describe('getStaticPaths - 404 behavior', () => {
});
});
describe('getStaticPaths - route params type validation', () => {
let fixture, devServer;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({ root: './fixtures/astro-get-static-paths/' });
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
describe('route params type validation', () => {
it('resolves 200 on nested array parameters', async () => {
const res = await fixture.fetch('/nested-arrays/slug1');
expect(res.status).to.equal(200);
@ -119,25 +106,6 @@ describe('getStaticPaths - route params type validation', () => {
});
});
describe('getStaticPaths - numeric route params', () => {
let fixture;
let devServer;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({
root: './fixtures/astro-get-static-paths/',
site: 'https://mysite.dev/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('resolves 200 on matching static paths', async () => {
// routes params provided for pages /posts/1, /posts/2, and /posts/3
for (const page of [1, 2, 3]) {
@ -155,25 +123,3 @@ describe('getStaticPaths - numeric route params', () => {
}
});
});
describe('getStaticPaths - Astro.url', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({
root: './fixtures/astro-get-static-paths/',
site: 'https://mysite.dev/',
});
await fixture.build();
});
it('Sets the current pathname', async () => {
const html = await fixture.readFile('/food/tacos/index.html');
const $ = cheerio.load(html);
expect($('#url').text()).to.equal('/food/tacos/');
});
});

View file

@ -3,11 +3,11 @@ import { loadFixture } from './test-utils.js';
import * as cheerio from 'cheerio';
describe('prerender getStaticPaths - build calls', () => {
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
/** @type {import('./test-utils').Fixture} */
let fixture;
const fixture = await loadFixture({
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-prerender-get-static-paths/',
site: 'https://mysite.dev/',
base: '/blog',
@ -15,10 +15,23 @@ describe('prerender getStaticPaths - build calls', () => {
await fixture.build();
});
afterEach(() => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
});
it('is only called once during build', () => {
// useless expect; if build() throws in setup then this test fails
expect(true).to.equal(true);
});
it('Astro.url sets the current pathname', async () => {
const html = await fixture.readFile('/food/tacos/index.html');
const $ = cheerio.load(html);
expect($('#props').text()).to.equal('10');
expect($('#url').text()).to.equal('/blog/food/tacos/');
});
});
describe('prerender getStaticPaths - dev calls', () => {
@ -26,11 +39,17 @@ describe('prerender getStaticPaths - dev calls', () => {
let devServer;
before(async () => {
globalThis.isCalledOnce = false;
fixture = await loadFixture({
root: './fixtures/ssr-prerender-get-static-paths/',
site: 'https://mysite.dev/',
});
devServer = await fixture.startDevServer();
});
afterEach(() => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({ root: './fixtures/ssr-prerender-get-static-paths/' });
devServer = await fixture.startDevServer();
});
after(async () => {
@ -47,24 +66,8 @@ describe('prerender getStaticPaths - dev calls', () => {
res = await fixture.fetch('/c');
expect(res.status).to.equal(200);
});
});
describe('prerender getStaticPaths - 404 behavior', () => {
let fixture;
let devServer;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({ root: './fixtures/ssr-prerender-get-static-paths/' });
devServer = await fixture.startDevServer();
});
after(async () => {
devServer.stop();
});
describe('404 behavior', () => {
it('resolves 200 on matching static path - named params', async () => {
const res = await fixture.fetch('/pizza/provolone-sausage');
expect(res.status).to.equal(200);
@ -90,21 +93,7 @@ describe('prerender getStaticPaths - 404 behavior', () => {
});
});
describe('prerender getStaticPaths - route params type validation', () => {
let fixture, devServer;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({ root: './fixtures/ssr-prerender-get-static-paths/' });
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
describe('route params type validation', () => {
it('resolves 200 on nested array parameters', async () => {
const res = await fixture.fetch('/nested-arrays/slug1');
expect(res.status).to.equal(200);
@ -123,25 +112,6 @@ describe('prerender getStaticPaths - route params type validation', () => {
});
});
describe('prerender getStaticPaths - numeric route params', () => {
let fixture;
let devServer;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({
root: './fixtures/ssr-prerender-get-static-paths/',
site: 'https://mysite.dev/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('resolves 200 on matching static paths', async () => {
// routes params provided for pages /posts/1, /posts/2, and /posts/3
for (const page of [1, 2, 3]) {
@ -159,47 +129,3 @@ describe('prerender getStaticPaths - numeric route params', () => {
}
});
});
describe('prerender getStaticPaths - Astro.url', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({
root: './fixtures/ssr-prerender-get-static-paths/',
site: 'https://mysite.dev/',
});
await fixture.build();
});
it('Sets the current pathname', async () => {
const html = await fixture.readFile('/food/tacos/index.html');
const $ = cheerio.load(html);
expect($('#url').text()).to.equal('/food/tacos/');
});
});
describe('prerender getStaticPaths - props', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;
fixture = await loadFixture({
root: './fixtures/ssr-prerender-get-static-paths/',
site: 'https://mysite.dev/',
});
await fixture.build();
});
it('Sets the current pathname', async () => {
const html = await fixture.readFile('/food/tacos/index.html');
const $ = cheerio.load(html);
expect($('#props').text()).to.equal('10');
});
});

View file

@ -187,7 +187,19 @@ export async function loadFixture(inlineConfig) {
},
config,
resolveUrl,
fetch: (url, init) => fetch(resolveUrl(url), init),
fetch: async (url, init) => {
const resolvedUrl = resolveUrl(url);
try {
return await fetch(resolvedUrl, init);
} catch (err) {
// undici throws a vague error when it fails, so we log the url here to easily debug it
if (err.message?.includes('fetch failed')) {
console.error(`[astro test] failed to fetch ${resolvedUrl}`);
console.error(err);
}
throw err;
}
},
preview: async (opts = {}) => {
process.env.NODE_ENV = 'production';
const previewServer = await preview(settings, {