1

In my unit test, I get an Object and I can list the keys.
However, I can't get the value for a specific key:

wrapper = mount(Toolbar, { router, i18n });
const currentFlag= wrapper.find("#current-flag")
const currentFlagObject = currentFlag.attributes("src")
console.log(Object.keys(currentFlagObject))
console.log(currentFlagObject['src'])

console:

console.log tests/unit/Toolbar.spec.js:27
  [ 'id', 'src', 'width' ]
console.log tests/unit/Toolbar.spec.js:28
  [object Object]

Why can't i get the key value?

6
  • try console.log(currentFlagObject.src); Commented Aug 28, 2018 at 11:42
  • thanks but it does not work as the object is a JS object not a JSON one Commented Aug 28, 2018 at 11:43
  • 1
    JSON.stringify(currentFlagObject.src) Commented Aug 28, 2018 at 11:45
  • @JasminMistry currentFlagObject.src and currentFlagObject['src'] are equivalent. There must not be a different result for both of them. Commented Aug 28, 2018 at 11:46
  • 1
    JSON.stringify(currentFlagObject).src shouldn't work at all Commented Aug 28, 2018 at 11:49

2 Answers 2

1
console.log(JSON.stringify(obj))

This will print the stringify version of object. So instead of [object Object] as an output you will get the content of object.

in your case it will be

console.log(JSON.stringify(currentFlagObject['src']))
Sign up to request clarification or add additional context in comments.

1 Comment

yes thanks ... and I discover that the src is anobject too.... a flag file.... not the path...
0

Try DOM getAttribute() Method

 currentFlag.getAttribute("src");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.