diff --git a/__mocks__/@org/vuepress-plugin-a.js b/__mocks__/@org/vuepress-plugin-mocked-a.js similarity index 100% rename from __mocks__/@org/vuepress-plugin-a.js rename to __mocks__/@org/vuepress-plugin-mocked-a.js diff --git a/__mocks__/@org/vuepress-plugin-b.js b/__mocks__/@org/vuepress-plugin-mocked-b.js similarity index 100% rename from __mocks__/@org/vuepress-plugin-b.js rename to __mocks__/@org/vuepress-plugin-mocked-b.js diff --git a/__mocks__/@org/vuepress-theme-a/index.js b/__mocks__/@org/vuepress-theme-mocked-a/index.js similarity index 100% rename from __mocks__/@org/vuepress-theme-a/index.js rename to __mocks__/@org/vuepress-theme-mocked-a/index.js diff --git a/__mocks__/@vuepress/plugin-a.js b/__mocks__/@vuepress/plugin-mocked-a.js similarity index 100% rename from __mocks__/@vuepress/plugin-a.js rename to __mocks__/@vuepress/plugin-mocked-a.js diff --git a/__mocks__/@vuepress/theme-a/index.js b/__mocks__/@vuepress/theme-mocked-a/index.js similarity index 100% rename from __mocks__/@vuepress/theme-a/index.js rename to __mocks__/@vuepress/theme-mocked-a/index.js diff --git a/__mocks__/vuepress-plugin-a.js b/__mocks__/vuepress-plugin-mocked-a.js similarity index 100% rename from __mocks__/vuepress-plugin-a.js rename to __mocks__/vuepress-plugin-mocked-a.js diff --git a/__mocks__/vuepress-plugin-b.js b/__mocks__/vuepress-plugin-mocked-b.js similarity index 100% rename from __mocks__/vuepress-plugin-b.js rename to __mocks__/vuepress-plugin-mocked-b.js diff --git a/__mocks__/vuepress-theme-child/index.js b/__mocks__/vuepress-theme-child/index.js deleted file mode 100644 index 8cf79b81..00000000 --- a/__mocks__/vuepress-theme-child/index.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - extend: 'vuepress-theme-parent' -} diff --git a/__mocks__/vuepress-theme-a/index.js b/__mocks__/vuepress-theme-mocked-a/index.js similarity index 100% rename from __mocks__/vuepress-theme-a/index.js rename to __mocks__/vuepress-theme-mocked-a/index.js diff --git a/__mocks__/vuepress-theme-child/Layout.vue b/__mocks__/vuepress-theme-mocked-child/Layout.vue similarity index 100% rename from __mocks__/vuepress-theme-child/Layout.vue rename to __mocks__/vuepress-theme-mocked-child/Layout.vue diff --git a/__mocks__/vuepress-theme-child/components/Home.vue b/__mocks__/vuepress-theme-mocked-child/components/Home.vue similarity index 100% rename from __mocks__/vuepress-theme-child/components/Home.vue rename to __mocks__/vuepress-theme-mocked-child/components/Home.vue diff --git a/__mocks__/vuepress-theme-mocked-child/index.js b/__mocks__/vuepress-theme-mocked-child/index.js new file mode 100644 index 00000000..0e50d097 --- /dev/null +++ b/__mocks__/vuepress-theme-mocked-child/index.js @@ -0,0 +1,3 @@ +module.exports = { + extend: 'vuepress-theme-mocked-parent' +} diff --git a/__mocks__/vuepress-theme-parent/Layout.vue b/__mocks__/vuepress-theme-mocked-parent/Layout.vue similarity index 100% rename from __mocks__/vuepress-theme-parent/Layout.vue rename to __mocks__/vuepress-theme-mocked-parent/Layout.vue diff --git a/__mocks__/vuepress-theme-parent/components/Home.vue b/__mocks__/vuepress-theme-mocked-parent/components/Home.vue similarity index 100% rename from __mocks__/vuepress-theme-parent/components/Home.vue rename to __mocks__/vuepress-theme-mocked-parent/components/Home.vue diff --git a/__mocks__/vuepress-theme-parent/components/Sidebar.vue b/__mocks__/vuepress-theme-mocked-parent/components/Sidebar.vue similarity index 100% rename from __mocks__/vuepress-theme-parent/components/Sidebar.vue rename to __mocks__/vuepress-theme-mocked-parent/components/Sidebar.vue diff --git a/__mocks__/vuepress-theme-parent/index.js b/__mocks__/vuepress-theme-mocked-parent/index.js similarity index 100% rename from __mocks__/vuepress-theme-parent/index.js rename to __mocks__/vuepress-theme-mocked-parent/index.js diff --git a/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js b/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js index 2f21ad45..237f3859 100644 --- a/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js +++ b/packages/@vuepress/core/lib/node/__tests__/plugin-api/PluginAPI.spec.js @@ -1,6 +1,6 @@ -jest.mock('vuepress-plugin-a') -jest.mock('vuepress-plugin-b') -jest.mock('@org/vuepress-plugin-a') +jest.mock('vuepress-plugin-mocked-a') +jest.mock('vuepress-plugin-mocked-b') +jest.mock('@org/vuepress-plugin-mocked-a') import PluginAPI from '../../plugin-api/index' import { PLUGIN_OPTION_MAP } from '../../plugin-api/constants' @@ -16,37 +16,37 @@ describe('Plugin', () => { test('useByPluginsConfig', () => { [ - ['a'], - [['a']], - [['a', true]], - { a: true } + ['mocked-a'], + [['mocked-a']], + [['mocked-a', true]], + { 'mocked-a': true } ].forEach(pluginsConfig => { const api = new PluginAPI() api.useByPluginsConfig(pluginsConfig) expect(api.enabledPlugins).toHaveLength(1) - expect(api.enabledPlugins[0].name).toBe('vuepress-plugin-a') + expect(api.enabledPlugins[0].name).toBe('vuepress-plugin-mocked-a') expect(api.disabledPlugins).toHaveLength(0) }) }) test('useByPluginsConfig - disable plugin', () => { [ - [['a', false]], - { a: false } + [['mocked-a', false]], + { 'mocked-a': false } ].forEach(pluginsConfig => { const api = new PluginAPI() api.useByPluginsConfig(pluginsConfig) expect(api.enabledPlugins).toHaveLength(0) expect(api.disabledPlugins).toHaveLength(1) - expect(api.disabledPlugins[0].name).toBe('vuepress-plugin-a') + expect(api.disabledPlugins[0].name).toBe('vuepress-plugin-mocked-a') }) }) test('useByPluginsConfig - get options', () => { const pluginOptions = {}; [ - [['a', pluginOptions]], - { a: pluginOptions } + [['mocked-a', pluginOptions]], + { 'mocked-a': pluginOptions } ].forEach(pluginsConfig => { const api = new PluginAPI() api.useByPluginsConfig(pluginsConfig) @@ -59,9 +59,9 @@ describe('Plugin', () => { const pluginOptions2 = {} const pluginOptions3 = {} const pluginsConfig = [ - ['a', pluginOptions1], - ['a', pluginOptions2], - ['a', pluginOptions3] + ['mocked-a', pluginOptions1], + ['mocked-a', pluginOptions2], + ['mocked-a', pluginOptions3] ] const api = new PluginAPI() api.useByPluginsConfig(pluginsConfig) @@ -74,8 +74,8 @@ describe('Plugin', () => { const pluginOptions1 = { a: 1 } const pluginOptions2 = { b: 1 } const pluginsConfig = [ - ['b', pluginOptions1], - ['b', pluginOptions2] + ['mocked-b', pluginOptions1], + ['mocked-b', pluginOptions2] ] const api = new PluginAPI() api.useByPluginsConfig(pluginsConfig) diff --git a/packages/@vuepress/core/lib/node/__tests__/theme-api/index.spec.js b/packages/@vuepress/core/lib/node/__tests__/theme-api/index.spec.js index 89dccf98..02ca5fe6 100644 --- a/packages/@vuepress/core/lib/node/__tests__/theme-api/index.spec.js +++ b/packages/@vuepress/core/lib/node/__tests__/theme-api/index.spec.js @@ -1,19 +1,19 @@ -jest.mock('vuepress-theme-parent') -jest.mock('vuepress-theme-child') +jest.mock('vuepress-theme-mocked-parent') +jest.mock('vuepress-theme-mocked-child') import ThemeAPI from '../../theme-api' import { resolve } from 'path' const theme = { - path: resolve(process.cwd(), '__mocks__/vuepress-theme-child'), - name: 'vuepress-theme-child', + path: resolve(process.cwd(), '__mocks__/vuepress-theme-mocked-child'), + name: 'vuepress-theme-mocked-child', shortcut: 'child', - entryFile: require('vuepress-theme-child') + entryFile: require('vuepress-theme-mocked-child') } const parent = { - path: resolve(process.cwd(), '__mocks__/vuepress-theme-parent'), - name: 'vuepress-theme-parent', + path: resolve(process.cwd(), '__mocks__/vuepress-theme-mocked-parent'), + name: 'vuepress-theme-mocked-parent', shortcut: 'parent', entryFile: {} } @@ -23,5 +23,4 @@ describe('ThemeAPI', () => { const themeAPI = new ThemeAPI(theme, parent) console.log(themeAPI.theme.entry) }) - // loadTheme('vuepress-theme-child') }) diff --git a/packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts b/packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts index 5f3e9dad..44ca9e2f 100644 --- a/packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts +++ b/packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts @@ -1,10 +1,10 @@ -jest.mock('vuepress-plugin-a') -jest.mock('@org/vuepress-plugin-a') -jest.mock('@vuepress/plugin-a') +jest.mock('vuepress-plugin-mocked-a') +jest.mock('@org/vuepress-plugin-mocked-a') +jest.mock('@vuepress/plugin-mocked-a') -jest.mock('vuepress-theme-a') -jest.mock('@org/vuepress-theme-a') -jest.mock('@vuepress/theme-a') +jest.mock('vuepress-theme-mocked-a') +jest.mock('@org/vuepress-theme-mocked-a') +jest.mock('@vuepress/theme-mocked-a') import path from 'path' import { @@ -39,25 +39,25 @@ afterAll(() => { describe('resolveScopePackage', () => { test('corrent format', () => { - const pkg = resolveScopePackage('@vuepress/plugin-a') + const pkg = resolveScopePackage('@vuepress/plugin-mocked-a') expect(pkg.org).toBe('vuepress') - expect(pkg.name).toBe('plugin-a') + expect(pkg.name).toBe('plugin-mocked-a') }) test('incorrect format', () => { - const pkg2 = resolveScopePackage('vuepress/plugin-a') + const pkg2 = resolveScopePackage('vuepress/plugin-mocked-a') expect(pkg2).toEqual({ "name": "", "org": "" }) - const pkg3 = resolveScopePackage('vuepress-plugin-a') + const pkg3 = resolveScopePackage('vuepress-plugin-mocked-a') expect(pkg3).toEqual({ "name": "", "org": "" }) }) }) const getBaseAsserts = (type: string) => [ - { input: 'a', output: ['a', `vuepress-${type}-a`] }, - { input: `vuepress-${type}-a`, output: ['a', `vuepress-${type}-a`] }, - { input: '@vuepress/a', output: ['@vuepress/a', `@vuepress/${type}-a`] }, - { input: '@org/a', output: ['@org/a', `@org/vuepress-${type}-a`] } + { input: 'a', output: ['a', `vuepress-${type}-mocked-a`] }, + { input: `vuepress-${type}-a`, output: ['a', `vuepress-${type}-mocked-a`] }, + { input: '@vuepress/a', output: ['@vuepress/a', `@vuepress/${type}-mocked-a`] }, + { input: '@org/a', output: ['@org/a', `@org/vuepress-${type}-mocked-a`] } ] describe('normalizeRequest', () => { @@ -87,7 +87,7 @@ describe('normalizeRequest', () => { const req = { name: 'a' } const { name, shortcut } = normalizeRequest(req) expect(shortcut).toBe('a') - expect(name).toBe(`vuepress-plugin-a`) + expect(name).toBe(`vuepress-plugin-mocked-a`) }) }) diff --git a/packages/@vuepress/shared-utils/src/moduleLoader.ts b/packages/@vuepress/shared-utils/src/moduleLoader.ts index b87d1875..3a32b9b2 100644 --- a/packages/@vuepress/shared-utils/src/moduleLoader.ts +++ b/packages/@vuepress/shared-utils/src/moduleLoader.ts @@ -38,14 +38,17 @@ const resolve = semver.satisfies(process.version, '>=10.0.0') ? require.resolve : resolveFallback +const MOCKED_REGEX = /^(@vuepress\/|(@[\w-]+\/)?vuepress-)(theme|plugin)-mock-[\w-]+/ + export function resolveModule (request: string, context: string): string { let resolvedPath // TODO // Temporary workaround for jest cannot resolve module path from '__mocks__' // when using 'require.resolve'. - if (env.isTest && request !== '@vuepress/theme-default') { + if (env.isTest && MOCKED_REGEX.test(request)) { resolvedPath = path.resolve(__dirname, '../../../../__mocks__', request) if (!fs.existsSync(`${resolvedPath}.js`) && !fs.existsSync(`${resolvedPath}/index.js`)) { + console.log(resolvedPath) throw new Error(`Cannot find module '${request}'`) } return resolvedPath