question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: attributes.item is not a function

See original GitHub issue

Subject of the issue

I have a component that contains an image (<img />) element. When I execute a test with the following code:

const img = wrapper.find('img') // img element exists for 100%
img.trigger('load') // <- here error happens / error log points for this particular line

Steps to reproduce

Create any Vue component that contains img element inside with binded @load="onImgLoad" method for img DOM element.

Create following test:

test('Should call onLoad after img load', async () => {
    const onImgLoad = jest.fn();
    const wrapper: Wrapper<any> = shallowMount(ImageComponent, {
      methods: {
        onImgLoad,
      },
    });

    const img = wrapper.find('img');
    await img.trigger('load');
    expect(onImgLoad).toHaveBeenCalledTimes(1);
  });

Expected behaviour

Test should pass just fine, load event should be triggered 1 time.

Actual behaviour

Test fails and throws an error:

TypeError: attributes.item is not a function

It also fails for other DOM elements (selected via wrapper.find()), when I try to use .trigger methods with other events on them.

Node version: v14.15.4

Vue-test-utils version: 1.0.0-beta.31

Using Vue 2 + Typescript

Possible Solution

I’ve updated the vue-test-utils.js file inside my node_modules folder (file url in the github repo: https://github.com/vuejs/vue-test-utils/blob/dev/packages/test-utils/src/wrapper.js )

In this method definition:

/**
 * Returns an Object containing all the attribute/value pairs on the element.
 */
attributes(key?: string): { [name: string]: string } | string {
  this.__warnIfDestroyed()

  const attributes = this.element.attributes
  const attributeMap = {}
  for (let i = 0; i < attributes.length; i++) {
    const att = attributes.item(i)
    attributeMap[att.localName] = att.value
  }

    return key ? attributeMap[key] : attributeMap
  }

I’ve replaced const att = attributes.item(i) to const att = attributes[i] and test started passing just fine.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
nikhilgosavi007commented, Sep 24, 2021

@lukaszkups Try using jsdom instead of @happy-dom/jest-environment as your testEnvironment. It worked for me 😃

1reaction
IGx89commented, Oct 7, 2022

This is the root cause: https://github.com/capricorn86/happy-dom/issues/308. Not fixed yet, but happy-dom has been having a lot of activity lately so maybe soon…

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: element.attr is not a function Jasmine
I have a simple function like this below which sets attributes to a given element then it returns the element with attributes.
Read more >
TypeError: getAttribute is not a function in JavaScript
Calling the getAttribute method on an object that is not a valid DOM element causes the error. To solve the "getAttribute is not...
Read more >
TypeError: "x" is not a function - JavaScript - MDN Web Docs
Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function?...
Read more >
JavaScript: Uncaught TypeError: n is not a function
This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to...
Read more >
Type Errors - Pyre
AttributeError: 'Child2' object has no attribute 'size'. To prevent such errors, Pyre raises a type error when violating contravariance: $ pyre
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found