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

Migrate from Mocha to Jest #706

Merged
merged 2 commits into from Oct 1, 2022

Conversation

miles170
Copy link
Contributor

@miles170 miles170 commented Sep 20, 2022

Closes #705.

@miles170 miles170 marked this pull request as draft September 20, 2022 01:46
@miles170 miles170 changed the title Remove TS version from template Migrate from mySnapshotTest to mocha-chai-jest-snapshot Sep 20, 2022
@miles170 miles170 changed the title Migrate from mySnapshotTest to mocha-chai-jest-snapshot Migrate from mySnapshotTest to mocha-chai-jest-snapshot Sep 20, 2022
@miles170 miles170 marked this pull request as ready for review September 20, 2022 01:50
Copy link
Owner

@Maxim-Mazurok Maxim-Mazurok left a comment

Choose a reason for hiding this comment

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

Sorry for not getting back to you earlier, now that I look at it, it's pretty hard to review changes to snapshots when they are all packed into one file with no syntax highlight... I would like to have them in separate files with proper extensions (to allow syntax highlight). I did a little investigation and didn't find an easy way to do this.

There's a jest plugin that can help us: https://github.com/igor-dv/jest-specific-snapshot

However, I find Jest to be significantly slower than Vitest, so if we would migrate from mocha/chai - we probably should migrate to Vitest. And I think that jest-specific-snapshot might work with Vitest since it has a lot of interoperability with Jest, but that needs to be confirmed.

TL;DR, let's investigate how we can keep snapshot files as they currently are, maybe by using Vitest with jest-specific-snapshot plugin

I appreciate your efforts, and it's unfortunate that I didn't see this through at first! Please feel free to post your findings/design here first before taking the time to code, thank you!

@miles170
Copy link
Contributor Author

Sorry for not getting back to you earlier, now that I look at it, it's pretty hard to review changes to snapshots when they are all packed into one file with no syntax highlight... I would like to have them in separate files with proper extensions (to allow syntax highlight). I did a little investigation and didn't find an easy way to do this.

There's a jest plugin that can help us: https://github.com/igor-dv/jest-specific-snapshot

However, I find Jest to be significantly slower than Vitest, so if we would migrate from mocha/chai - we probably should migrate to Vitest. And I think that jest-specific-snapshot might work with Vitest since it has a lot of interoperability with Jest, but that needs to be confirmed.

TL;DR, let's investigate how we can keep snapshot files as they currently are, maybe by using Vitest with jest-specific-snapshot plugin

I appreciate your efforts, and it's unfortunate that I didn't see this through at first! Please feel free to post your findings/design here first before taking the time to code, thank you!

I would like to try separate snapshots in separate files first.
Maybe use the https://github.com/igor-dv/jest-specific-snapshot.

If everything works fine, maybe migrate Jest to Vitest.

@miles170
Copy link
Contributor Author

It seems that even if we separated snapshots into separate files,
we wouldn't get syntax highlighting because snapshots are still stored as strings.

See jestjs/jest#2676 (comment)

@Maxim-Mazurok
Copy link
Owner

I see... I'd probably suggest creating a separate PR for #688
And then maybe research if there's any better solution for snapshot testing that would preserve syntax highlight in generated files.
Thank you!

@Maxim-Mazurok
Copy link
Owner

I've updated title and description for #705 to reflect our findings

@miles170
Copy link
Contributor Author

I see... I'd probably suggest creating a separate PR for #688 And then maybe research if there's any better solution for snapshot testing that would preserve syntax highlight in generated files. Thank you!

Jest Snapshot Language Support provides syntax highlighting for your Jest snapshots.

@miles170 miles170 marked this pull request as draft September 22, 2022 02:35
@Maxim-Mazurok
Copy link
Owner

Well, it seems to kinda work for d.ts files:
image

But doesn't work well for package.json:
image

or markdown:
image

And also that's not an option when reviewing here on GitHub.

However, with Jest/Vitest snapshots we'll get multiplatform support and all other nice features for free, making this project easier for new contributors, among other things...

I think that if we could at least split templates into individual files, even without syntax highlighting the benefits probably would outweigh the limitations. What do you think?

@miles170
Copy link
Contributor Author

I agree with you. It might be great if we first split snapshot files first, then go on to migrate to vest or syntax highlighting.

@miles170
Copy link
Contributor Author

There is one problem with using jest-specific-snapshot that we have to write ChaiPlugin for it.

@Maxim-Mazurok
Copy link
Owner

Cool :)
Then let's do this:

Thanks!

@Maxim-Mazurok
Copy link
Owner

There is one problem with using jest-specific-snapshot that we have to write ChaiPlugin for it.

I don't think it's worth it, we probably can just migrate these tests to jest instead of using mocha-chai-jest-snapshot.

mocha-chai-jest-snapshot is kinda workaround to add snapshot testing for mocha/chai...

@miles170
Copy link
Contributor Author

There is one problem with using jest-specific-snapshot that we have to write ChaiPlugin for it.

I don't think it's worth it, we probably can just migrate these tests to jest instead of using mocha-chai-jest-snapshot.

mocha-chai-jest-snapshot is kinda workaround to add snapshot testing for mocha/chai...

I'll see what I can do.

@miles170 miles170 changed the title Migrate from mySnapshotTest to mocha-chai-jest-snapshot Migrate from mySnapshotTest to jest-specific-snapshots Sep 22, 2022
@miles170
Copy link
Contributor Author

miles170 commented Sep 22, 2022

Playing around, I found out that jest with typescript did not play well with ES modules.

TS and ts-jest meet "type": "module"
jestjs/jest#9430

@Maxim-Mazurok
Copy link
Owner

Playing around, I found out that jest with typescript did not play well with ES modules.

TS and ts-jest meet "type": "module" facebook/jest#9430

Yeah, I've had a lot of trouble setting up jest with ES Modules, but sometimes I was lucky...
Perhaps we should look into using Vitest straight away since it promises to support ESM out-of-box

@miles170
Copy link
Contributor Author

Playing around, I found out that jest with typescript did not play well with ES modules.
TS and ts-jest meet "type": "module" facebook/jest#9430

Yeah, I've had a lot of trouble setting up jest with ES Modules, but sometimes I was lucky... Perhaps we should look into using Vitest straight away since it promises to support ESM out-of-box

I migrated the test to Jest. If you wouldn't mind, please check the code. Thanks!

@miles170 miles170 force-pushed the cleanup-template branch 2 times, most recently from 5a879c9 to aa57209 Compare September 22, 2022 09:07
@miles170 miles170 changed the title Migrate from mySnapshotTest to jest-specific-snapshots Migrate from mySnapshotTest to Jest + jest-specific-snapshots Sep 22, 2022
@miles170 miles170 marked this pull request as ready for review September 22, 2022 09:09
@miles170 miles170 force-pushed the cleanup-template branch 2 times, most recently from e7fdc1a to 95a911d Compare September 23, 2022 02:48
Copy link
Owner

@Maxim-Mazurok Maxim-Mazurok left a comment

Choose a reason for hiding this comment

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

Amazing job! I really appreciate your efforts. I've added some comments that I'd like to discuss/address before merging. Thank you!

jest.config.js Outdated Show resolved Hide resolved
jest.config.js Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
test/restDocs/test.spec.ts Outdated Show resolved Hide resolved
test/restDocs/test.spec.ts Outdated Show resolved Hide resolved
test/restDocs/test.spec.ts Outdated Show resolved Hide resolved
test/utils.spec.ts Outdated Show resolved Hide resolved
test/utils.spec.ts Outdated Show resolved Hide resolved
@miles170
Copy link
Contributor Author

miles170 commented Sep 24, 2022

All requested changes are resolved.

@miles170 miles170 force-pushed the cleanup-template branch 9 times, most recently from ee812ec to 062854a Compare September 27, 2022 11:46
Copy link
Owner

