Skip to content

Commit 690b38e

Browse files
committed
fix type for PluginModule
1 parent c4ec67f commit 690b38e

File tree

3 files changed

+98
-45
lines changed

3 files changed

+98
-45
lines changed

packages/docusaurus-types/src/plugin.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ export type LoadedPlugin = InitializedPlugin & {
191191
export type PluginModule<Content = unknown> = {
192192
(context: LoadContext, options: unknown):
193193
| Plugin<Content>
194-
| Promise<Plugin<Content>>
195194
| null
196-
| Promise<null>;
195+
| Promise<Plugin<Content> | null>;
197196

198197
validateOptions?: <T, U>(data: OptionValidationContext<T, U>) => U;
199198
validateThemeConfig?: <T>(data: ThemeConfigValidationContext<T>) => T;

packages/docusaurus/src/server/__tests__/__snapshots__/configValidation.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ exports[`plugins should throw error if plugins is not array for the input of: {}
9393
"
9494
`;
9595

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

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

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

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

160-
exports[`plugins should throw error if themes is not array for the input of: {} 1`] = `
160+
exports[`themes should throw error if themes is not array for the input of: {} 1`] = `
161161
""themes" must be an array
162162
"
163163
`;

packages/docusaurus/src/server/__tests__/configValidation.test.ts

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
validateConfig,
1313
} from '../configValidation';
1414
import type {StorageConfig} from '@docusaurus/types/src/config';
15-
import type {Config, DocusaurusConfig} from '@docusaurus/types';
15+
import type {Config, DocusaurusConfig, PluginConfig} from '@docusaurus/types';
1616
import type {DeepPartial} from 'utility-types';
1717

1818
const baseConfig = {
@@ -466,6 +466,11 @@ describe('markdown', () => {
466466
});
467467

468468
describe('plugins', () => {
469+
// Only here to verify typing
470+
function ensurePlugins(plugins: PluginConfig[]): PluginConfig[] {
471+
return plugins;
472+
}
473+
469474
it.each([
470475
['should throw error if plugins is not array', {}],
471476
[
@@ -494,64 +499,86 @@ describe('plugins', () => {
494499
});
495500

496501
it.each([
497-
['should throw error if themes is not array', {}],
498-
[
499-
"should throw error if themes is not a string and it's not an array #1",
500-
[123],
501-
],
502-
[
503-
'should throw error if themes is not an array of [string, object][] #1',
504-
[['example/path', 'wrong parameter here']],
505-
],
506-
[
507-
'should throw error if themes is not an array of [string, object][] #2',
508-
[[{}, 'example/path']],
509-
],
510-
[
511-
'should throw error if themes is not an array of [string, object][] #3',
512-
[[{}, {}]],
513-
],
514-
])(`%s for the input of: %p`, (_message, themes) => {
515-
expect(() => {
516-
normalizeConfig({
517-
// @ts-expect-error: test
518-
themes,
519-
});
520-
}).toThrowErrorMatchingSnapshot();
521-
});
522-
523-
it.each([
524-
['should accept [string] for plugins', ['plain/string']],
502+
['should accept [string] for plugins', ensurePlugins(['plain/string'])],
525503
[
526504
'should accept string[] for plugins',
527-
['plain/string', 'another/plain/string/path'],
505+
ensurePlugins(['plain/string', 'another/plain/string/path']),
528506
],
529507
[
530508
'should accept [string, object] for plugins',
531-
[['plain/string', {it: 'should work'}]],
509+
ensurePlugins([['plain/string', {it: 'should work'}]]),
532510
],
533511
[
534512
'should accept [string, object][] for plugins',
535-
[
513+
ensurePlugins([
536514
['plain/string', {it: 'should work'}],
537515
['this/should/work', {too: 'yes'}],
538-
],
516+
]),
539517
],
540518
[
541519
'should accept ([string, object]|string)[] for plugins',
542-
[
520+
ensurePlugins([
543521
'plain/string',
544522
['plain', {it: 'should work'}],
545523
['this/should/work', {too: 'yes'}],
546-
],
524+
]),
525+
],
526+
[
527+
'should accept function returning null',
528+
ensurePlugins([
529+
function plugin() {
530+
return null;
531+
},
532+
]),
533+
],
534+
[
535+
'should accept function returning plugin',
536+
ensurePlugins([
537+
function plugin() {
538+
return {name: 'plugin'};
539+
},
540+
]),
541+
],
542+
[
543+
'should accept function returning plugin or null',
544+
ensurePlugins([
545+
function plugin() {
546+
return Math.random() > 0.5 ? null : {name: 'plugin'};
547+
},
548+
]),
549+
],
550+
[
551+
'should accept async function returning null',
552+
ensurePlugins([
553+
async function plugin() {
554+
return null;
555+
},
556+
]),
557+
],
558+
[
559+
'should accept async function returning plugin',
560+
ensurePlugins([
561+
async function plugin() {
562+
return {name: 'plugin'};
563+
},
564+
]),
565+
],
566+
[
567+
'should accept function returning plugin or null',
568+
ensurePlugins([
569+
async function plugin() {
570+
return Math.random() > 0.5 ? null : {name: 'plugin'};
571+
},
572+
]),
547573
],
548-
['should accept function for plugin', [function plugin() {}]],
549-
['should accept async function for plugin', [async function plugin() {}]],
550574
[
551575
'should accept [function, object] for plugin',
552576
[[() => {}, {it: 'should work'}]],
553577
],
554-
['should accept false/null for plugin', [false as const, null, 'classic']],
578+
[
579+
'should accept false/null for plugin',
580+
ensurePlugins([false as const, null, 'classic']),
581+
],
555582
])(`%s for the input of: %p`, (_message, plugins) => {
556583
expect(() => {
557584
normalizeConfig({
@@ -562,6 +589,33 @@ describe('plugins', () => {
562589
});
563590

564591
describe('themes', () => {
592+
it.each([
593+
['should throw error if themes is not array', {}],
594+
[
595+
"should throw error if themes is not a string and it's not an array #1",
596+
[123],
597+
],
598+
[
599+
'should throw error if themes is not an array of [string, object][] #1',
600+
[['example/path', 'wrong parameter here']],
601+
],
602+
[
603+
'should throw error if themes is not an array of [string, object][] #2',
604+
[[{}, 'example/path']],
605+
],
606+
[
607+
'should throw error if themes is not an array of [string, object][] #3',
608+
[[{}, {}]],
609+
],
610+
])(`%s for the input of: %p`, (_message, themes) => {
611+
expect(() => {
612+
normalizeConfig({
613+
// @ts-expect-error: test
614+
themes,
615+
});
616+
}).toThrowErrorMatchingSnapshot();
617+
});
618+
565619
it.each([
566620
['should accept [string] for themes', ['plain/string']],
567621
[

0 commit comments

Comments
 (0)