Skip to content

Commit

Permalink
fix type for PluginModule
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 19, 2024
1 parent c4ec67f commit 690b38e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 45 deletions.
3 changes: 1 addition & 2 deletions packages/docusaurus-types/src/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ export type LoadedPlugin = InitializedPlugin & {
export type PluginModule<Content = unknown> = {
(context: LoadContext, options: unknown):
| Plugin<Content>
| Promise<Plugin<Content>>
| null
| Promise<null>;
| Promise<Plugin<Content> | null>;

validateOptions?: <T, U>(data: OptionValidationContext<T, U>) => U;
validateThemeConfig?: <T>(data: ThemeConfigValidationContext<T>) => T;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ exports[`plugins should throw error if plugins is not array for the input of: {}
"
`;

exports[`plugins should throw error if themes is not a string and it's not an array #1 for the input of: [123] 1`] = `
exports[`themes should throw error if themes is not a string and it's not an array #1 for the input of: [123] 1`] = `
" => Bad Docusaurus theme value themes[0].
Example valid theme config:
{
Expand All @@ -109,7 +109,7 @@ Example valid theme config:
"
`;

exports[`plugins should throw error if themes is not an array of [string, object][] #1 for the input of: [[Array]] 1`] = `
exports[`themes should throw error if themes is not an array of [string, object][] #1 for the input of: [[Array]] 1`] = `
" => Bad Docusaurus theme value themes[0].
Example valid theme config:
{
Expand All @@ -125,7 +125,7 @@ Example valid theme config:
"
`;

exports[`plugins should throw error if themes is not an array of [string, object][] #2 for the input of: [[Array]] 1`] = `
exports[`themes should throw error if themes is not an array of [string, object][] #2 for the input of: [[Array]] 1`] = `
" => Bad Docusaurus theme value themes[0].
Example valid theme config:
{
Expand All @@ -141,7 +141,7 @@ Example valid theme config:
"
`;

exports[`plugins should throw error if themes is not an array of [string, object][] #3 for the input of: [[Array]] 1`] = `
exports[`themes should throw error if themes is not an array of [string, object][] #3 for the input of: [[Array]] 1`] = `
" => Bad Docusaurus theme value themes[0].
Example valid theme config:
{
Expand All @@ -157,7 +157,7 @@ Example valid theme config:
"
`;

exports[`plugins should throw error if themes is not array for the input of: {} 1`] = `
exports[`themes should throw error if themes is not array for the input of: {} 1`] = `
""themes" must be an array
"
`;
130 changes: 92 additions & 38 deletions packages/docusaurus/src/server/__tests__/configValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
validateConfig,
} from '../configValidation';
import type {StorageConfig} from '@docusaurus/types/src/config';
import type {Config, DocusaurusConfig} from '@docusaurus/types';
import type {Config, DocusaurusConfig, PluginConfig} from '@docusaurus/types';
import type {DeepPartial} from 'utility-types';

const baseConfig = {
Expand Down Expand Up @@ -466,6 +466,11 @@ describe('markdown', () => {
});

describe('plugins', () => {
// Only here to verify typing
function ensurePlugins(plugins: PluginConfig[]): PluginConfig[] {
return plugins;
}

it.each([
['should throw error if plugins is not array', {}],
[
Expand Down Expand Up @@ -494,64 +499,86 @@ describe('plugins', () => {
});

it.each([
['should throw error if themes is not array', {}],
[
"should throw error if themes is not a string and it's not an array #1",
[123],
],
[
'should throw error if themes is not an array of [string, object][] #1',
[['example/path', 'wrong parameter here']],
],
[
'should throw error if themes is not an array of [string, object][] #2',
[[{}, 'example/path']],
],
[
'should throw error if themes is not an array of [string, object][] #3',
[[{}, {}]],
],
])(`%s for the input of: %p`, (_message, themes) => {
expect(() => {
normalizeConfig({
// @ts-expect-error: test
themes,
});
}).toThrowErrorMatchingSnapshot();
});

it.each([
['should accept [string] for plugins', ['plain/string']],
['should accept [string] for plugins', ensurePlugins(['plain/string'])],
[
'should accept string[] for plugins',
['plain/string', 'another/plain/string/path'],
ensurePlugins(['plain/string', 'another/plain/string/path']),
],
[
'should accept [string, object] for plugins',
[['plain/string', {it: 'should work'}]],
ensurePlugins([['plain/string', {it: 'should work'}]]),
],
[
'should accept [string, object][] for plugins',
[
ensurePlugins([
['plain/string', {it: 'should work'}],
['this/should/work', {too: 'yes'}],
],
]),
],
[
'should accept ([string, object]|string)[] for plugins',
[
ensurePlugins([
'plain/string',
['plain', {it: 'should work'}],
['this/should/work', {too: 'yes'}],
],
]),
],
[
'should accept function returning null',
ensurePlugins([
function plugin() {
return null;
},
]),
],
[
'should accept function returning plugin',
ensurePlugins([
function plugin() {
return {name: 'plugin'};
},
]),
],
[
'should accept function returning plugin or null',
ensurePlugins([
function plugin() {
return Math.random() > 0.5 ? null : {name: 'plugin'};
},
]),
],
[
'should accept async function returning null',
ensurePlugins([
async function plugin() {
return null;
},
]),
],
[
'should accept async function returning plugin',
ensurePlugins([
async function plugin() {
return {name: 'plugin'};
},
]),
],
[
'should accept function returning plugin or null',
ensurePlugins([
async function plugin() {
return Math.random() > 0.5 ? null : {name: 'plugin'};
},
]),
],
['should accept function for plugin', [function plugin() {}]],
['should accept async function for plugin', [async function plugin() {}]],
[
'should accept [function, object] for plugin',
[[() => {}, {it: 'should work'}]],
],
['should accept false/null for plugin', [false as const, null, 'classic']],
[
'should accept false/null for plugin',
ensurePlugins([false as const, null, 'classic']),
],
])(`%s for the input of: %p`, (_message, plugins) => {
expect(() => {
normalizeConfig({
Expand All @@ -562,6 +589,33 @@ describe('plugins', () => {
});

describe('themes', () => {
it.each([
['should throw error if themes is not array', {}],
[
"should throw error if themes is not a string and it's not an array #1",
[123],
],
[
'should throw error if themes is not an array of [string, object][] #1',
[['example/path', 'wrong parameter here']],
],
[
'should throw error if themes is not an array of [string, object][] #2',
[[{}, 'example/path']],
],
[
'should throw error if themes is not an array of [string, object][] #3',
[[{}, {}]],
],
])(`%s for the input of: %p`, (_message, themes) => {
expect(() => {
normalizeConfig({
// @ts-expect-error: test
themes,
});
}).toThrowErrorMatchingSnapshot();
});

it.each([
['should accept [string] for themes', ['plain/string']],
[
Expand Down

0 comments on commit 690b38e

Please sign in to comment.