Skip to content

Commit 8f205c0

Browse files
authored
test: run renderToString tests in node environment (#542)
1 parent 8836071 commit 8f205c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+951
-790
lines changed

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
"lint:fix": "npm run lint -- --fix",
2020
"prepublish": "npm run build && npm run test:unit:only",
2121
"publish": "lerna publish --conventional-commits -m \"chore(release): publish %s\"",
22-
"test": "npm run lint && npm run lint:docs && npm run flow && npm run test:types && npm run test:unit && npm run test:unit:karma",
22+
"test": "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",
2323
"test:compat": "scripts/test-compat.sh",
2424
"test:unit": "npm run build:test && npm run test:unit:only",
25-
"test:unit:only": "cross-env BABEL_ENV=test && 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 && cross-env BABEL_ENV=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 && cross-env BABEL_ENV=test TARGET=browser karma start test/setup/karma.conf.js --single-run",
25+
"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 TARGET=browser karma start test/setup/karma.conf.js --single-run",
28+
"test:unit:node": "npm run build:test && npm run test:unit:node:only",
2829
"test:types": "tsc -p packages/test-utils/types && tsc -p packages/server-test-utils/types"
2930
},
3031
"devDependencies": {
@@ -37,6 +38,7 @@
3738
"babel-preset-flow-vue": "^1.0.0",
3839
"babel-preset-stage-2": "^6.24.1",
3940
"chai": "^4.0.0",
41+
"cheerio": "^1.0.0-rc.2",
4042
"cross-env": "^5.0.0",
4143
"css-loader": "^0.28.4",
4244
"eslint": "^4.18.1",

packages/create-instance/add-slots.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ function addSlotToVm (vm: Component, slotName: string, slotValue: Component | st
1010
if (!compileToFunctions) {
1111
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
1212
}
13+
if (typeof window === 'undefined') {
14+
throwError('the slots string option does not support strings in server-test-uitls.')
15+
}
1316
if (window.navigator.userAgent.match(/PhantomJS/i)) {
1417
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.')
1518
}

packages/server-test-utils/dist/vue-server-test-utils.js

Lines changed: 130 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/server-test-utils/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"rollup-plugin-node-resolve": "^3.0.3",
3838
"typescript": "^2.6.2"
3939
},
40+
"dependencies": {
41+
"@vue/test-utils": "1.0.0-beta.14"
42+
},
4043
"peerDependencies": {
4144
"vue": "2.x",
4245
"vue-server-renderer": "2.x",

packages/server-test-utils/scripts/build.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ const rollupOptions = process.env.NODE_ENV === 'test' ? rollupOptionsTest : roll
3535
rollupOptions.forEach(options => {
3636
rollup({
3737
input: resolve('src/index.js'),
38-
external: ['vue', 'vue-template-compiler', 'vue-server-renderer', 'cheerio'],
38+
external: [
39+
'vue',
40+
'vue-template-compiler',
41+
'vue-server-renderer',
42+
'cheerio',
43+
'@vue/test-utils'
44+
],
3945
plugins: [
4046
flow(),
4147
json(),

packages/test-utils/dist/vue-test-utils.js

Lines changed: 68 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/test-utils/src/matches-polyfill.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if (!Element.prototype.matches) {
1+
if (typeof Element !== 'undefined' && !Element.prototype.matches) {
22
Element.prototype.matches =
33
Element.prototype.matchesSelector ||
44
Element.prototype.mozMatchesSelector ||

packages/test-utils/src/mount.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
22

3-
import './warn-if-no-window'
43
import './matches-polyfill'
54
import './object-assign-polyfill'
65
import Vue from 'vue'
@@ -12,12 +11,14 @@ import errorHandler from './error-handler'
1211
import { findAllVueComponentsFromVm } from './find-vue-components'
1312
import { mergeOptions } from 'shared/merge-options'
1413
import config from './config'
14+
import warnIfNoWindow from './warn-if-no-window'
1515

1616
Vue.config.productionTip = false
1717
Vue.config.devtools = false
1818
Vue.config.errorHandler = errorHandler
1919

2020
export default function mount (component: Component, options: Options = {}): VueWrapper {
21+
warnIfNoWindow()
2122
// Remove cached constructor
2223
delete component._Ctor
2324
const vueClass = options.localVue || createLocalVue()
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { throwError } from 'shared/util'
22

3-
if (typeof window === 'undefined') {
4-
throwError(
5-
'window is undefined, vue-test-utils needs to be run in a browser environment.\n' +
6-
'You can run the tests in node using jsdom + jsdom-global.\n' +
7-
'See https://vue-test-utils.vuejs.org/en/guides/common-tips.html for more details.'
8-
)
3+
export default function warnIfNoWindow () {
4+
if (typeof window === 'undefined') {
5+
throwError(
6+
'window is undefined, vue-test-utils needs to be run in a browser environment.\n' +
7+
'You can run the tests in node using jsdom + jsdom-global.\n' +
8+
'See https://vue-test-utils.vuejs.org/en/guides/common-tips.html for more details.'
9+
)
10+
}
911
}

test/resources/utils.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { renderToString } from '~vue/server-test-utils'
66

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

9-
export const isRunningJSDOM = navigator.userAgent.includes && navigator.userAgent.includes('jsdom')
9+
export const isRunningJSDOM =
10+
typeof navigator !== 'undefined' &&
11+
navigator.userAgent.includes &&
12+
navigator.userAgent.includes('jsdom')
1013

1114
export function injectSupported () {
1215
return vueVersion > 2.2
@@ -24,15 +27,20 @@ export function functionalSFCsSupported () {
2427
return vueVersion >= 2.5
2528
}
2629

27-
const shallowAndMount = [mount, shallow]
28-
const shallowMountAndRender = isRunningJSDOM
29-
? [mount, shallow, renderToString]
30+
const shallowAndMount = process.env.TEST_ENV === 'node'
31+
? []
32+
: [mount, shallow]
33+
console.log(shallowAndMount)
34+
const shallowMountAndRender = process.env.TEST_ENV === 'node'
35+
? [renderToString]
3036
: [mount, shallow]
3137

3238
export function describeWithShallowAndMount (spec, cb) {
33-
shallowAndMount.forEach(method => {
34-
describe(`${spec} with ${method.name}`, () => cb(method))
35-
})
39+
if (shallowAndMount.length > 0) {
40+
shallowAndMount.forEach(method => {
41+
describe(`${spec} with ${method.name}`, () => cb(method))
42+
})
43+
}
3644
}
3745

3846
describeWithShallowAndMount.skip = function (spec, cb) {
@@ -80,3 +88,9 @@ export function itDoNotRunIf (predicate, spec, cb) {
8088
it(spec, cb)
8189
}
8290
}
91+
92+
export function describeIf (predicate, spec, cb) {
93+
if (predicate) {
94+
describe(spec, cb)
95+
}
96+
}

0 commit comments

Comments
 (0)