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

Remove @types/jest, and use Jest's own type annotations instead #5113

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

andiflabs
Copy link
Contributor

@andiflabs andiflabs commented Jul 8, 2024

Summary

This removes @types/jest in favor of @jest/types and friends. This allows using jest and expect to their full potential, and allow using new features like this: https://github.com/iron-fish/ironfish/blob/d8dffe54b17a3555efb013230726c3bb19eef784/ironfish/src/testUtilities/matchers/transaction.ts.

@types/jest was probably added at a time when Jest did not have any type annotations, but now that jest does have the annotations, it makes sense (in my opinion) to switch to using those. Some drawbacks:

  • jest requires more verbose type annotations
  • jest does not declare any global objects, so we have to do it by ourselves
  • jest's types are more strict, and slightly incorrect code that used to compile before no longer compiles (this is not necessarily a drawback, but it's the reason why the delta is so large)

In addition to removing @types/jest, this PR also:

  • removes some unused test functions
  • changes incorrect uses of mockResolvedValue(Promise.resolved(x)) with mockResolvedValue(x)
  • changes incorrect uses of toMatchObject(x) with toEqual(x)

Testing Plan

  • yarn test

Documentation

N/A

Breaking Change

N/A

@andiflabs andiflabs requested a review from a team as a code owner July 8, 2024 20:04
@andiflabs andiflabs force-pushed the andrea/rm-types-jest branch 3 times, most recently from 8a381ed to 6679059 Compare July 9, 2024 16:35
Comment on lines 9 to 20
const it: Global.GlobalAdditions['it']
const test: Global.GlobalAdditions['test']
const fit: Global.GlobalAdditions['fit']
const xit: Global.GlobalAdditions['xit']
const xtest: Global.GlobalAdditions['xtest']
const describe: Global.GlobalAdditions['describe']
const xdescribe: Global.GlobalAdditions['xdescribe']
const fdescribe: Global.GlobalAdditions['fdescribe']
const beforeAll: Global.GlobalAdditions['beforeAll']
const beforeEach: Global.GlobalAdditions['beforeEach']
const afterEach: Global.GlobalAdditions['afterEach']
const afterAll: Global.GlobalAdditions['afterAll']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If any TypeScript expert knows how to put everything from Global.GlobalAdditions into the global namespace with 1 line, please let me know!

Copy link
Member

Choose a reason for hiding this comment

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

const {
  it, 
  test, 
  fit, 
  xit, 
  xtest, 
  describe, 
  xdescribe, 
  fdescribe, 
  beforeAll, 
  beforeEach, 
  afterEach, 
  afterAll,
}: Global.GlobalAdditions;

Does this work for what you want?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's better, thanks. What I would really like to do is avoiding writing it, test, fit, xit, ... I would like to do something like import * from Global.GlobalAdditions

Copy link
Member

Choose a reason for hiding this comment

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

I don't think TS has a way to import all properties from a type into the global namespace, but maybe someone else knows a better way.

@andiflabs andiflabs force-pushed the andrea/rm-types-jest branch from 6679059 to 8d1f93e Compare July 10, 2024 23:55
@andiflabs andiflabs force-pushed the andrea/rm-types-jest branch from 8d1f93e to 8c16d8d Compare July 11, 2024 16:49
@andiflabs andiflabs merged commit f6439a7 into staging Jul 11, 2024
13 checks passed
@andiflabs andiflabs deleted the andrea/rm-types-jest branch July 11, 2024 22:17
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.

2 participants