-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
browser does not have global
defined. Should this be globalThis
?
#438
Comments
I spent a few hours on this and simply can't replicate it. Could you please provide a repo with which to reproduce this? |
No I mean how are you installing and building your application such that you're seeing this behavior |
Sorry for my lack of understanding :( , but I don't understand the relevance? Do different environments polyfill different globals? But I'm installing with yarn, and bundling with webpack (specifically for the browser which lacks 'global', used in these places: https://github.com/testdouble/testdouble.js/search?utf8=%E2%9C%93&q=global&type= ) And in eval space here: Line 7 in cacc21f
|
To reproduce: Clone repo and delete this line: https://github.com/NullVoxPopuli/emberclear/blob/master/packages/frontend/tests/index.html#L37 error will occur during test run (I forget which one atm): |
I pulled down your repo (update: and deleted the line from your test index file), then ran
There's no mention of |
Ah, it looks like I removed it from my tests: https://github.com/NullVoxPopuli/emberclear/search?utf8=%E2%9C%93&q=testdouble&type= I'm super new to test double 😭 been thinking about learning it as an alternative to all of the sinon APIs.
What environment is that? Does it define 'global'? Fwiw, I think by default, webpack polyfills a bunch of node stuff so you don't run in to build errors -- I wonder if 'global' is a node thing? Confirm! So, I really don't like this behavior of webpack, cause.. while it makes for a better newbie experience when configuring webpack, and pulling in random npm packages (not noticing if intended for the browser or node), and making things 'just work'... But like.. you'd get 'fs' , 'path', 'crypto', etc. This super bloats your bundle. After checking the node documentation, idk what you'd do as a maintainer to support pure browser usage. The 'global' need to be abstracted or something.. like.. Idk, I found this: var getGlobal = function () {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; } // same as globalThis
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
}; https://github.com/tc39/proposal-global/blob/master/README.md |
Yes, that's correct. Even if this one line were obviated, I am not confident that the library would actually work when loaded this way by a browser. For browser use, the package includes a browserify build of the library in |
hmmm. gotchya now that I'm at a computer, I'm gonna get you something more concrete |
reproduction: https://github.com/NullVoxPopuli/testdouble-global-missing-reproduction
|
cc/ @jasonkarns in this year of 2020, is there yet an agreed up on correct way to indicate a browser distribution in package.json? I imagine |
I think it is supposed to be the browser field https://docs.npmjs.com/files/package.json#browser I've never made a multi-platform npm package before, so.. I'm not much help anyway, but should dist/testdouble.js be listed in there? |
Yes, that's been npm's documentation for a long time IIRC but there's been a lot of back and forth on actual best practice around that field and of course, predating any of it, browserify uses the field as a way to swap out browser-incompatible node files (as we are doing above) |
I had thought brawserify was dead, but after checking out their GitHub, It is not! It looks like, based on their goals of using node things in the browser that it's not possible to support brawserify and modern web dev. :/ Turns out, they have an open issue about this: browserify/browserify#1694 |
Though, even after reading that issue, it seems the brawserify folks actually do use 'browser' as an alternate entry point? |
@searls Sorry, I've been way out of this space for too long to have any valuable input here. |
@NullVoxPopuli I read the thread and agree that this is an issue. We are using the So, because we're already using the key for that purpose, we can't also use it as a way to signal where the generated/gitignored bundle is to potential clients. As a result I don't think there's yet another to signal to tools "hey this is where the fully bundled JS distribution is with a global set on window, etc". I'm going to close this for now, but if at some point someone submits a PR that makes this better for everyone without breaking anything, I'd absolutely welcome it! |
Description
I want to load my tests. :)
Issue
Environment
node -v
output: Chrome (no node)npm -v
(oryarn --version
) output: (doesn't matter?)npm ls testdouble
(oryarn list testdouble
) version:[email protected]
I don't yet have time to do any digging, so I'm skiping the repro stuff below.
Just wanted to report this for now in case something obvious is happening.
I've been able to get passed this error with:
but is this a hack? is this build-environment-dependent? idk
Failing Test
Example Repo
npm it
and observe the issueRunkit Notebook
var td = require('testdouble')
at the topCode-fenced Examples
The text was updated successfully, but these errors were encountered: