Skip to content
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

fix: throw error if stub contains circular reference #504

Merged
merged 2 commits into from
Mar 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"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:types": "tsc -p packages/test-utils/types && tsc -p packages/server-test-utils/types"
},
Expand Down
12 changes: 0 additions & 12 deletions packages/shared/CHANGELOG.md

This file was deleted.

8 changes: 8 additions & 0 deletions packages/shared/stub-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { compileToFunctions } from 'vue-template-compiler'
import { throwError } from './util'
import { componentNeedsCompiling } from './validators'
import { compileTemplate } from './compile-template'
import { capitalize, camelize, hyphenate } from './util'

function isVueComponent (comp) {
return comp && (comp.render || comp.template || comp.options)
Expand Down Expand Up @@ -43,6 +44,13 @@ function createStubFromString (templateString: string, originalComponent: Compon
if (!compileToFunctions) {
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
}

if (templateString.indexOf(hyphenate(originalComponent.name)) !== -1 ||
templateString.indexOf(capitalize(originalComponent.name)) !== -1 ||
templateString.indexOf(camelize(originalComponent.name)) !== -1) {
throwError('options.stub cannot contain a circular reference')
}

return {
...getCoreProperties(originalComponent),
...compileToFunctions(templateString)
Expand Down
2 changes: 2 additions & 0 deletions packages/test-utils/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const buble = require('rollup-plugin-buble')
const nodeResolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs')
const chalk = require('chalk')
const json = require('rollup-plugin-json')

function success (text) {
console.log(chalk.green(`${text} ✔`))
Expand Down Expand Up @@ -56,6 +57,7 @@ rollupOptions.forEach(options => {
external: ['vue', 'vue-template-compiler'],
plugins: [
flow(),
json(),
buble({
objectAssign: 'Object.assign'
}),
Expand Down
7 changes: 3 additions & 4 deletions test/specs/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import {
} from '~vue/test-utils'

describe('config', () => {
let configStubsSave
beforeEach(() => {
TransitionGroupStub.name = 'another-temp-name'
TransitionStub.name = 'a-temp-name'
configStubsSave = config.stubs
})

afterEach(() => {
TransitionGroupStub.name = 'transition-group'
TransitionStub.name = 'transition'
config.stubs = configStubsSave
})

it('stubs transition and transition-group by default', () => {
Expand Down Expand Up @@ -57,7 +60,6 @@ describe('config', () => {
})

it('doesn\'t stub transition when config.stubs is set to false', () => {
const configStubsSave = config.stubs
config.stubs = false
const testComponent = {
template: `
Expand All @@ -69,11 +71,9 @@ describe('config', () => {
const wrapper = mount(testComponent)
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
expect(wrapper.contains(TransitionStub)).to.equal(false)
config.stubs = configStubsSave
})

it('doesn\'t stub transition when config.stubs is set to a string', () => {
const configStubsSave = config.stubs
config.stubs = 'a string'
const testComponent = {
template: `
Expand All @@ -85,6 +85,5 @@ describe('config', () => {
const wrapper = mount(testComponent)
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
expect(wrapper.contains(TransitionStub)).to.equal(false)
config.stubs = configStubsSave
})
})
12 changes: 12 additions & 0 deletions test/specs/mounting-options/stubs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ describeWithMountingMethods('options.stub', (mountingMethod) => {
expect(HTML).contains('No render function')
})

const invalidValues = ['child-component', 'ChildComponent', 'childComponent']
invalidValues.forEach(invalidValue => {
it('throws an error when passed a circular reference', () => {
const error = '[vue-test-utils]: options.stub cannot contain a circular reference'
const fn = () => mountingMethod(ComponentWithChild, {
stubs: {
ChildComponent: `<${invalidValue} />`
}})
expect(fn).to.throw().with.property('message', error)
})
})

it('throws an error when passed an invalid value as stub', () => {
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
const invalidValues = [1, null, [], {}, NaN]
Expand Down