Skip to content

Run stubCall/expectCalled/expectNotCalled rewrites in setUp/tearDown/beforeEach/afterEach hooks#403

Open
chrisdp wants to merge 2 commits into
masterfrom
feature/stubcall-in-hooks
Open

Run stubCall/expectCalled/expectNotCalled rewrites in setUp/tearDown/beforeEach/afterEach hooks#403
chrisdp wants to merge 2 commits into
masterfrom
feature/stubcall-in-hooks

Conversation

@chrisdp

@chrisdp chrisdp commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • m.stubCall(...), m.expectCalled(...), and m.expectNotCalled(...) are compile-time AST rewrites performed by the bsc-plugin. Until now the plugin only walked the bodies of @it-decorated methods, so the same calls inside @setup / @tearDown / @beforeEach / @afterEach hooks were left untouched and the underlying call executed unmodified at runtime.
  • This PR walks each group's hook methods once per build (de-duped by lowercased function name) so the assertion-rewrite and global-stub-gathering passes run there too.
  • Hooks already run on the same m.testSuite instance the tests use, and per-test cleanMocks/cleanStubs already happens after each test — so stubs and expectations set up in beforeEach persist into the test and are cleaned correctly afterwards.

Code changes

  • src/lib/rooibos/TestGroup.ts — extracted the per-function walk into modifyAssertionsInFunction; added modifyAssertionsForHook(hookFuncName, ...) that reuses it.
  • src/lib/rooibos/MockUtil.tsgatherGlobalMethodMocks now walks each group's hook methods too, so free functions referenced only from a hook get registered in efficient global-stub mode.
  • src/plugin.tsbeforeFileTranspile walks each group's four hook methods once per build (separate modifiedHookFunctions Set keyed on suite + path + lowercased funcName) before processing test cases.

Tests

  • 6 new tests under transpilation in setup hooks in src/plugin.spec.ts covering: stubCall in @beforeEach, expectCalled with the early-exit guard, expectNotCalled in @afterEach, stubCall in both @setup and @tearDown, global-stub registration from @beforeEach (efficient mode), and the no-double-walk de-dup across groups that share a method name.
  • New sample test-project/src/source/HookStubs.spec.bs (no existing samples changed) demonstrating each form for documentation/regression purposes.

The bsc-plugin previously only walked the bodies of @it-decorated methods,
so m.stubCall / m.expectCalled / m.expectNotCalled inside @setup / @teardown
/ @beforeeach / @afterEach were left as-is and the underlying call executed
unmodified. Walk each hook method once per build alongside test cases so the
same AST rewrites run there.
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