@Maxim-Mazurok Maxim-Mazurok left a comment

Choose a reason for hiding this comment

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

I do appreciate the investigation and effort that went into making sure that snapshots are in individual files, I've never dealt with this before and this is for sure very interesting knowledge about jest's inner workings. Now I can see that this requires much more complication than I originally anticipated. Custom resolver, for example, is very advanced, requires a deep understanding of jest inner-workings, and in case jest breaks backward compatibility in future updates - it'll be very challenging to work with. And we can't use TS yet which isn't good for code quality. And we probably can't really test this complex logic.
Now having this full picture thanks to your research, I think that having one huge file with snapshots isn't that big of a problem. The default approach is quite good for JSON and TS files: it doesn't add extra escaping for double quotes and jest extension in VS Code lets you fold it and does a pretty good job at highlighting. Markdown README will be a challenge as it can't be folded and it takes up many pages so it might be challenging to understand the context, like from which test this part of README is. But that shouldn't be a huge problem.
I'm pretty happy with all the changes in tests. Do you think that it'd make sense to bring all the snapshots back into one file?
Thank you!

.eslintignore Outdated Show resolved Hide resolved
@miles170
Copy link
Contributor Author

I do appreciate the investigation and effort that went into making sure that snapshots are in individual files, I've never dealt with this before and this is for sure very interesting knowledge about jest's inner workings. Now I can see that this requires much more complication than I originally anticipated. Custom resolver, for example, is very advanced, requires a deep understanding of jest inner-workings, and in case jest breaks backward compatibility in future updates - it'll be very challenging to work with. And we can't use TS yet which isn't good for code quality. And we probably can't really test this complex logic.
Now having this full picture thanks to your research, I think that having one huge file with snapshots isn't that big of a problem. The default approach is quite good for JSON and TS files: it doesn't add extra escaping for double quotes and jest extension in VS Code lets you fold it and does a pretty good job at highlighting. Markdown README will be a challenge as it can't be folded and it takes up many pages so it might be challenging to understand the context, like from which test this part of README is. But that shouldn't be a huge problem.
I'm pretty happy with all the changes in tests. Do you think that it'd make sense to bring all the snapshots back into one file?
Thank you!

Agreed! Thanks for your efforts.

@miles170 miles170 changed the title Migrate from mySnapshotTest to Jest + jest-specific-snapshots Migrate from mySnapshotTest to Jest Sep 29, 2022
@miles170 miles170 changed the title Migrate from mySnapshotTest to Jest Migrate from Mocha to Jest Sep 29, 2022
Copy link
Owner

@Maxim-Mazurok Maxim-Mazurok left a comment

Choose a reason for hiding this comment

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

I have generated all types using the master branch, committed, then generated using your branch and compared the difference, there was none, except for one package where revision got updated. So I'm quite certain this won't break anything and will be very happy to merge it as soon as you fix that little timeout issue. Thank you so much!

'v10',
'v11',
]);
}, 0); // performs requests to the actual server
Copy link
Owner

Choose a reason for hiding this comment

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

Turns out setting timeout to 0 doesn't disable timeouts in jest, please set it to 30000 instead, should give it enough time. It took just ~2 seconds for CI so that's why it didn't fail, but it tool just a little bit over the default 5s timeout on my computer (probably Australian internet to blame here)

Copy link
Owner

Choose a reason for hiding this comment

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

Fixed in a613f11

@Maxim-Mazurok
Copy link
Owner

Actually, I think it's easier and quicker to just merge it and fix the test myself

@Maxim-Mazurok Maxim-Mazurok merged commit 8dbb7af into Maxim-Mazurok:master Oct 1, 2022
@Maxim-Mazurok
Copy link
Owner

Thank you again for an amazing job!

@miles170 miles170 deleted the cleanup-template branch October 1, 2022 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maybe migrate from mySnapshotTest to Jest + jest-specific-snapshots
2 participants