Skip to content

Commit

Permalink
fix(core): fail fast if plugin has no name (#10239)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored Jun 20, 2024
1 parent 2eecd70 commit 97630b4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

57 changes: 47 additions & 10 deletions packages/docusaurus/src/server/plugins/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import path from 'path';
import {loadContext, type LoadContextParams} from '../../site';
import {initPlugins} from '../init';

describe('initPlugins', () => {
async function loadSite(options: Omit<LoadContextParams, 'siteDir'> = {}) {
const siteDir = path.join(__dirname, '__fixtures__', 'site-with-plugin');
const context = await loadContext({...options, siteDir});
const plugins = await initPlugins(context);
async function loadSite(
fixtureName: string,
options: Omit<LoadContextParams, 'siteDir'> = {},
) {
const siteDir = path.join(__dirname, '__fixtures__', fixtureName);
const context = await loadContext({...options, siteDir});
const plugins = await initPlugins(context);

return {siteDir, context, plugins};
}
return {siteDir, context, plugins};
}

describe('initPlugins', () => {
it('parses plugins correctly and loads them in correct order', async () => {
const {context, plugins} = await loadSite();
const {context, plugins} = await loadSite('site-with-plugin');
expect(context.siteConfig.plugins).toHaveLength(6);
expect(plugins).toHaveLength(10);

Expand All @@ -45,7 +48,41 @@ describe('initPlugins', () => {

it('throws user-friendly error message for plugins with bad values', async () => {
await expect(() =>
loadSite({config: 'badPlugins.docusaurus.config.js'}),
).rejects.toThrowErrorMatchingSnapshot();
loadSite('site-with-plugin', {config: 'badPlugins.docusaurus.config.js'}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
" => Bad Docusaurus plugin value plugins[0].
Example valid plugin config:
{
plugins: [
["@docusaurus/plugin-content-docs",options],
"./myPlugin",
["./myPlugin",{someOption: 42}],
function myPlugin() { },
[function myPlugin() { },options]
],
};
=> Bad Docusaurus plugin value plugins[1].
Example valid plugin config:
{
plugins: [
["@docusaurus/plugin-content-docs",options],
"./myPlugin",
["./myPlugin",{someOption: 42}],
function myPlugin() { },
[function myPlugin() { },options]
],
};
"
`);
});

it('throws user-friendly error message for plugins with no name', async () => {
await expect(() => loadSite('site-with-unnamed-plugin')).rejects
.toThrowErrorMatchingInlineSnapshot(`
"A Docusaurus plugin is missing a 'name' property.
Note that even inline/anonymous plugin functions require a 'name' property."
`);
});
});
7 changes: 7 additions & 0 deletions packages/docusaurus/src/server/plugins/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ export async function initPlugins(
pluginOptions,
);

if (!pluginInstance.name) {
throw new Error(
`A Docusaurus plugin is missing a 'name' property.
Note that even inline/anonymous plugin functions require a 'name' property.`,
);
}

return {
...pluginInstance,
options: pluginOptions,
Expand Down

0 comments on commit 97630b4

Please sign in to comment.