This isn't a problem with setMocks, just with the runtime not detecting that you're running as a test. Normally it does detect this correctly, but it is possible to "break" this detection. For example, in some of my test code I use pulumi.runtime.registerStackTransformation() which does not play well with Pulumi's test-detection logic.