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

@ -46,7 +46,7 @@ describe('flat', () => {
expect(
flat({
foo: {
bar: value,
bar: value as string,
},
}),
).toEqual({

View file

@ -7,13 +7,14 @@
import {jest} from '@jest/globals';
import normalizeLocation from '../normalizeLocation';
import type {Location} from 'history';
describe('normalizeLocation', () => {
it('rewrites locations with index.html', () => {
expect(
normalizeLocation({
pathname: '/index.html',
}),
} as Location),
).toEqual({
pathname: '/',
});
@ -23,7 +24,7 @@ describe('normalizeLocation', () => {
pathname: '/docs/introduction/index.html',
search: '?search=foo',
hash: '#features',
}),
} as Location),
).toEqual({
pathname: '/docs/introduction',
search: '?search=foo',
@ -35,7 +36,7 @@ describe('normalizeLocation', () => {
pathname: '/index.html',
search: '',
hash: '#features',
}),
} as Location),
).toEqual({
pathname: '/',
search: '',
@ -47,7 +48,7 @@ describe('normalizeLocation', () => {
expect(
normalizeLocation({
pathname: '/docs/installation.html',
}),
} as Location),
).toEqual({
pathname: '/docs/installation',
});
@ -56,7 +57,7 @@ describe('normalizeLocation', () => {
pathname: '/docs/introduction/foo.html',
search: '',
hash: '#bar',
}),
} as Location),
).toEqual({
pathname: '/docs/introduction/foo',
search: '',
@ -65,7 +66,7 @@ describe('normalizeLocation', () => {
});
it('does not strip extension if the route location has one', () => {
expect(normalizeLocation({pathname: '/page.html'})).toEqual({
expect(normalizeLocation({pathname: '/page.html'} as Location)).toEqual({
pathname: '/page.html',
});
});
@ -78,7 +79,7 @@ describe('normalizeLocation', () => {
pathname: '/docs/introduction',
search: '',
hash: '#features',
}),
} as Location),
).toEqual({
pathname: '/docs/introduction',
search: '',
@ -91,7 +92,7 @@ describe('normalizeLocation', () => {
pathname: '/docs/introduction',
search: '',
hash: '#features',
}),
} as Location),
).toEqual({
pathname: '/docs/introduction',
search: '',
@ -102,7 +103,7 @@ describe('normalizeLocation', () => {
expect(
normalizeLocation({
pathname: '/',
}),
} as Location),
).toEqual({
pathname: '/',
});

View file

@ -31,7 +31,7 @@ describe('interpolate', () => {
object: {hello: 'world'},
array: ['Hello'],
};
// Do we need to improve the JS type -> String conversion logic here?
// @ts-expect-error: test
expect(interpolate(text, values)).toMatchInlineSnapshot(
`"42 Hello [object Object] Hello"`,
);
@ -52,6 +52,7 @@ describe('interpolate', () => {
// Should we emit warnings in such case?
const text = 'Hello {name} how are you {unprovidedValue}?';
const values = {name: 'Sébastien', extraValue: 'today'};
// @ts-expect-error: test
expect(interpolate(text, values)).toMatchInlineSnapshot(
`"Hello Sébastien how are you {unprovidedValue}?"`,
);
@ -61,6 +62,7 @@ describe('interpolate', () => {
// Should we emit warnings in such case?
const text = 'Hello {name} how are you {day}?';
expect(interpolate(text)).toEqual(text);
// @ts-expect-error: test
expect(interpolate(text, {})).toEqual(text);
});
@ -84,6 +86,7 @@ describe('interpolate', () => {
extraUselessValue1: <div>test</div>,
extraUselessValue2: 'hi',
};
// @ts-expect-error: test
expect(interpolate(text, values)).toMatchSnapshot();
});
});
@ -133,6 +136,7 @@ describe('<Interpolate>', () => {
`"The Docusaurus <Interpolate> component only accept simple string values. Received: React element"`,
);
expect(() =>
// @ts-expect-error: test
renderer.create(<Interpolate>{null}</Interpolate>),
).toThrowErrorMatchingInlineSnapshot(
`"The Docusaurus <Interpolate> component only accept simple string values. Received: object"`,

View file

@ -28,7 +28,7 @@ describe('useBaseUrl', () => {
baseUrl: '/',
url: 'https://docusaurus.io',
},
});
} as DocusaurusContext);
expect(mockUseBaseUrl('hello')).toBe('/hello');
expect(mockUseBaseUrl('/hello')).toBe('/hello');
@ -56,7 +56,7 @@ describe('useBaseUrl', () => {
baseUrl: '/docusaurus/',
url: 'https://docusaurus.io',
},
});
} as DocusaurusContext);
expect(mockUseBaseUrl('')).toBe('');
expect(mockUseBaseUrl('hello')).toBe('/docusaurus/hello');
@ -97,7 +97,7 @@ describe('useBaseUrlUtils().withBaseUrl()', () => {
baseUrl: '/',
url: 'https://docusaurus.io',
},
});
} as DocusaurusContext);
expect(withBaseUrl('hello')).toBe('/hello');
expect(withBaseUrl('/hello')).toBe('/hello');
@ -125,7 +125,7 @@ describe('useBaseUrlUtils().withBaseUrl()', () => {
baseUrl: '/docusaurus/',
url: 'https://docusaurus.io',
},
});
} as DocusaurusContext);
expect(withBaseUrl('hello')).toBe('/docusaurus/hello');
expect(withBaseUrl('/hello')).toBe('/docusaurus/hello');

View file

@ -7,6 +7,7 @@
import path from 'path';
import {loadPlugins} from '..';
import type {Props} from '@docusaurus/types';
describe('loadPlugins', () => {
it('loads plugins', async () => {
@ -16,7 +17,6 @@ describe('loadPlugins', () => {
siteDir,
generatedFilesDir: path.join(siteDir, '.docusaurus'),
outDir: path.join(siteDir, 'build'),
// @ts-expect-error: good enough
siteConfig: {
baseUrl: '/',
trailingSlash: true,
@ -51,7 +51,7 @@ describe('loadPlugins', () => {
],
},
siteConfigPath: path.join(siteDir, 'docusaurus.config.js'),
}),
} as unknown as Props),
).resolves.toMatchSnapshot();
});
});

View file

@ -24,14 +24,14 @@ describe('initPlugins', () => {
expect(context.siteConfig.plugins).toHaveLength(4);
expect(plugins).toHaveLength(8);
expect(plugins[0].name).toBe('preset-plugin1');
expect(plugins[1].name).toBe('preset-plugin2');
expect(plugins[2].name).toBe('preset-theme1');
expect(plugins[3].name).toBe('preset-theme2');
expect(plugins[4].name).toBe('first-plugin');
expect(plugins[5].name).toBe('second-plugin');
expect(plugins[6].name).toBe('third-plugin');
expect(plugins[7].name).toBe('fourth-plugin');
expect(plugins[0]!.name).toBe('preset-plugin1');
expect(plugins[1]!.name).toBe('preset-plugin2');
expect(plugins[2]!.name).toBe('preset-theme1');
expect(plugins[3]!.name).toBe('preset-theme2');
expect(plugins[4]!.name).toBe('first-plugin');
expect(plugins[5]!.name).toBe('second-plugin');
expect(plugins[6]!.name).toBe('third-plugin');
expect(plugins[7]!.name).toBe('fourth-plugin');
expect(context.siteConfig.themeConfig).toEqual({a: 1});
});

View file

@ -8,12 +8,11 @@
import {ensureUniquePluginInstanceIds} from '../pluginIds';
import type {InitializedPlugin} from '@docusaurus/types';
function createTestPlugin(name: string, id?: string): InitializedPlugin {
// @ts-expect-error: good enough for tests
function createTestPlugin(name: string, id?: string) {
return {
name,
options: {id},
};
options: {id: id ?? 'default'},
} as InitializedPlugin;
}
describe('ensureUniquePluginInstanceIds', () => {

View file

@ -62,7 +62,7 @@ describe('loadPresets', () => {
[path.join(__dirname, '__fixtures__/presets/preset-plugins.js')],
],
},
} as Partial<LoadContext>;
} as unknown as LoadContext;
const presets = await loadPresets(context);
expect(presets).toMatchSnapshot();
});
@ -78,7 +78,7 @@ describe('loadPresets', () => {
],
],
},
} as Partial<LoadContext>;
} as unknown as LoadContext;
const presets = await loadPresets(context);
expect(presets).toMatchSnapshot();
});
@ -98,7 +98,7 @@ describe('loadPresets', () => {
],
],
},
} as Partial<LoadContext>;
} as unknown as LoadContext;
const presets = await loadPresets(context);
expect(presets).toMatchSnapshot();
});

View file

