You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@victorquinn I was looking into this out of curiosity, because I use Postman as well, but never thought of using Chance in the pre-requests.
Root cause:
this.Chance and this.chance are defined manually in various ways at the end of the file (CommonJS, AMD, importScripts and window). The postman environment doesn't have any of these, including a window object, but it does have a this reference to a context.
Possible Solution:
Passing this into the function and binding to it, resolves the issue.
Replacing the window binding
Browsers today have an object called globalThis (See MDN & Can I Use)
// If there is a window object, that at least has a document property,// instantiate and define chance on the windowif(typeofwindow==="object"&&typeofwindow.document==="object"){window.Chance=Chance;window.chance=newChance();}
it could be replaced with a globalThis binding. For Postman (and that one individual left using IE11), we also pass in this into the IIFE
(function(t){/* Lines 8 - 7914 */// If there is a globalThis object (which is typically window), define chance// otherwise set it to this(t="undefined"!=typeofglobalThis?globalThis:t||self).Chance=Chance;(t="undefined"!=typeofglobalThis?globalThis:t||self).chance=newChance();})(this);
Test results
Ava Tests are not affected. (uses globalThis)
Browsers not affected
Postman works
Haven't tried Node, but since exports.Chance = Chance is still there, I don't see why it wouldn't work.
P.S. You don't have to replace the window code, you could leave it there and simple add the passing of this to the IIFE and have a few lines to handle this. It just sets window.Chance then this.Chance (which is window)
The Library is not working when loaded with eval to use it inside postman or similar kind of apps.
https://stackoverflow.com/questions/73711777/unable-to-load-and-eval-javascript-code-in-postman-pre-request
The text was updated successfully, but these errors were encountered: