test: fix some type errors in test files (#7486)

This commit is contained in:
Joshua Chen 2022-05-25 11:46:10 +08:00 committed by GitHub
parent 624735bd92
commit e2e40b8f5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 319 additions and 182 deletions

View file

@ -44,11 +44,14 @@ exports[`translateContent falls back when translation is incomplete 1`] = `
],
"blogPosts": [
{
"content": "",
"id": "hello",
"metadata": {
"authors": [],
"date": 2021-07-19T00:00:00.000Z,
"description": "/blog/2021/06/19/hello",
"formattedDate": "June 19, 2021",
"frontMatter": {},
"permalink": "/blog/2021/06/19/hello",
"source": "/blog/2021/06/19/hello",
"tags": [],
@ -85,11 +88,14 @@ exports[`translateContent returns translated loaded 1`] = `
],
"blogPosts": [
{
"content": "",
"id": "hello",
"metadata": {
"authors": [],
"date": 2021-07-19T00:00:00.000Z,
"description": "/blog/2021/06/19/hello",
"formattedDate": "June 19, 2021",
"frontMatter": {},
"permalink": "/blog/2021/06/19/hello",
"source": "/blog/2021/06/19/hello",
"tags": [],

View file

@ -211,7 +211,11 @@ describe('linkify', () => {
title: 'date-matter',
},
truncated: false,
frontMatter: {},
authors: [],
formattedDate: '',
},
content: '',
},
];

View file

@ -21,6 +21,9 @@ const DefaultI18N: I18n = {
defaultLocale: 'en',
localeConfigs: {
en: {
label: 'English',
direction: 'ltr',
htmlLang: 'en',
calendar: 'gregory',
},
},

View file

@ -56,7 +56,9 @@ function testField(params: {
);
} catch (err) {
// eslint-disable-next-line jest/no-conditional-expect
expect(err.message).toMatch(new RegExp(escapeStringRegexp(message)));
expect((err as Error).message).toMatch(
new RegExp(escapeStringRegexp(message)),
);
}
});
});

View file

@ -41,7 +41,14 @@ function getI18n(locale: string): I18n {
currentLocale: locale,
locales: [locale],
defaultLocale: locale,
localeConfigs: {[locale]: {calendar: 'gregory'}},
localeConfigs: {
[locale]: {
calendar: 'gregory',
label: locale,
htmlLang: locale,
direction: 'ltr',
},
},
};
}
@ -297,28 +304,28 @@ describe('blog plugin', () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const blogPostsFrench = await getBlogPosts(siteDir, {}, getI18n('fr'));
expect(blogPostsFrench).toHaveLength(8);
expect(blogPostsFrench[0].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[0]!.metadata.formattedDate).toMatchInlineSnapshot(
`"6 mars 2021"`,
);
expect(blogPostsFrench[1].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[1]!.metadata.formattedDate).toMatchInlineSnapshot(
`"5 mars 2021"`,
);
expect(blogPostsFrench[2].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[2]!.metadata.formattedDate).toMatchInlineSnapshot(
`"16 août 2020"`,
);
expect(blogPostsFrench[3].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[3]!.metadata.formattedDate).toMatchInlineSnapshot(
`"15 août 2020"`,
);
expect(blogPostsFrench[4].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[4]!.metadata.formattedDate).toMatchInlineSnapshot(
`"27 février 2020"`,
);
expect(blogPostsFrench[5].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[5]!.metadata.formattedDate).toMatchInlineSnapshot(
`"2 janvier 2019"`,
);
expect(blogPostsFrench[6].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[6]!.metadata.formattedDate).toMatchInlineSnapshot(
`"1 janvier 2019"`,
);
expect(blogPostsFrench[7].metadata.formattedDate).toMatchInlineSnapshot(
expect(blogPostsFrench[7]!.metadata.formattedDate).toMatchInlineSnapshot(
`"14 décembre 2018"`,
);
});

View file

@ -9,7 +9,7 @@ import {normalizePluginOptions} from '@docusaurus/utils-validation';
import {validateOptions, DEFAULT_OPTIONS} from '../options';
import type {Options} from '@docusaurus/plugin-content-blog';
function testValidate(options: Options) {
function testValidate(options?: Options) {
return validateOptions({validate: normalizePluginOptions, options});
}
@ -44,13 +44,14 @@ describe('validateOptions', () => {
});
it('accepts valid user options', () => {
const userOptions = {
const userOptions: Options = {
...defaultOptions,
routeBasePath: 'myBlog',
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
rehypePlugins: [
// @ts-expect-error: it seems to work in practice
markdownPluginsObjectStub,
[markdownPluginsFunctionStub, {option1: '42'}],
],
@ -73,6 +74,7 @@ describe('validateOptions', () => {
expect(() =>
testValidate({
feedOptions: {
// @ts-expect-error: test
type: 'none',
},
}),
@ -138,6 +140,7 @@ describe('validateOptions', () => {
it('rejects "abcdef" sidebar count', () => {
const userOptions = {blogSidebarCount: 'abcdef'};
// @ts-expect-error: test
expect(() => testValidate(userOptions)).toThrowErrorMatchingInlineSnapshot(
`""blogSidebarCount" must be one of [ALL, number]"`,
);
@ -153,6 +156,7 @@ describe('validateOptions', () => {
it('rejects 42 sidebar title', () => {
const userOptions = {blogSidebarTitle: 42};
// @ts-expect-error: test
expect(() => testValidate(userOptions)).toThrowErrorMatchingInlineSnapshot(
`""blogSidebarTitle" must be a string"`,
);

View file

@ -33,7 +33,10 @@ const sampleBlogPosts: BlogPost[] = [
tags: [],
title: 'Hello',
truncated: true,
authors: [],
frontMatter: {},
},
content: '',
},
];

View file

@ -135,7 +135,7 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
export function validateOptions({
validate,
options,
}: OptionValidationContext<Options, PluginOptions>): PluginOptions {
}: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions {
const validatedOptions = validate(PluginOptionSchema, options);
return validatedOptions;
}

View file

@ -13,7 +13,7 @@ import footnoteIDFixer from '../footnoteIDFixer';
const processFixture = async (name: string) => {
const filepath = path.join(__dirname, `__fixtures__/${name}.md`);
const result = await mdx(await fs.readFile(filepath), {
const result = await mdx(await fs.readFile(filepath, 'utf8'), {
filepath,
remarkPlugins: [footnoteIDFixer],
});