Refactor-Safe Mocks / Stubs that Allow Renaming

オープン
#6,818 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
deno, typescript
領域
testing

調査の方向性

Deno mocking tutorial の部分モックのセクションから始め、参照されている jest-mock-extended、ts-mockito、strong-mock プロジェクトを確認します。実装ファイルもテストのエントリーポイントも特定されていないため、まずこのリポジトリに存在する mocking API とテストを見つけ、その後、実装前にサポートする mock の動作とカバレッジを定義します。

索引モデルが issue の本文から書いたものです。

説明

Is your feature request related to a problem? Please describe.

Currently we get type hinting when stubbing like having autocomplete for instance methods/properties on an object. But if those names are ever refactored the test code does not update propertly.

Describe the solution you'd like

I would love to see a solution like:

This binds typing to the original class, so refactors like renaming then update throughout tests.

Example
describe('Party Tests', () => {
   test('Mock out an interface', () => {
       const mock = mock<PartyProvider>();
       mock.start('disco party');
       
       expect(mock.start).toHaveBeenCalledWith('disco party');
   });
   
   test('mock out a return type', () => {
       const mock = mock<PartyProvider>();
       mock.getPartyType.mockReturnValue('west coast party');
       
       expect(mock.getPartyType()).toBe('west coast party');
   });

Describe alternatives you've considered

as unknown as Class Pattern

We've tried the as unknown as Class pattern, but it's ugly and doesn't necessarily solve this problem.
This allows us to push partially mocked instances/classes through dependency injection.

export function createMockClient(): SomeClient {
  return {
    getWords: () => Promise.resolve(undefined),
  } as unknown as SomeClient;
}
TS Mockito

This library is fairly great, but it's not well maintained, and I would prefer to have something out-of-the-box with deno. I've also never been a huge fan of the when then pattern that it utilizes for stubbing, but it's modeled after Java Mockito and I imagine it's fairly opinionated. I've seen reasons of the when then pattern, but in my experience it just causes confusion during testing when something doesn't get called even though you're calling it. I would rather setup for a test, and if something hasn't been mocked it could error out. Maybe better messaging is key. I know the when pattern helps set expectations, but I would rather just "expect" them in my test and validate what was called.

Strong Mock

Seems promising, very much like ts-mockito, just not a lot of adoption yet it seems, but at least it seems to be maintained.

Other Thoughts

Ultimately typesafe testing in Deno feels a bit janky, between potentially lock of documentation/examples, and fighting the ES6 import system. I'm used to being able to mock out imports before a library brings the dependency in. I feel that you shouldn't have to fight the system in place, that you should be able to lean into your underlying basics of JS, you shouldn't need to introduce patterns like dependency injection (even though they are great patterns that help testing), you shouldn't need to reinvent a dependency injection container when ES6's import system is good enough.

I know there's a "partial mocking" section in the documentation, but I would rather have an empty shell that I explicitly mock out parts that are needed, rather than take a working instance and individually mocking out parts. This seems like it opens you up to potential issues with actual code in place. https://docs.deno.com/examples/mocking_tutorial/#partial-mocking This also still doesn't solve the renaming issue.

Other Things I've Looked Into
主要言語
TypeScript
スター
3.6k
フォーク
713
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

denoland/std のほかの issue

denoland/std の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。