Skip to content

Commit

Permalink
Add error message if Astro.glob is called outside (#7204)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored May 26, 2023
1 parent 184870e commit 52af9ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-coats-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add error message if `Astro.glob` is called outside of an Astro file
5 changes: 5 additions & 0 deletions packages/astro/src/runtime/server/astro-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { ASTRO_VERSION } from '../../core/constants.js';
/** Create the Astro.glob() runtime function. */
function createAstroGlobFn() {
const globHandler = (importMetaGlobResult: Record<string, any>, globValue: () => any) => {
if (typeof importMetaGlobResult === 'string') {
throw new Error(
'Astro.glob() does not work outside of an Astro file. Use `import.meta.glob()` instead.'
);
}
let allEntries = [...Object.values(importMetaGlobResult)];
if (allEntries.length === 0) {
throw new Error(`Astro.glob(${JSON.stringify(globValue())}) - no matches found.`);
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/test/units/runtime/astro-global.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from 'chai';
import { createAstro } from '../../../dist/runtime/server/index.js';

describe('astro global', () => {
it('Glob should error if passed incorrect value', async () => {
const Astro = createAstro(undefined);
expect(() => {
Astro.glob('./**/*.md');
}).to.throw(
'Astro.glob() does not work outside of an Astro file. Use `import.meta.glob()` instead.'
);
});
});

0 comments on commit 52af9ad

Please sign in to comment.