codesandbox / codesandbox/codesandbox-client

global window properties are not loaded when loading an external script

Open
#8,534 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
13.6k
Forks
2.4k
Avg merge
6d 19h
Merged PRs (30d)
2

Description

🐛 bug report

Description of the problem

When loading external script, some global window properties should be added.
Specifically, after loading 'mocha' and 'chai' scripts, the following properties should exist: 'window.mocha' and 'window.chai'.
This works well in a normal environment.
However, when running the same code in a codesandbox, these properties are missing from the global 'window' variable.

It seems that the chai.js and mocha.js script files are downloaded (in the network tab of the browser debugger).

I isolated the problem to the location of 'index.html' file. When it is under the 'src' folder, this problem persists.
When it is under the project root folder, this problem doesn't occur.

How has this issue affected you? What are you trying to accomplish?

My goal is to create a codesandbox to demonstrate Mocha capability of running in a browser, as specified here:
https://mochajs.org/#running-mocha-in-the-browser

To Reproduce

Create a Node environment with a simple html, sourcing the following script:

<script src="https://unpkg.com/chai/chai.js"></script>

Then, in another script, try to access window.chai

In a non-codesandbox environment, you will get a structure.
In codesandbox, you will get undefined.

So, when trying to access window.chai.expect, the following Type Error occurs:

Cannot read properties of undefined (reading 'expect')

The reason is that chai is undefined in window

Link to sandbox:

link to sandbox

Your Environment
Software Name/Version
Сodesandbox Latest
Browser Chrome Version 127.0.6533.88 (Official Build) (arm64)
Operating System MacOS Sonoma 14.2.1 (23C71)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the linked sandbox with index.html under src, then compare its external-script loading with index.html at the project root. Trace why chai.js and mocha.js do not populate window and verify that window.chai and window.mocha are available after loading.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.