How to conditionally skip a folder when running a collection

I need to skip a folder when I’m running a collection in a particular environment. This is the folder’s pre-request script

console.log("isAllowedEnv: ", isAllowedEnv)
if (!isAllowedEnv) {
    postman.setNextRequest(null); // Should skip all runs in the folder

It logs false, but the request in the folder runs anyway.
I have copied that code into the request’s pre-script, too, and it is still running.
I have also tried to add a dummy request in the first position, and still, both run.

setNexRequest just sets the request that will run after the current request and all of the code in any pre-request script or tests tab has completed.

The current request will always run.

However, saying all of that, what should happen with your example code is that it should just stop executing all requests after this is triggered. It won’t just skip a folder but all requests.

First troubleshooting step would be to put a console log just before the postman.setNextRequest method. Is the if statement actually being triggered?

“false” and false are not the same thing.

Once you’ve got the if statement being triggered correctly, you might want to look at the skip request feature which is probably what you need to use instead of setNextRequest().

This topic also discusses skipping folders.

Newman Run Issue for subfolders collection - :person_raising_hand: Help - Postman Community

Indeed, the answer is skipRequest() at the folder level. That will jump to the next folder, skipping all subfolders and requests within.

if (!isAllowedEnv) {