Skip to content

refactor: run renderToString tests in node environment #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
"lint:fix": "npm run lint -- --fix",
"prepublish": "npm run build && npm run test:unit:only",
"publish": "lerna publish --conventional-commits -m \"chore(release): publish %s\"",
"test": "npm run lint && npm run lint:docs && npm run flow && npm run test:types && npm run test:unit && npm run test:unit:karma",
"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",
"test:compat": "scripts/test-compat.sh",
"test:unit": "npm run build:test && npm run test:unit:only",
"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",
"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",
"test:unit:karma": "npm run build:test && cross-env BABEL_ENV=test TARGET=browser karma start test/setup/karma.conf.js --single-run",
"test:unit:only": "mocha-webpack --webpack-config test/setup/webpack.test.config.js test/specs --recursive --require test/setup/mocha.setup.js",
"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",
"test:unit:karma": "npm run build:test TARGET=browser karma start test/setup/karma.conf.js --single-run",
"test:unit:node": "npm run build:test && npm run test:unit:node:only",
"test:types": "tsc -p packages/test-utils/types && tsc -p packages/server-test-utils/types"
},
"devDependencies": {
Expand All @@ -37,6 +38,7 @@
"babel-preset-flow-vue": "^1.0.0",
"babel-preset-stage-2": "^6.24.1",
"chai": "^4.0.0",
"cheerio": "^1.0.0-rc.2",
"cross-env": "^5.0.0",
"css-loader": "^0.28.4",
"eslint": "^4.18.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/create-instance/add-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function addSlotToVm (vm: Component, slotName: string, slotValue: Component | st
if (!compileToFunctions) {
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
}
if (typeof window === 'undefined') {
throwError('the slots string option does not support strings in server-test-uitls.')
}
if (window.navigator.userAgent.match(/PhantomJS/i)) {
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.')
}
Expand Down
157 changes: 130 additions & 27 deletions packages/server-test-utils/dist/vue-server-test-utils.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/server-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"rollup-plugin-node-resolve": "^3.0.3",
"typescript": "^2.6.2"
},
"dependencies": {
"@vue/test-utils": "1.0.0-beta.14"
},
"peerDependencies": {
"vue": "2.x",
"vue-server-renderer": "2.x",
Expand Down
8 changes: 7 additions & 1 deletion packages/server-test-utils/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ const rollupOptions = process.env.NODE_ENV === 'test' ? rollupOptionsTest : roll
rollupOptions.forEach(options => {
rollup({
input: resolve('src/index.js'),
external: ['vue', 'vue-template-compiler', 'vue-server-renderer', 'cheerio'],
external: [
'vue',
'vue-template-compiler',
'vue-server-renderer',
'cheerio',
'@vue/test-utils'
],
plugins: [
flow(),
json(),
Expand Down
84 changes: 68 additions & 16 deletions packages/test-utils/dist/vue-test-utils.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/test-utils/src/matches-polyfill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if (!Element.prototype.matches) {
if (typeof Element !== 'undefined' && !Element.prototype.matches) {
Element.prototype.matches =
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Expand Down
3 changes: 2 additions & 1 deletion packages/test-utils/src/mount.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow

import './warn-if-no-window'
import './matches-polyfill'
import './object-assign-polyfill'
import Vue from 'vue'
Expand All @@ -12,12 +11,14 @@ import errorHandler from './error-handler'
import { findAllVueComponentsFromVm } from './find-vue-components'
import { mergeOptions } from 'shared/merge-options'
import config from './config'
import warnIfNoWindow from './warn-if-no-window'

Vue.config.productionTip = false
Vue.config.devtools = false
Vue.config.errorHandler = errorHandler

export default function mount (component: Component, options: Options = {}): VueWrapper {
warnIfNoWindow()
// Remove cached constructor
delete component._Ctor
const vueClass = options.localVue || createLocalVue()
Expand Down
14 changes: 8 additions & 6 deletions packages/test-utils/src/warn-if-no-window.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { throwError } from 'shared/util'

if (typeof window === 'undefined') {
throwError(
'window is undefined, vue-test-utils needs to be run in a browser environment.\n' +
'You can run the tests in node using jsdom + jsdom-global.\n' +
'See https://vue-test-utils.vuejs.org/en/guides/common-tips.html for more details.'
)
export default function warnIfNoWindow () {
if (typeof window === 'undefined') {
throwError(
'window is undefined, vue-test-utils needs to be run in a browser environment.\n' +
'You can run the tests in node using jsdom + jsdom-global.\n' +
'See https://vue-test-utils.vuejs.org/en/guides/common-tips.html for more details.'
)
}
}
28 changes: 21 additions & 7 deletions test/resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { renderToString } from '~vue/server-test-utils'

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

export const isRunningJSDOM = navigator.userAgent.includes && navigator.userAgent.includes('jsdom')
export const isRunningJSDOM =
typeof navigator !== 'undefined' &&
navigator.userAgent.includes &&
navigator.userAgent.includes('jsdom')

export function injectSupported () {
return vueVersion > 2.2
Expand All @@ -24,15 +27,20 @@ export function functionalSFCsSupported () {
return vueVersion >= 2.5
}

const shallowAndMount = [mount, shallow]
const shallowMountAndRender = isRunningJSDOM
? [mount, shallow, renderToString]
const shallowAndMount = process.env.TEST_ENV === 'node'
? []
: [mount, shallow]
console.log(shallowAndMount)
const shallowMountAndRender = process.env.TEST_ENV === 'node'
? [renderToString]
: [mount, shallow]

export function describeWithShallowAndMount (spec, cb) {
shallowAndMount.forEach(method => {
describe(`${spec} with ${method.name}`, () => cb(method))
})
if (shallowAndMount.length > 0) {
shallowAndMount.forEach(method => {
describe(`${spec} with ${method.name}`, () => cb(method))
})
}
}

describeWithShallowAndMount.skip = function (spec, cb) {
Expand Down Expand Up @@ -80,3 +88,9 @@ export function itDoNotRunIf (predicate, spec, cb) {
it(spec, cb)
}
}

export function describeIf (predicate, spec, cb) {
if (predicate) {
describe(spec, cb)
}
}
4 changes: 3 additions & 1 deletion test/setup/mocha.setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require('jsdom-global')()
if (process.env.TEST_ENV !== 'node') {
require('jsdom-global')()
}

const chai = require('chai')
const sinon = require('sinon')
Expand Down
13 changes: 6 additions & 7 deletions test/setup/webpack.test.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
const nodeExternals = require('webpack-node-externals')
const webpack = require('webpack')
const browser = process.env.TARGET === 'browser'
const path = require('path')

const projectRoot = path.resolve(__dirname, '../../')
const isCoverage = process.env.NODE_ENV === 'coverage'

const rules = [].concat(
isCoverage ? {
test: /\.js/,
include: path.resolve('dist'),
loader: 'istanbul-instrumenter-loader'
} : [],
{
test: /\.vue$/,
loader: 'vue-loader'
Expand Down Expand Up @@ -41,5 +37,8 @@ module.exports = {
node: {
fs: 'empty',
module: 'empty'
}
},
plugins: [
new webpack.EnvironmentPlugin(['TEST_ENV'])
]
}
10 changes: 0 additions & 10 deletions test/specs/add-attrs.spec.js

This file was deleted.

10 changes: 0 additions & 10 deletions test/specs/add-listeners.spec.js

This file was deleted.

9 changes: 5 additions & 4 deletions test/specs/components/RouterLink.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mount, RouterLinkStub } from '~vue/test-utils'
import { RouterLinkStub } from '~vue/test-utils'
import { describeWithShallowAndMount } from '~resources/utils'

describe('RouterLinkStub', () => {
describeWithShallowAndMount('RouterLinkStub', (mountingMethod) => {
it('takes correct props', () => {
const TestComponent = {
template: `
Expand All @@ -18,7 +19,7 @@ describe('RouterLinkStub', () => {
</div>
`
}
const wrapper = mount(TestComponent, {
const wrapper = mountingMethod(TestComponent, {
stubs: {
RouterLink: RouterLinkStub
}
Expand All @@ -42,7 +43,7 @@ describe('RouterLinkStub', () => {
</div>
`
}
const wrapper = mount(TestComponent, {
const wrapper = mountingMethod(TestComponent, {
stubs: {
RouterLink: RouterLinkStub
}
Expand Down
9 changes: 5 additions & 4 deletions test/specs/components/TransitionGroupStub.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ComponentWithTransitionGroup from '~resources/components/component-with-transition-group.vue'
import { mount, TransitionGroupStub } from '~vue/test-utils'
import { TransitionGroupStub } from '~vue/test-utils'
import { describeWithShallowAndMount } from '~resources/utils'

describe('TransitionGroupStub', () => {
describeWithShallowAndMount('TransitionGroupStub', (mountingMethod) => {
it('update synchronously when used as stubs for Transition', () => {
const wrapper = mount(ComponentWithTransitionGroup, {
const wrapper = mountingMethod(ComponentWithTransitionGroup, {
stubs: {
'transition-group': TransitionGroupStub
}
Expand Down Expand Up @@ -34,7 +35,7 @@ describe('TransitionGroupStub', () => {
</transition-group>
`
}
const wrapper = mount(TestComponent, {
const wrapper = mountingMethod(TestComponent, {
stubs: {
'transition-group': TransitionGroupStub
}
Expand Down
42 changes: 19 additions & 23 deletions test/specs/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {
mount,
config,
TransitionStub,
TransitionGroupStub,
createLocalVue
} from '~vue/test-utils'

describe('config', () => {
import { describeWithShallowAndMount, itDoNotRunIf } from '~resources/utils'
import { config, TransitionStub, TransitionGroupStub, createLocalVue } from '~vue/test-utils'

describeWithShallowAndMount('config', (mountingMethod) => {
let configStubsSave
beforeEach(() => {
TransitionGroupStub.name = 'another-temp-name'
Expand All @@ -20,19 +15,20 @@ describe('config', () => {
config.stubs = configStubsSave
})

it('stubs transition and transition-group by default', () => {
const testComponent = {
template: `
itDoNotRunIf(mountingMethod.name === 'shallow',
'stubs transition and transition-group by default', () => {
const testComponent = {
template: `
<div>
<transition><p /></transition>
<transition-group><p /><p /></transition-group>
</div>
`
}
const wrapper = mount(testComponent)
expect(wrapper.contains(TransitionStub)).to.equal(true)
expect(wrapper.contains(TransitionGroupStub)).to.equal(true)
})
}
const wrapper = mountingMethod(testComponent)
expect(wrapper.contains(TransitionStub)).to.equal(true)
expect(wrapper.contains(TransitionGroupStub)).to.equal(true)
})

it('mocks a global variable', () => {
const localVue = createLocalVue()
Expand All @@ -47,7 +43,7 @@ describe('config', () => {

config.mocks['$t'] = 'mock value'

const wrapper = mount(testComponent, {
const wrapper = mountingMethod(testComponent, {
localVue, t
})

Expand All @@ -66,7 +62,7 @@ describe('config', () => {

config.methods['val'] = () => 'method'

const wrapper = mount(testComponent)
const wrapper = mountingMethod(testComponent)

expect(wrapper.vm.val()).to.equal('method')
expect(wrapper.text()).to.equal('method')
Expand All @@ -81,7 +77,7 @@ describe('config', () => {
`
}
config.stubs.transition = false
const wrapper = mount(testComponent)
const wrapper = mountingMethod(testComponent)
expect(wrapper.contains(TransitionStub)).to.equal(false)
})

Expand All @@ -94,7 +90,7 @@ describe('config', () => {
`
}
config.stubs['transition-group'] = false
const wrapper = mount(testComponent)
const wrapper = mountingMethod(testComponent)
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
})

Expand All @@ -107,7 +103,7 @@ describe('config', () => {
</div>
`
}
const wrapper = mount(testComponent)
const wrapper = mountingMethod(testComponent)
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
expect(wrapper.contains(TransitionStub)).to.equal(false)
})
Expand All @@ -121,7 +117,7 @@ describe('config', () => {
</div>
`
}
const wrapper = mount(testComponent)
const wrapper = mountingMethod(testComponent)
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
expect(wrapper.contains(TransitionStub)).to.equal(false)
})
Expand Down
Loading