use-mocked-name

This commit is contained in:
Shigma
2019-04-03 15:38:13 +08:00
parent 9fba54931c
commit e24866c754
20 changed files with 47 additions and 45 deletions
-3
View File
@@ -1,3 +0,0 @@
module.exports = {
extend: 'vuepress-theme-parent'
}
@@ -0,0 +1,3 @@
module.exports = {
extend: 'vuepress-theme-mocked-parent'
}
@@ -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)
@@ -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')
})
@@ -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`)
})
})
@@ -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