Skip to content

Commit 93080a3

Browse files
DavertMikclaude
andcommitted
fix: return plugin-injected functions directly from container instead of wrapping in non-callable Proxy
The 4.x ESM migration changed Container.support(name) to always go through the proxy, which wraps values in lazyLoad() — a Proxy designed for page objects (property access), not callable functions. This broke the auth plugin's loginAs/login injection since the returned Proxy has no apply trap. Also fixes missing-key detection in inject.js (was never firing because lazyLoad always returns a truthy Proxy). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c5572aa commit 93080a3

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/container.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ class Container {
150150
if (!name) {
151151
return container.proxySupport
152152
}
153-
// Always return the proxy to ensure MetaStep creation works
153+
if (typeof container.support[name] === 'function') {
154+
return container.support[name]
155+
}
154156
return container.proxySupport[name]
155157
}
156158

@@ -600,6 +602,8 @@ function createSupportObjects(config) {
600602
let value
601603
if (container.sharedKeys.has(prop) && prop in container.support) {
602604
value = container.support[prop]
605+
} else if (prop in container.support && typeof container.support[prop] === 'function') {
606+
value = container.support[prop]
603607
} else {
604608
value = lazyLoad(prop)
605609
}
@@ -614,6 +618,9 @@ function createSupportObjects(config) {
614618
if (container.sharedKeys.has(key) && key in container.support) {
615619
return container.support[key]
616620
}
621+
if (key in container.support && typeof container.support[key] === 'function') {
622+
return container.support[key]
623+
}
617624
return lazyLoad(key)
618625
},
619626
},

lib/mocha/inject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const getInjectedArguments = async (fn, test, suite) => {
3030
testArgs[key] = test.inject[key]
3131
continue
3232
}
33-
if (!objects[key]) {
33+
if (!(key in objects)) {
3434
throw new Error(`Object of type ${key} is not defined in container`)
3535
}
3636
testArgs[key] = container.support(key)

test/unit/container_test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@ describe('Container', () => {
287287
expect(container.support('userPage')).is.ok
288288
expect(container.support('userPage').login).is.eql('#login')
289289
})
290+
291+
it('should be able to add and retrieve a function as a support object', async () => {
292+
await container.create({})
293+
const loginFunction = async name => `logged in as ${name}`
294+
container.append({ support: { loginAs: loginFunction } })
295+
expect(container.support('I')).is.ok
296+
const loginAs = container.support('loginAs')
297+
expect(loginAs).to.be.a('function')
298+
const result = await loginAs('admin')
299+
expect(result).to.eql('logged in as admin')
300+
})
290301
})
291302

292303
describe('TypeScript support', () => {

0 commit comments

Comments
 (0)