Skip to content

Commit eca8a61

Browse files
committed
test: init @vuepress/test-utils and migrate all tests
1 parent d708d33 commit eca8a61

File tree

37 files changed

+227
-224
lines changed

37 files changed

+227
-224
lines changed

Diff for: package.json

-9
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,13 @@
4040
]
4141
},
4242
"devDependencies": {
43-
"@vue/test-utils": "^1.0.0-beta.16",
44-
"@babel/core": "^7.0.0-beta.47",
45-
"@babel/preset-env": "^7.0.0-beta.47",
46-
"babel-core": "^7.0.0-0",
47-
"babel-jest": "^23.0.0",
4843
"conventional-changelog-cli": "^1.3.22",
4944
"eslint": "^4.19.1",
5045
"eslint-plugin-jest": "^21.15.1",
5146
"eslint-plugin-vue-libs": "^3.0.0",
52-
"execa": "^0.10.0",
53-
"jest": "^23.0.0",
54-
"jest-serializer-vue": "^1.0.0",
5547
"lerna": "^2.11.0",
5648
"lint-staged": "^7.0.4",
5749
"minimist": "^1.2.0",
58-
"vue-jest": "^2.6.0",
5950
"yorkie": "^1.0.3"
6051
},
6152
"engines": {

Diff for: test/app/Content.spec.js renamed to packages/@vuepress/core/__test__/app/Content.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import Content from '@/app/components/Content'
1+
import Content from '../../lib/app/components/Content'
22
import { mount } from '@vue/test-utils'
3-
import { getRouter, modeTestRunner } from '../util'
3+
import modeTestRunner from '@vuepress/test-utils/modeTestRunner'
4+
import getRouter from '@vuepress/test-utils/getRouter'
45

56
function test (mode, localVue) {
67
it(`${mode} - add custom class by default.`, () => {

Diff for: test/prepare/util.spec.js renamed to packages/@vuepress/core/__test__/prepare/util.spec.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
isIndexFile,
3-
fileToPath
4-
} from '@/prepare/util.js'
1+
import { isIndexFile, fileToPath } from '../../lib/prepare/util.js'
52

63
describe('prepare - util', () => {
74
test('isIndexFile', () => {

Diff for: test/util/parseHeaders.spec.js renamed to packages/@vuepress/core/__test__/util/parseHeaders.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
parseHeaders,
33
removeNonCodeWrappedHTML,
44
deeplyParseHeaders
5-
} from '@/util/parseHeaders'
5+
} from '../../lib/util/parseHeaders'
66

77
describe('parseHeaders', () => {
88
test('should unescape html', () => {

Diff for: packages/@vuepress/core/lib/app/app.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { routes } from '@internal/routes'
66
import { siteData } from '@internal/siteData'
77
import appEnhancers from '@internal/app-enhancers'
88
import globalUIComponents from '@internal/global-ui'
9+
import I18n from '@internal/i18n'
910

1011
// generated from user config
1112
import('@temp/style.styl')
@@ -31,7 +32,7 @@ if (module.hot) {
3132
Vue.config.productionTip = false
3233
Vue.use(Router)
3334
// mixin for exposing $site and $page
34-
Vue.mixin(dataMixin(siteData))
35+
Vue.mixin(dataMixin(I18n, siteData))
3536
// component for rendering markdown content and setting title etc.
3637
Vue.component('Content', Content)
3738
Vue.component('OutboundLink', OutboundLink)

Diff for: packages/@vuepress/core/lib/app/dataMixin.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* global VUEPRESS_TEMP_PATH */
22

33
import Vue from 'vue'
4-
import I18n from '@internal/i18n'
54

6-
export default function dataMixin (siteData) {
5+
export default function dataMixin (I18n, siteData) {
76
prepare(siteData)
87
const store = new Vue({
98
data: {

Diff for: packages/@vuepress/test-utils/.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__tests__
2+
__mocks__

Diff for: packages/@vuepress/test-utils/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @vuepress/test-utils
2+
3+
> test-utils for vuepress

Diff for: packages/@vuepress/test-utils/createJestConfig.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const defaultJestConfig = require('./jest/jest.config')
2+
3+
module.exports = function createJestConfig (override) {
4+
return Object.assign({}, defaultJestConfig, override)
5+
}

Diff for: packages/@vuepress/test-utils/createJestRunner.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const execa = require('execa')
2+
const rawArgs = process.argv.slice(2)
3+
4+
module.exports = function createJestRunner (jestArgs) {
5+
return async function () {
6+
console.log(`running jest with args: ${jestArgs.join(' ')}`)
7+
await execa('jest', [
8+
...jestArgs,
9+
...rawArgs
10+
], {
11+
stdio: 'inherit'
12+
})
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
dest: 'vuepress',
3+
locales: {
4+
'/': {
5+
lang: 'en-US',
6+
title: 'VuePress',
7+
description: 'Vue-powered Static Site Generator'
8+
},
9+
'/zh/': {
10+
lang: 'zh-CN',
11+
title: 'VuePress',
12+
description: 'Vue 驱动的静态网站生成器'
13+
}
14+
},
15+
themeConfig: {
16+
repo: 'vuejs/vuepress',
17+
editLinks: true,
18+
docsDir: 'docs',
19+
locales: {
20+
'/': {
21+
label: 'English',
22+
selectText: 'Languages',
23+
editLinkText: 'Edit this page on GitHub',
24+
lastUpdated: 'Last Updated'
25+
},
26+
'/zh/': {
27+
label: '简体中文',
28+
selectText: '选择语言',
29+
editLinkText: '在 GitHub 上编辑此页',
30+
lastUpdated: '上次更新',
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Hello, VuePress!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 你好, VuePress!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Hello, VuePress!

Diff for: packages/@vuepress/test-utils/getRouter.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Router = require('vue-router')
2+
3+
module.exports = function () {
4+
return new Router()
5+
}

Diff for: packages/@vuepress/test-utils/jest/jest.config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://github.com/facebook/jest/tree/master/packages/babel-jest
2+
// TODO remove 'babel-core@^7.0.0-0' when babel-jest can work with '@babel/core'
3+
4+
const path = require('path')
5+
6+
module.exports = {
7+
rootDir: path.resolve(__dirname, '..'),
8+
verbose: true,
9+
testURL: 'http://localhost/',
10+
moduleFileExtensions: [
11+
'js',
12+
'vue'
13+
],
14+
testPathIgnorePatterns: [
15+
'test.js',
16+
path.resolve(__dirname, '../test')
17+
],
18+
moduleNameMapper: {
19+
'^@/(.*)$': '<rootDir>/$1'
20+
},
21+
transform: {
22+
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
23+
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
24+
},
25+
snapshotSerializers: [
26+
'<rootDir>/node_modules/jest-serializer-vue'
27+
]
28+
}

Diff for: packages/@vuepress/test-utils/meta.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { fs } = require('@vuepress/shared-utils')
2+
const path = require('path')
3+
4+
const docsBaseDir = path.resolve(__dirname, 'fixtures')
5+
const docsModeNames = fs.readdirSync(docsBaseDir)
6+
const docsModes = docsModeNames.map(name => {
7+
const docsPath = path.resolve(docsBaseDir, name)
8+
const docsTempPath = path.resolve(docsPath, '.vuepress/.temp')
9+
return { name, docsPath, docsTempPath }
10+
})
11+
12+
exports.docsModes = docsModes

Diff for: test/hoc.js renamed to packages/@vuepress/test-utils/mockComponent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Component = {
1010
// When the child component is a pure presentation component,
1111
// we want to be able to display a sub-component with minimal info,
1212
// rather than stubbing it directly.
13-
export function mockComponent (name) {
13+
module.exports = function mockComponent (name) {
1414
return {
1515
render (h) {
1616
return h(Component, { props: { name }})

Diff for: packages/@vuepress/test-utils/modeTestRunner.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { createLocalVue } = require('@vue/test-utils')
2+
const dataMixin = require('@vuepress/core/lib/app/dataMixin').default
3+
const I18n = require('@vuepress/core/lib/prepare/I18n')
4+
const Router = require('vue-router')
5+
const mockComponent = require('./mockComponent')
6+
const { docsModes } = require('./meta')
7+
8+
function getLocalVueByMode (mode) {
9+
const localVue = createLocalVue()
10+
localVue.use(Router)
11+
12+
// register global component
13+
localVue.component('OutboundLink', mockComponent('outbound-link'))
14+
15+
const { siteData } = require(`${mode.docsTempPath}/internal/siteData.js`)
16+
localVue.component(siteData.pages[0].key, mockComponent('page-component'))
17+
localVue.mixin(dataMixin(I18n, siteData))
18+
return localVue
19+
}
20+
21+
/**
22+
* Used to test components in VuePress at different mode.
23+
* Since VuePress has two main modes, i18n or normal mode, we need to make
24+
* sure that the core Vue components in VuePress work well in both modes.
25+
* @param description
26+
* @param testFn
27+
*/
28+
module.exports = function modeTestRunner (description, testFn) {
29+
docsModes.forEach(mode => {
30+
describe(description, () => {
31+
testFn(mode.name, getLocalVueByMode(mode))
32+
})
33+
})
34+
}

Diff for: packages/@vuepress/test-utils/package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@vuepress/test-utils",
3+
"version": "1.0.0",
4+
"description": "test-utils for vuepress",
5+
"publishConfig": {
6+
"access": "public"
7+
},
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/vuejs/vuepress.git"
11+
},
12+
"keywords": [
13+
"documentation",
14+
"vue",
15+
"vuepress",
16+
"generator"
17+
],
18+
"dependencies": {
19+
"@vuepress/core": "^1.0.0",
20+
"@vuepress/shared-utils": "^1.0.0",
21+
"@vue/test-utils": "^1.0.0-beta.16",
22+
"@babel/core": "^7.0.0-beta.47",
23+
"@babel/preset-env": "^7.0.0-beta.47",
24+
"babel-core": "^7.0.0-0",
25+
"babel-jest": "^23.0.0",
26+
"jest": "^23.0.0",
27+
"jest-serializer-vue": "^1.0.0",
28+
"vue-jest": "^2.6.0",
29+
"execa": "^0.10.0"
30+
},
31+
"author": "Evan You",
32+
"license": "MIT",
33+
"bugs": {
34+
"url": "https://github.com/vuejs/vuepress/issues"
35+
},
36+
"homepage": "https://github.com/vuejs/vuepress/packages/@vuepress/test-utils#readme"
37+
}

Diff for: packages/@vuepress/test-utils/prepare.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { fs, logger } = require('@vuepress/shared-utils')
2+
const prepare = require('@vuepress/core/lib/prepare')
3+
const { docsModes } = require('./meta')
4+
5+
async function prepareForTest () {
6+
await Promise.all(docsModes.map(async ({ docsPath, docsTempPath }) => {
7+
await fs.ensureDir(docsTempPath)
8+
await prepare(docsPath, { theme: 'default', temp: docsTempPath })
9+
}))
10+
}
11+
12+
module.exports = function globalSetup () {
13+
logger.wait('Start preparing for testing ...')
14+
return prepareForTest().then(() => {
15+
logger.wait('Finished preparing for testing ...')
16+
})
17+
}

Diff for: test/default-theme/DropdownLink.spec.js renamed to packages/@vuepress/theme-default/__test__/components/DropdownLink.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mount, RouterLinkStub } from '@vue/test-utils'
2-
import DropdownLink from '@/default-theme/DropdownLink.vue'
3-
import { modeTestRunner } from '../util'
2+
import DropdownLink from '../../src/DropdownLink.vue'
3+
import modeTestRunner from '@vuepress/test-utils/modeTestRunner'
44

55
function test (mode, localVue) {
66
it(`$${mode} - renders dropdown link.`, () => {

Diff for: test/default-theme/NavLink.spec.js renamed to packages/@vuepress/theme-default/__test__/components/NavLink.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mount, RouterLinkStub } from '@vue/test-utils'
2-
import { modeTestRunner } from '../util'
3-
import NavLink from '@/default-theme/NavLink.vue'
2+
import modeTestRunner from '@vuepress/test-utils/modeTestRunner'
3+
import NavLink from '../../src/NavLink.vue'
44

55
function test (mode, localVue) {
66
it(`$${mode} - renders nav link with internal link`, () => {

Diff for: test/default-theme/__snapshots__/DropdownLink.spec.js.snap renamed to packages/@vuepress/theme-default/__test__/components/__snapshots__/DropdownLink.spec.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`DropdownLink $i18n - renders dropdown link. 1`] = `
3+
exports[`DropdownLink $docs-i18n - renders dropdown link. 1`] = `
44
<div class="dropdown-wrapper">
55
<a class="dropdown-title"><span class="title">VuePress</span> <span class="arrow right"></span></a>
66
<ul class="nav-dropdown" style="display: none;" name="dropdown">
@@ -16,7 +16,7 @@ exports[`DropdownLink $i18n - renders dropdown link. 1`] = `
1616
</div>
1717
`;
1818

19-
exports[`DropdownLink $simple - renders dropdown link. 1`] = `
19+
exports[`DropdownLink $docs-simple - renders dropdown link. 1`] = `
2020
<div class="dropdown-wrapper">
2121
<a class="dropdown-title"><span class="title">VuePress</span> <span class="arrow right"></span></a>
2222
<ul class="nav-dropdown" style="display: none;" name="dropdown">
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`NavLink $i18n - renders nav link with external link 1`] = `
3+
exports[`NavLink $docs-i18n - renders nav link with external link 1`] = `
44
<a href="http://vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
55
Vue
66
<p class="component outbound-link">outbound-link</p>
77
</a>
88
`;
99

10-
exports[`NavLink $i18n - renders nav link with internal link 1`] = `<a class="nav-link">VuePress</a>`;
10+
exports[`NavLink $docs-i18n - renders nav link with internal link 1`] = `<a class="nav-link">VuePress</a>`;
1111

12-
exports[`NavLink $simple - renders nav link with external link 1`] = `
12+
exports[`NavLink $docs-simple - renders nav link with external link 1`] = `
1313
<a href="http://vuejs.org/" target="_blank" rel="noopener noreferrer" class="nav-link external">
1414
Vue
1515
<p class="component outbound-link">outbound-link</p>
1616
</a>
1717
`;
1818

19-
exports[`NavLink $simple - renders nav link with internal link 1`] = `<a class="nav-link">VuePress</a>`;
19+
exports[`NavLink $docs-simple - renders nav link with internal link 1`] = `<a class="nav-link">VuePress</a>`;

Diff for: scripts/jest.config.js

+6-23
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
// https://github.com/facebook/jest/tree/master/packages/babel-jest
2-
// TODO remove 'babel-core@^7.0.0-0' when babel-jest can work with '@babel/core'
3-
41
const path = require('path')
2+
const createJestConfig = require('@vuepress/test-utils/createJestConfig')
53

6-
module.exports = {
4+
module.exports = createJestConfig({
75
rootDir: path.resolve(__dirname, '..'),
8-
verbose: true,
9-
testURL: 'http://localhost/',
10-
moduleFileExtensions: [
11-
'js',
12-
'vue'
13-
],
14-
testPathIgnorePatterns: [
15-
'test.js',
16-
path.resolve(__dirname, '../test')
17-
],
186
moduleNameMapper: {
19-
'^@/(.*)$': '<rootDir>/$1'
20-
},
21-
transform: {
22-
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
23-
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
7+
'^@/(.*)$': '<rootDir>/$1',
8+
'^@core/(.*)$': '<rootDir>/packages/@vuepress/core/$1'
249
},
25-
snapshotSerializers: [
26-
'<rootDir>/node_modules/jest-serializer-vue'
27-
]
28-
}
10+
globalSetup: '<rootDir>/packages/@vuepress/test-utils/prepare.js'
11+
})

0 commit comments

Comments
 (0)