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

feat: Test implementation of update workflow #8150

Open
wants to merge 24 commits into
base: feat/approval-workflow
Choose a base branch
from

Conversation

miya
Copy link
Member

@miya miya commented Oct 10, 2023

Task

#131556 [approval-workflow] ワークフロー編集画面からワークフローを編集できる
#131908 [server] テスト実装

@miya miya self-assigned this Oct 10, 2023
expect(caller).rejects.toThrow('Cannot edit workflows that are not in progress');
});

it('Should fail when the target approverGroup does not exist', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

ここから 357 行目まで WorkflowApproverGroupService 内部の異常系のテストなので、WorkflowApproverGroup.integ.ts に切り出すのでも良いかと思いました。WorkflowApproverGroupService.removeApproverGroup 等 WorkflowApproverGroupService.updateApproverGroup から呼び出されている各メソッドはプライベートメソッドになっているため (対象クラスのパブリックなメソッドから呼ばれることが前提となっている)、いずれにせよインテグレーションテストで良いと判断しました。

Copy link
Member

Choose a reason for hiding this comment

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

expect(caller).rejects.toThrow(message) の message 部分がちゃんとテスト対象と一致する .spec.ts や .integ.ts を作るべきなので、「WorkflowApproverGroup.integ.ts に切り出すのでも良い」ではなく「切り出さないといけない」

Copy link
Member

Choose a reason for hiding this comment

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

WorkflowApproverGroupService.updateApproverGroup から呼び出されている各メソッドはプライベートメソッドになっているため (対象クラスのパブリックなメソッドから呼ばれることが前提となっている)、いずれにせよインテグレーションテストで良いと判断しました。

これはインテグレーションテストでよい理由にはならない。単体テストが望ましいのであれば、

  • workflow-approver-group
    • index.ts
    • workflow-approver-group.ts
    • workflow-approver-group.spec.ts
    • workflow-approver-group.integ.ts
    • remove-approver-group.ts
    • remove-approver-group.spec.ts

のようなファイル構造にした上で単体テストを書くべき。

Copy link
Member Author

Choose a reason for hiding this comment

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

WorkflowApproverGroupService 内部の異常系のテストを、WorkflowApproverGroup.integ.ts に切り出しました。

Copy link
Member Author

Choose a reason for hiding this comment

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

すみません、まだどういう時に単体テストを書くべきなのかという基準がわかっていないのでインテグレーションテストのみ書いています。

以前 workflow-serializer.ts の単体テストを書いた時は workflow-serializer.ts 内部で依存している user-serializer を mock 化しすることで、workflow-serializer.ts 内部のオリジナルの実装部分のみをテストできるというメリットが理解できたのですが、今回のようなケースでは内部で依存している外部モジュール等がなかったので単体テストは書いていないです。
https://github.com/weseek/growi/blob/5eabb93912136c15a6c68a7cd088e21341c605a9/apps/app/src/features/approval-workflow/server/models/serializers/workflow-serializer.spec.ts

],
creator: { _id: new mongoose.Types.ObjectId().toString() },
getLatestApprovedApproverGroupIndex: vi.fn(() => 1),
} as any;
Copy link
Member

Choose a reason for hiding this comment

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

any はやめよう
今実装の方見たらそっちにも any いっぱいあるからなくしてほしい

Copy link
Member Author

Choose a reason for hiding this comment

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

これは自分も問題だと思っていて、mongose のインスタンス用の typeDB から取得してきた純粋なデータの type が混合している結果 any を使わざるを得ない状況になってしまっています。

現状は DB から取得してきた純粋なデータの type (IWorkflowHasId など) のみしかなく今回のケースのように mongoose のインスタンスを引数として取るものに対しても IWorkflow~ を使うような実装になってしまっています。

これは現状の DX も下がってしまっているので優先度を上げて (次回の見通しに入れるレベル) 後続タスク で対応しようとお思うのですがどうでしょうか?

改修ストーリー
https://redmine.weseek.co.jp/issues/132928

Copy link
Member

Choose a reason for hiding this comment

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

model にある type や interface を export するだけじゃだめなの?

expect(caller).rejects.toThrow('Cannot edit workflows that are not in progress');
});

it('Should fail when the target approverGroup does not exist', () => {
Copy link
Member

Choose a reason for hiding this comment

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

expect(caller).rejects.toThrow(message) の message 部分がちゃんとテスト対象と一致する .spec.ts や .integ.ts を作るべきなので、「WorkflowApproverGroup.integ.ts に切り出すのでも良い」ではなく「切り出さないといけない」

expect(caller).rejects.toThrow('Cannot edit workflows that are not in progress');
});

it('Should fail when the target approverGroup does not exist', () => {
Copy link
Member

Choose a reason for hiding this comment

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

WorkflowApproverGroupService.updateApproverGroup から呼び出されている各メソッドはプライベートメソッドになっているため (対象クラスのパブリックなメソッドから呼ばれることが前提となっている)、いずれにせよインテグレーションテストで良いと判断しました。

これはインテグレーションテストでよい理由にはならない。単体テストが望ましいのであれば、

  • workflow-approver-group
    • index.ts
    • workflow-approver-group.ts
    • workflow-approver-group.spec.ts
    • workflow-approver-group.integ.ts
    • remove-approver-group.ts
    • remove-approver-group.spec.ts

のようなファイル構造にした上で単体テストを書くべき。


// then
const createdTargetApproverGroup1 = updatedWorkflow.approverGroups[1];
const createdTargetApproverGroup2 = updatedWorkflow.approverGroups[3];
Copy link
Member

Choose a reason for hiding this comment

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

ここで assert してる対象が updateWorkflow から返ってきたドキュメントインスタンスに対してのみになっているので、
もちろんそれも必要なんだけど DB から改めて ID 指定で撮り直したドキュメントインスタンスに対しても同じ assertion をやってほしい。

Copy link
Member Author

Choose a reason for hiding this comment

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

修正しました

@miya miya requested a review from yuki-takei October 18, 2023 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants