Jest migration
This collects refactoring issues for moving from Karma to Jest.
## How do I migrate a spec to Jest?
1. Use [`jestodus-codemod`](https://gitlab.com/gitlab-org/frontend/playground/jestodus-codemod) and some manual intervention
1. Use our deprecated [`migrate-karma-to-jest` script](https://gitlab.com/gitlab-org/frontend/playground/migrate-karma-to-jest) and some manual intervention
1. Manually (see below)
## How do I migrate a spec to Jest manually?
1. Move the spec file(s) from the Karma directory to the corresponding Jest directory.
Example: `mv spec/javascripts/foo/foo_spec.js spec/frontend/foo/foo_spec.js`
2. Run Jest for the new files.
Example: `yarn run jest --watch spec/frontend/foo`
3. Start resolving failures using discretion and the [migration steps](https://gitlab.com/gitlab-org/gitlab/snippets/1926871).
During the migration, please make as little changes to the test files as possible. This ensures the review / overall migration is faster and potential side effects / risk of reverting is reduced. Cleaning up existing tests can happen in follow-up merge requests.
## What if there's a Karma spec which is simply unmovable to Jest (i.e. it is dependent on a running browser environment)?
Since Jest does not run in a real browser, it is an unfortunate possibility that an existing Karma spec cannot or should not be moved to Jest (at least for now)...
We want Jest to be the single source of truth for unit tests of a subject, so we should not have the same spec file exist in both Jest and Karma. This leaves us with the following options:
- Do not migrate this file to Jest and add a comment in the corresponding `Migrate spec/javascripts/... to Jest` issue why. It would also be great to add a comment on the file itself that this cannot be migrated to Jest (preferably with a link to the issue comment).
- If you can migrate most of the tests in a file, but there's a few which are dependent on an actual running browser environment, move the browser dependent Karma specs to an appropriately named Karma file (i.e. `spec/javascripts/foo/browser_spec.js`).
## What do I do with Karma test helpers like `mock_data.js` if I can't migrate an entire directory?
Jest should be the single source of truth for anything like this, so go ahead and copy these files to Jest, but update the original Karma file to read from the Jest one:
```javascript
// spec/javascripts/foo/mock_data.js
export * from '../../frontend/foo/mock_data.js';
```
## I ran into a really weird issue and I cannot figure out what's wrong. Help!
Push up some WIP code (and create a WIP MR if one doesn't already exist), then add a comment describing the situation you're running into.
Feel free to ping @pslaughter directly or any other GitLab Frontend maintainer.
## Migration steps
Please see [this snippet](https://gitlab.com/gitlab-org/gitlab/snippets/1926871).
## References
- https://leipert-projects.gitlab.io/is-gitlab-pretty-yet/jest/
- https://gitlab.com/gitlab-org/gitlab-ce/issues/52483
- https://gitlab.com/gitlab-org/frontend/playground/jestodus-codemod
- https://gitlab.com/gitlab-org/frontend/playground/migrate-karma-to-jest
epic