Skip to content

Commit 280fa84

Browse files
killaclaude
authored andcommitted
fix(mock): only reuse currentContext owned by the same app
mockContext() reused this.currentContext without checking the context belongs to this app. When the async-local context storage is shared across app instances (multiple mm.app() apps in one realm, or vitest `isolate: false` carry-over) app2.mockContext() could return app1's context, binding helpers/services to the wrong app config — e.g. security `surl` custom-protocol returning '' and csrf 401s. Guard the reuse with `this.currentContext.app === this`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 28cf009 commit 280fa84

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

plugins/mock/src/app/extend/application.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ export default abstract class ApplicationUnittest extends Application {
100100
const res = new http.ServerResponse(req);
101101

102102
if (options.reuseCtxStorage !== false) {
103-
if (this.currentContext && !this.currentContext[REUSED_CTX]) {
103+
// Only reuse the active async-local context when it actually belongs to
104+
// this app. The context storage can be shared across app instances (e.g.
105+
// multiple `mm.app()` apps in one realm, or vitest `isolate: false` where
106+
// a previous app's context still lingers); reusing a foreign app's
107+
// context would bind helpers/services to the wrong app config.
108+
if (this.currentContext && this.currentContext.app === this && !this.currentContext[REUSED_CTX]) {
104109
mockRequest(this.currentContext.request.req);
105110
this.currentContext[REUSED_CTX] = true;
106111
return this.currentContext as MockContext;

0 commit comments

Comments
 (0)