From c12cbfa48f0625607fb06c43c2b506b11aa61694 Mon Sep 17 00:00:00 2001 From: sebastien Date: Fri, 23 Aug 2024 12:40:42 +0200 Subject: [PATCH] add workaround to make tests happy --- .../src/__tests__/cli.test.ts | Bin 14593 -> 14619 bytes .../src/__tests__/index.test.ts | 2 +- .../docusaurus-plugin-content-docs/src/cli.ts | 16 +++++++++++++++- .../src/index.ts | 4 ++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts index 2930b70ef1656bbbb44bb380a2a00f7ef834a3c2..baa34816943d2cd70c264557f7393bb9343e655f 100644 GIT binary patch delta 38 ocmZoHnq9OZh><-xC(|WAxp;CWFaZHQN02f*f*Z=?k delta 24 gcmbPT)L673h>@{+GNZOAW9{Tf#($d|nP!;*0B)NI!vFvP diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts index 2030e4da0d77..d399ac0ae4d0 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts @@ -25,7 +25,7 @@ import pluginContentDocs from '../index'; import {toSidebarsProp} from '../props'; import {DefaultSidebarItemsGenerator} from '../sidebars/generator'; import {DisabledSidebars} from '../sidebars'; -import * as cliDocs from '../cli'; +import cliDocs from '../cli'; import {validateOptions} from '../options'; import type {RouteConfig, Validate, Plugin} from '@docusaurus/types'; diff --git a/packages/docusaurus-plugin-content-docs/src/cli.ts b/packages/docusaurus-plugin-content-docs/src/cli.ts index 5cc52f2a5f9b..ed5b46e29a44 100644 --- a/packages/docusaurus-plugin-content-docs/src/cli.ts +++ b/packages/docusaurus-plugin-content-docs/src/cli.ts @@ -53,7 +53,7 @@ async function createVersionedSidebarFile({ } // Tests depend on non-default export for mocking. -export async function cliDocsVersionCommand( +async function cliDocsVersionCommand( version: unknown, {id: pluginId, path: docsPath, sidebarPath}: PluginOptions, {siteDir, i18n}: LoadContext, @@ -142,3 +142,17 @@ export async function cliDocsVersionCommand( logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`; } + +// TODO try to remove this workaround +// Why use a default export instead of named exports here? +// This is only to make Jest mocking happy +// After upgrading Jest/SWC we got this weird mocking error in extendCli tests +// "spyOn: Cannot redefine property cliDocsVersionCommand" +// I tried various workarounds, and it's the only one that worked :/ +// See also: +// - https://pyk.sh/fixing-typeerror-cannot-redefine-property-x-error-in-jest-tests#heading-solution-2-using-barrel-imports +// - https://github.com/aelbore/esbuild-jest/issues/26 +// - https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property/69951703#69951703 +export default { + cliDocsVersionCommand, +}; diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index c45c520ac5d0..878ffd34c401 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -40,7 +40,7 @@ import { readVersionsMetadata, toFullVersion, } from './versions'; -import {cliDocsVersionCommand} from './cli'; +import cliDocs from './cli'; import {VERSIONS_JSON_FILE} from './constants'; import {toGlobalDataVersion} from './globalData'; import { @@ -134,7 +134,7 @@ export default async function pluginContentDocs( .arguments('') .description(commandDescription) .action((version: unknown) => - cliDocsVersionCommand(version, options, context), + cliDocs.cliDocsVersionCommand(version, options, context), ); },