Skip to content

Commit 881588a

Browse files
authoredMar 31, 2018
fix: throw error if stub contains circular reference (#504)
1 parent 3e50827 commit 881588a

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed
 

Diff for: ‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"test:compat": "scripts/test-compat.sh",
2424
"test:unit": "npm run build:test && npm run test:unit:only",
2525
"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",
2627
"test:unit:karma": "npm run build:test && cross-env BABEL_ENV=test TARGET=browser karma start test/setup/karma.conf.js --single-run",
2728
"test:types": "tsc -p packages/test-utils/types && tsc -p packages/server-test-utils/types"
2829
},

Diff for: ‎packages/shared/CHANGELOG.md

-12
This file was deleted.

Diff for: ‎packages/shared/stub-components.js

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { compileToFunctions } from 'vue-template-compiler'
55
import { throwError } from './util'
66
import { componentNeedsCompiling } from './validators'
77
import { compileTemplate } from './compile-template'
8+
import { capitalize, camelize, hyphenate } from './util'
89

910
function isVueComponent (comp) {
1011
return comp && (comp.render || comp.template || comp.options)
@@ -43,6 +44,13 @@ function createStubFromString (templateString: string, originalComponent: Compon
4344
if (!compileToFunctions) {
4445
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
4546
}
47+
48+
if (templateString.indexOf(hyphenate(originalComponent.name)) !== -1 ||
49+
templateString.indexOf(capitalize(originalComponent.name)) !== -1 ||
50+
templateString.indexOf(camelize(originalComponent.name)) !== -1) {
51+
throwError('options.stub cannot contain a circular reference')
52+
}
53+
4654
return {
4755
...getCoreProperties(originalComponent),
4856
...compileToFunctions(templateString)

Diff for: ‎packages/test-utils/scripts/build.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const buble = require('rollup-plugin-buble')
55
const nodeResolve = require('rollup-plugin-node-resolve')
66
const commonjs = require('rollup-plugin-commonjs')
77
const chalk = require('chalk')
8+
const json = require('rollup-plugin-json')
89

910
function success (text) {
1011
console.log(chalk.green(`${text} ✔`))
@@ -56,6 +57,7 @@ rollupOptions.forEach(options => {
5657
external: ['vue', 'vue-template-compiler'],
5758
plugins: [
5859
flow(),
60+
json(),
5961
buble({
6062
objectAssign: 'Object.assign'
6163
}),

Diff for: ‎test/specs/config.spec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import {
66
} from '~vue/test-utils'
77

88
describe('config', () => {
9+
let configStubsSave
910
beforeEach(() => {
1011
TransitionGroupStub.name = 'another-temp-name'
1112
TransitionStub.name = 'a-temp-name'
13+
configStubsSave = config.stubs
1214
})
1315

1416
afterEach(() => {
1517
TransitionGroupStub.name = 'transition-group'
1618
TransitionStub.name = 'transition'
19+
config.stubs = configStubsSave
1720
})
1821

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

5962
it('doesn\'t stub transition when config.stubs is set to false', () => {
60-
const configStubsSave = config.stubs
6163
config.stubs = false
6264
const testComponent = {
6365
template: `
@@ -69,11 +71,9 @@ describe('config', () => {
6971
const wrapper = mount(testComponent)
7072
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
7173
expect(wrapper.contains(TransitionStub)).to.equal(false)
72-
config.stubs = configStubsSave
7374
})
7475

7576
it('doesn\'t stub transition when config.stubs is set to a string', () => {
76-
const configStubsSave = config.stubs
7777
config.stubs = 'a string'
7878
const testComponent = {
7979
template: `
@@ -85,6 +85,5 @@ describe('config', () => {
8585
const wrapper = mount(testComponent)
8686
expect(wrapper.contains(TransitionGroupStub)).to.equal(false)
8787
expect(wrapper.contains(TransitionStub)).to.equal(false)
88-
config.stubs = configStubsSave
8988
})
9089
})

Diff for: ‎test/specs/mounting-options/stubs.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,18 @@ describeWithMountingMethods('options.stub', (mountingMethod) => {
299299
expect(HTML).contains('No render function')
300300
})
301301

302+
const invalidValues = ['child-component', 'ChildComponent', 'childComponent']
303+
invalidValues.forEach(invalidValue => {
304+
it('throws an error when passed a circular reference', () => {
305+
const error = '[vue-test-utils]: options.stub cannot contain a circular reference'
306+
const fn = () => mountingMethod(ComponentWithChild, {
307+
stubs: {
308+
ChildComponent: `<${invalidValue} />`
309+
}})
310+
expect(fn).to.throw().with.property('message', error)
311+
})
312+
})
313+
302314
it('throws an error when passed an invalid value as stub', () => {
303315
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
304316
const invalidValues = [1, null, [], {}, NaN]

0 commit comments

Comments
 (0)
Please sign in to comment.