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.

Has anybody thought/worked on a codemod already to automatically refactor jQuery based tests?

Would like to get rid off jQuery from ember-bootstrap (@cibernox thanks for the awesome blog post btw!), but refactoring the test suite manually is probably the most work.

Certainly it would be next to impossible to cover all possible cases of chainable jQuery code, but at least for the 90% of very common cases (assertions based on .length, .text(), .hasClass()) there could be some solution that relieves us at least from most of the work.

Have not worked with AST before, but would be a good use case to learn something new, unless there is already some work going into that direction?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
cibernoxcommented, Apr 12, 2017

Sure, play with it.

The general rules are:

Acceptance:

  1. Convert click() to await click() (same with fillIn and others)
  2. So step 1 only if the selector inside is a valid CSS3 selector (no :contains/:eq/:odd)
  3. At the end of a test check what helpers were used and add the proper import statement.
  4. If a test had one await, add async to the function.
  5. Convert this.$(selector).text() to find(selector).textContent (and assume nobody does the thing I mentioned)
  6. Convert this.$(selector).length to findAll(selector).length
  7. Convert this.$(selector).click() => click(selector);
  8. Convert this.$(selector).focus() => focus(selector);
  9. Convert this.$(selector).val('something') => fillIn(selector);
  10. Convert this.$(selector).val() => find(selector).value;
  11. Convert this.$(selector).trigger('keydown', { keyCode: num }) => keyEvent(selector, 'keydown', num);
  12. Very likely, convert $el.attr('id') to el.id

This alone is like 70% of the work.

Given that the change of getting things wrong along the way ideally the user should be propted to review and answer Y/N on every step.

0reactions
simonihmigcommented, Jun 13, 2017

Sure, this can be closed. Forgot about it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Codemods: A Quick and Easy Way to Automate Code ...
The codemod will add import PropTypes from 'prop-types'; to each file and replace any instances of React.PropTypes with PropTypes .
Read more >
Write Code to Rewrite Your Code: jscodeshift - Toptal
Up your refactoring game by using codemods, scripts used to rewrite other scripts. In this article, Toptal Freelance Developer Jeremy Greer walks us...
Read more >
Writing your very first codemod with jscodeshift - Medium
Step 1: Open AST Explorer. From the Transform menu at the top of the page, select jscodeshift. Step 2: In the top-left panel,...
Read more >
Advanced Features: Codemods | Next.js
Use codemods to update your codebase when upgrading Next.js to the latest version. ... Safely removes <a> from next/link or adds legacyBehavior prop....
Read more >
Your first codemod - CodeshiftCommunity
Your first codemod. Every codemod follows the same series of operations: find, modify/insert, remove and finally output. That's it.
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