jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 117 forks source link

Improved TS support #99

Closed dsebastien closed 5 years ago

dsebastien commented 5 years ago

Hello,

I've tried to improve the TypeScript type definitions. With those changes, the TS API should closer match the implementation.

dsebastien commented 5 years ago

This should close #83 and #82

jefflau commented 5 years ago

This is great! Thanks for this. I don't have much TS experience, so a couple of noob questions. How are you running the typescript tests you have in there? I'm assuming it's this line "test": "jest && npm run tsc && npm run dtslint". Which module installs the tsc command line tool, and is that compiling the typescript?

What is dtslint and what does it do? How is it configured?

Is this the config that allows you to just import jest-fetch-mock in your tests.ts ?

"paths": {
      "jest-fetch-mock": [
        "."
      ]
    }

Thanks again for this. Just trying to understand all the moving parts first.

dsebastien commented 5 years ago

Hello Jeff, great questions!

Indeed, the tests are executed through npm run test.

First, dts-lint is executed to check the "styling" rules of the TypeScript type definitions. dts-lint provides a set of lint rules on top of the TSLint linter that are tailored for checking type definition files. I load those additional rules through the "tslint.json" file.

Note that dts-lint checks for comptability with previous TypeScript versions, which is why there is the following comment at the top of index.d.ts: // TypeScript Version: 2.3. This means that the type definitions I've written will work down to TS 2.3, which is pretty old.

Then, just to be on the safe side, I also execute the TypeScript compiler (the tsc command). tsc is installed by the devDependency on TypeScript. Indeed the TypeScript compiler compiles the test.ts file and takes the type definitions into account. No output is generated because noEmit is set to true, but at least if the test command passes it means that the code could be compiled successfully.

And indeed yes, the paths option is what allows the import of jest-fetch-mock to work.

jefflau commented 5 years ago

Hey @eryshev @Guria @spirosikmd, you guys did some previous TS PRs. How does this look to you? Would be great to get a once over by you guys as you guys are the typescript users!

dsebastien commented 5 years ago

Hello.

I've updated the PR based on the comments.

dsebastien commented 5 years ago

Hello @jefflau. By any chance, can we hope a release/publish this year still?

jefflau commented 5 years ago

Pretty sure i published 3 days ago. Can you check?


From: Sebastien Dubois notifications@github.com Sent: Tuesday, December 18, 2018 8:28:34 AM To: jefflau/jest-fetch-mock Cc: Jeff Lau; Mention Subject: Re: [jefflau/jest-fetch-mock] Improved TS support (#99)

Hello @jefflauhttps://github.com/jefflau. By any chance, can we hope a release/publish this year still?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/jefflau/jest-fetch-mock/pull/99#issuecomment-448137087, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABKiFedIYz5yqDt0T5xsuTNV1ebFwvJYks5u6KcygaJpZM4Y9ejJ.

dsebastien commented 5 years ago

Oh yes indeed. I was only looking at the releases tab in GitHub :)

dsebastien commented 5 years ago

Great, thanks! Now I can finalize the code for my book's chapter ;-)

jefflau commented 5 years ago

What’s your book about?

dsebastien commented 5 years ago

About TypeScript! It'll be a hands-on book to learn TS for creating Web applications, full of code samples and example projects. I've posted a bit more info over here: https://medium.com/@dSebastien/the-story-behind-my-upcoming-book-learn-typescript-by-building-web-applications-part-1-26926bd1756d. More articles will come later on :)

In the fifth chapter I'm explaining how to use promises, async/await and fetch. Then of course some testing notions are introduced for good measure, which is why I needed jest-fetch-mock and better typings ;-)