Skip to content

Commit 5d892bb

Browse files
authored
Merge pull request #1396 from vuejs/feat/allow-testing-src-directly
Allow testing src directly
2 parents cff91c9 + 13e04b8 commit 5d892bb

40 files changed

+568
-481
lines changed

.github/CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ $ yarn test:unit
5959

6060
# run the full test suite, include linting / type checking
6161
$ yarn test
62+
63+
# run tests against src files only. Allows inline debugging.
64+
$ yarn test:unit:only:dev
6265
```
6366

6467
There are some other scripts available in the `scripts` section of the `package.json` file.

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@
1515
"flow": "flow check",
1616
"lint": "eslint --ext js,vue . --ignore-path .gitignore",
1717
"lint:docs": "eslint --ext js,vue,md docs --ignore-path .gitignore",
18-
"lint:fix": "npm run lint -- --fix",
18+
"lint:fix": "yarn lint -- --fix",
1919
"format": "prettier --write \"**/*.{js,json,vue,md}\"",
2020
"format:check": "prettier --check \"**/*.{js,json,vue,md}\"",
21-
"release": "npm run build && npm run test:unit:only && lerna publish --conventional-commits -m \"chore(release): publish %s\" --cd-version prerelease",
22-
"test": "npm run format:check && npm run lint && npm run lint:docs && npm run flow && npm run test:types && npm run test:unit && npm run test:unit:karma && npm run test:unit:node",
21+
"release": "yarn build && yarn test:unit:only && lerna publish --conventional-commits -m \"chore(release): publish %s\" --cd-version prerelease",
22+
"test": "yarn format:check && yarn lint && yarn lint:docs && yarn flow && yarn test:types && yarn test:unit && yarn test:unit:karma && yarn test:unit:node",
2323
"test:compat": "scripts/test-compat.sh",
24-
"test:unit": "npm run build:test && npm run test:unit:only",
24+
"test:unit": "yarn build:test && yarn test:unit:only",
2525
"test:unit:only": "mocha-webpack --webpack-config test/setup/webpack.test.config.js test/specs --recursive --require test/setup/mocha.setup.js",
26-
"test:unit:debug": "npm run build:test && node --inspect-brk node_modules/.bin/mocha-webpack --webpack-config test/setup/webpack.test.config.js test/specs --recursive --require test/setup/mocha.setup.js",
27-
"test:unit:karma": "npm run build:test && npm run test:unit:karma:only",
26+
"test:unit:only:dev": "cross-env TARGET=dev yarn test:unit:only",
27+
"test:unit:debug": "yarn build:test && node --inspect-brk node_modules/.bin/mocha-webpack --webpack-config test/setup/webpack.test.config.js test/specs --recursive --require test/setup/mocha.setup.js",
28+
"test:unit:karma": "yarn build:test && yarn test:unit:karma:only",
2829
"test:unit:karma:only": "cross-env TARGET=browser karma start test/setup/karma.conf.js --single-run",
29-
"test:unit:node": "npm run build:test && npm run test:unit:node:only",
30+
"test:unit:node": "yarn build:test && yarn test:unit:node:only",
3031
"test:unit:node:only": "cross-env TEST_ENV=node mocha-webpack --webpack-config test/setup/webpack.test.config.js test/specs/render.spec.js test/specs/renderToString.spec.js --require test/setup/mocha.setup.js",
3132
"test:types": "tsc -p packages/test-utils/types && tsc -p packages/server-test-utils/types"
3233
},
3334
"dependencies": {
3435
"babel-core": "^6.26.0",
3536
"babel-eslint": "^8.2.2",
37+
"babel-helper-vue-jsx-merge-props": "^2.0.3",
3638
"babel-loader": "^7.1.3",
3739
"babel-plugin-syntax-jsx": "^6.18.0",
3840
"babel-plugin-transform-decorators-legacy": "^1.3.4",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import testUtils from '@vue/test-utils'
1+
import * as testUtils from '@vue/test-utils'
22

33
export default testUtils.config

packages/server-test-utils/src/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@ import renderToString from './renderToString'
22
import render from './render'
33
import config from './config'
44

5-
export default {
6-
renderToString,
7-
config,
8-
render
9-
}
5+
export { renderToString, config, render }

packages/test-utils/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function shallow(component, options) {
1616
return shallowMount(component, options)
1717
}
1818

19-
export default {
19+
export {
2020
createLocalVue,
2121
createWrapper,
2222
config,

test/resources/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* global describe */
22

33
import Vue from 'vue'
4-
import { shallowMount, mount } from '~vue/test-utils'
5-
import { renderToString } from '~vue/server-test-utils'
4+
import { shallowMount, mount } from '@vue/test-utils'
5+
import { renderToString } from '@vue/server-test-utils'
66

77
export const vueVersion = Number(
88
`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`

test/setup/webpack.test.config.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,35 @@ const rules = [].concat(
1515
{
1616
test: /\.js$/,
1717
loader: 'babel-loader',
18-
exclude: /node_modules\/(?!(shared|create-instance)\/).*/
18+
exclude: /node_modules/
1919
}
2020
)
21-
21+
const externals = nodeExternals({
22+
// we need to whitelist both `create-instance` and files in `shared` package. Otherwise webpack won't bundle them in the test dev env
23+
whitelist: [
24+
'@vue/test-utils',
25+
'@vue/server-test-utils',
26+
'create-instance',
27+
/^shared\/.*/
28+
]
29+
})
30+
// define the default aliases
31+
let aliasedFiles = {}
32+
if (process.env.TARGET === 'dev') {
33+
// if we are in dev test mode, we want to alias all files to the src file, not dist
34+
aliasedFiles = {
35+
'@vue/server-test-utils': `@vue/server-test-utils/src/index.js`,
36+
'@vue/test-utils': `@vue/test-utils/src/index.js`
37+
}
38+
}
2239
module.exports = {
2340
module: {
2441
rules
2542
},
26-
externals: !browser ? [nodeExternals()] : undefined,
43+
externals: !browser ? [externals] : undefined,
2744
resolve: {
2845
alias: {
29-
'~vue/server-test-utils': `${projectRoot}/packages/server-test-utils/dist/vue-server-test-utils.js`,
30-
'~vue/test-utils': `${projectRoot}/packages/test-utils/dist/vue-test-utils.js`,
46+
...aliasedFiles,
3147
'~resources': `${projectRoot}/test/resources`
3248
}
3349
},

test/specs/components/RouterLink.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RouterLinkStub } from '~vue/test-utils'
1+
import { RouterLinkStub } from '@vue/test-utils'
22
import { describeWithShallowAndMount } from '~resources/utils'
33

44
describeWithShallowAndMount('RouterLinkStub', mountingMethod => {

test/specs/config.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describeWithShallowAndMount } from '~resources/utils'
22
import ComponentWithProps from '~resources/components/component-with-props.vue'
3-
import { config, createLocalVue } from '~vue/test-utils'
3+
import { config, createLocalVue } from '@vue/test-utils'
44

55
describeWithShallowAndMount('config', mountingMethod => {
66
const sandbox = sinon.createSandbox()

test/specs/create-local-vue.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
33
import VueRouter from 'vue-router'
4-
import { createLocalVue } from '~vue/test-utils'
4+
import { createLocalVue } from '@vue/test-utils'
55
import Component from '~resources/components/component.vue'
66
import ComponentWithVuex from '~resources/components/component-with-vuex.vue'
77
import ComponentWithRouter from '~resources/components/component-with-router.vue'

0 commit comments

Comments
 (0)