Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: async transform test support #518

Closed
wants to merge 1 commit into from

Conversation

shiyangzhaoa
Copy link

@shiyangzhaoa shiyangzhaoa commented Jul 27, 2022

fix #516

* Example jscodeshift async transformer. Simply reverses the names of all
* identifiers.
*/
function transformer(file, api) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this async rather than returning a resolved promise? I think that'll more accurately reflect how people use it.

@@ -50,6 +50,7 @@
"devDependencies": {
"babel-eslint": "^10.0.1",
"eslint": "^5.9.0",
"is-promise": "^4.0.0",
Copy link
Member

@Daniel15 Daniel15 Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to pull in a third-party library here? Every third-party dependency adds extra liability.

I guess this is needed to support "promise-like" objects, when used in older versions of Node that require a polyfill? Node added native promises in v0.12 way back in 2015, so that shouldn't be an issue. I also guess it could be an issue if Jest mocks promises

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want use util.types.isPromise, but it added in v10...

if (isPromise(output)) {
return output.then(result => {
expect(result).toMatchSnapshot();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove empty line

return (output || '').trim();
}
exports.applyTransform = applyTransform;

function runSnapshotTest(module, options, input) {
const output = applyTransform(module, options, input);

if (isPromise(output)) {
Copy link
Member

@Daniel15 Daniel15 Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad we can't use async here, because then this could be a bit cleaner:

let output = applyTransform(.......);
if (isPromise(output)) {
  output = await output;
}
expect(output).toMatchSnapshot();

I think jscodeshift still supports Node.js versions without native async/await...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so. On the other hand, using async/await will cause the return value to become a promise, I'm not sure if this will affect the existing logic...
I think these modifications are ugly, i'll close the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

defineTest can not resolve async transform
3 participants