Skip to content

Commit

Permalink
test: fix some type errors in test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 25, 2022
1 parent 624735b commit a008e46
Show file tree
Hide file tree
Showing 50 changed files with 320 additions and 183 deletions.
17 changes: 17 additions & 0 deletions jest/deps.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// modules only used in tests

declare module 'to-vfile';

declare module 'remark-mdx';

declare module '@testing-utils/git' {
const createTempRepo: typeof import('./utils/git').createTempRepo;
export {createTempRepo};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function process(doc: string, plugins: Plugin[] = []) {
return removePosition(processor.runSync(processor.parse(doc)), true);
}

function heading(label: string, id: string) {
function heading(label: string | null, id: string) {
return u(
'heading',
{depth: 2, data: {id, hProperties: {id}}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import plugin from '..';
import transformImage, {type PluginOptions} from '../../transformImage';

const processFixture = async (name: string, options?: PluginOptions) => {
const filePath = path.join(__dirname, `__fixtures__/${name}.md`);
const siteDir = path.join(__dirname, `__fixtures__`);
const staticDirs = [
path.join(__dirname, '__fixtures__/static'),
path.join(__dirname, '__fixtures__/static2'),
path.join(siteDir, 'static'),
path.join(siteDir, 'static2'),
];
const file = await vfile.read(filePath);
const file = await vfile.read(path.join(siteDir, `${name}.md`));
const result = await remark()
.use(mdx)
.use(transformImage, {...options, filePath, staticDirs})
.use(transformImage, {...options, siteDir, staticDirs})
.use(plugin, {
...options,
staticDirs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {normalizePluginOptions} from '@docusaurus/utils-validation';
import {validateOptions, DEFAULT_OPTIONS} from '../options';
import type {Options} from '../options';

function testValidate(options: Options) {
function testValidate(options?: Options) {
return validateOptions({validate: normalizePluginOptions, options});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ describe('writeRedirectFiles', () => {
await writeRedirectFiles(filesMetadata);

await expect(
fs.readFile(filesMetadata[0].fileAbsolutePath, 'utf8'),
fs.readFile(filesMetadata[0]!.fileAbsolutePath, 'utf8'),
).resolves.toBe('content 1');

await expect(
fs.readFile(filesMetadata[1].fileAbsolutePath, 'utf8'),
fs.readFile(filesMetadata[1]!.fileAbsolutePath, 'utf8'),
).resolves.toBe('content 2');
});

Expand All @@ -195,7 +195,7 @@ describe('writeRedirectFiles', () => {
];

await fs.outputFile(
filesMetadata[0].fileAbsolutePath,
filesMetadata[0]!.fileAbsolutePath,
'file already exists!',
);

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-client-redirects/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ const UserOptionsSchema = Joi.object<PluginOptions>({
export function validateOptions({
validate,
options: userOptions,
}: OptionValidationContext<Options, PluginOptions>): PluginOptions {
}: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions {
return validate(UserOptionsSchema, userOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down Expand Up @@ -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": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ describe('linkify', () => {
title: 'date-matter',
},
truncated: false,
frontMatter: {},
authors: [],
formattedDate: '',
},
content: '',
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const DefaultI18N: I18n = {
defaultLocale: 'en',
localeConfigs: {
en: {
label: 'English',
direction: 'ltr',
htmlLang: 'en',
calendar: 'gregory',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
};
}

Expand Down Expand Up @@ -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"`,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

Expand Down Expand Up @@ -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'}],
],
Expand All @@ -73,6 +74,7 @@ describe('validateOptions', () => {
expect(() =>
testValidate({
feedOptions: {
// @ts-expect-error: test
type: 'none',
},
}),
Expand Down Expand Up @@ -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]"`,
);
Expand All @@ -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"`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const sampleBlogPosts: BlogPost[] = [
tags: [],
title: 'Hello',
truncated: true,
authors: [],
frontMatter: {},
},
content: '',
},
];

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});
Expand Down
Binary file modified packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {toGlobalDataVersion} from '../globalData';
import {createSidebarsUtils} from '../sidebars/utils';
import {getCategoryGeneratedIndexMetadataList} from '../categoryGeneratedIndex';
import type {Sidebars} from '../sidebars/types';
import type {DocMetadata} from '@docusaurus/plugin-content-docs';

describe('toGlobalDataVersion', () => {
it('generates the right docs, sidebars, and metadata', () => {
Expand All @@ -25,7 +26,7 @@ describe('toGlobalDataVersion', () => {
sidebar: 'tutorial',
frontMatter: {},
},
];
] as DocMetadata[];
const sidebars: Sidebars = {
tutorial: [
{
Expand All @@ -46,6 +47,8 @@ describe('toGlobalDataVersion', () => {
id: 'doc',
},
],
collapsed: false,
collapsible: true,
},
],
links: [
Expand Down Expand Up @@ -75,6 +78,8 @@ describe('toGlobalDataVersion', () => {
id: 'doc',
},
],
collapsed: false,
collapsible: true,
},
],
};
Expand All @@ -85,15 +90,14 @@ describe('toGlobalDataVersion', () => {
label: 'Label',
isLast: true,
path: '/current',
mainDocId: 'main',
docs,
drafts: [
{
unversionedId: 'some-draft-id',
permalink: '/current/draft',
sidebar: undefined,
},
],
] as DocMetadata[],
sidebars,
categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({
docs,
Expand Down
Loading

0 comments on commit a008e46

Please sign in to comment.