Ramin Mousavi
Ramin Mousavi

Reputation: 616

Extend Enzyme js

Is there a way to add a new method to Enzyme js?

for instance, I want to add a findByTestAttr method to use this syntax wrapper.findByTestAttr('foo') instead of using wrapper.find('[data-test="foo"]')

I know I can fork their repo and extend ReactWrapper and add my method, but I want to know whether is there a solution or not.

Upvotes: 3

Views: 895

Answers (1)

Ramin Mousavi
Ramin Mousavi

Reputation: 616

so, I found a workaround:

I implemented this function inside my configuration file:

ShallowWrapper.prototype.findByTestAttr = function (attr) {
  return this.find(`[data-test="${attr}"]`)
}

Upvotes: 11

Related Questions