@ -20,6 +20,7 @@ import {
} from '../translations';
import type {
InitializedPlugin,
LoadedPlugin,
TranslationFile,
TranslationFileContent,
} from '@docusaurus/types';
@ -399,7 +400,7 @@ describe('writePluginTranslations', () => {
options: {
id: 'my-plugin-id',
},
},
} as LoadedPlugin,
options: {},
}),
@ -426,11 +427,10 @@ describe('localizePluginTranslationFile', () => {
siteDir,
locale: 'fr',
translationFile,
// @ts-expect-error: enough for this test
plugin: {
name: 'my-plugin-name',
options: {},
},
} as LoadedPlugin,
});
expect(localizedTranslationFile).toEqual(translationFile);
@ -466,11 +466,10 @@ describe('localizePluginTranslationFile', () => {
siteDir,
locale: 'fr',
translationFile,
// @ts-expect-error: enough for this test
plugin: {
name: 'my-plugin-name',
options: {},
},
} as LoadedPlugin,
});
expect(localizedTranslationFile).toEqual({
@ -521,25 +520,30 @@ describe('readCodeTranslationFileContent', () => {
it('fails for invalid translation file content', async () => {
await expect(() =>
// @ts-expect-error: test
testReadTranslation('HEY'),
).rejects.toThrowErrorMatchingInlineSnapshot(
`""value" must be of type object"`,
);
await expect(() =>
// @ts-expect-error: test
testReadTranslation(42),
).rejects.toThrowErrorMatchingInlineSnapshot(
`""value" must be of type object"`,
);
await expect(() =>
// @ts-expect-error: test
testReadTranslation({key: {description: 'no message'}}),
).rejects.toThrowErrorMatchingInlineSnapshot(`""key.message" is required"`);
await expect(() =>
// @ts-expect-error: test
testReadTranslation({key: {message: 42}}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`""key.message" must be a string"`,
);
await expect(() =>
testReadTranslation({
// @ts-expect-error: test
key: {message: 'Message', description: 42},
}),
).rejects.toThrowErrorMatchingInlineSnapshot(

View file

@ -15,7 +15,7 @@ import {
extractSiteSourceCodeTranslations,
} from '../translationsExtractor';
import {getBabelOptions} from '../../../webpack/utils';
import type {InitializedPlugin} from '@docusaurus/types';
import type {InitializedPlugin, LoadedPlugin} from '@docusaurus/types';
const TestBabelOptions = getBabelOptions({
isServer: true,
@ -693,7 +693,7 @@ export default function MyComponent(props: Props) {
plugin1,
plugin2,
{name: 'dummy', options: {}, version: {type: 'synthetic'}} as const,
];
] as LoadedPlugin[];
const translations = await extractSiteSourceCodeTranslations(
siteDir,
plugins,

View file

@ -63,7 +63,7 @@ describe('babel transpilation exclude logic', () => {
});
describe('base webpack config', () => {
const props: Props = {
const props = {
outDir: '',
siteDir: path.resolve(__dirname, '__fixtures__', 'base_test_site'),
siteConfig: {staticDirectories: ['static']},
@ -98,7 +98,7 @@ describe('base webpack config', () => {
},
},
],
};
} as Props;
afterEach(() => {
jest.restoreAllMocks();

View file

@ -15,7 +15,11 @@ describe('webpack production config', () => {
it('simple', async () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
const props = await loadSetup('simple');
const config = await createServerConfig({props});
const config = await createServerConfig({
props,
onHeadTagsCollected: () => {},
onLinksCollected: () => {},
});
const errors = webpack.validate(config);
expect(errors).toBeUndefined();
});
@ -23,7 +27,11 @@ describe('webpack production config', () => {
it('custom', async () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
const props = await loadSetup('custom');
const config = await createServerConfig({props});
const config = await createServerConfig({
props,
onHeadTagsCollected: () => {},
onLinksCollected: () => {},
});
const errors = webpack.validate(config);
expect(errors).toBeUndefined();
});

View file

@ -60,6 +60,7 @@ describe('extending generated webpack config', () => {
},
};
// @ts-expect-error: Testing an edge-case that we did not write types for
const configureWebpack: Plugin['configureWebpack'] = (
generatedConfig,
isServer,
@ -125,14 +126,16 @@ describe('extending generated webpack config', () => {
},
};
const createConfigureWebpack: (mergeStrategy?: {
[key: string]: 'prepend' | 'append';
}) => Plugin['configureWebpack'] = (mergeStrategy) => () => ({
module: {
rules: [{use: 'zzz'}],
},
mergeStrategy,
});
const createConfigureWebpack =
(mergeStrategy?: {
[key: string]: 'prepend' | 'append';
}): Plugin['configureWebpack'] =>
() => ({
module: {
rules: [{use: 'zzz'}],
},
mergeStrategy,
});
const defaultStrategyMergeConfig = applyConfigureWebpack(
createConfigureWebpack(),