Skip to content
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

'test is not defined' in Jest test files #3373

Closed
SheaBelsky opened this issue Jan 21, 2020 · 7 comments
Closed

'test is not defined' in Jest test files #3373

SheaBelsky opened this issue Jan 21, 2020 · 7 comments

Comments

@SheaBelsky
Copy link

SheaBelsky commented Jan 21, 2020

🐛 bug report

Description of the problem

When trying to open a file with Jest tests (where Jest is a dependency in package.json), CodeSandbox will show an error that test is not defined.

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

The opened CodeSandbox does not run tests as expected. As of a few days ago, this functionality worked fine. The expected behavior is that these tests run and fail, and users fix the tests as part of our educational site Awesome Learning.

Link to sandbox: https://codesandbox.io/s/github/wayfair/awesome-learning-exercises/tree/master/promises/intro-to-promises?fontsize=14

Your Environment

Software Name/Version
Сodesandbox PROD-1579516524-d2848195c
Browser 79.0.3945.130 (Official Build)
Operating System macOS

I can also reproduce this on https://codesandbox.io/s/github/wayfair/awesome-learning-exercises/tree/master/promises/advanced-promises?fontsize=14&previewwindow=tests. I noticed that the unit tests aren't running at all because test is undefined, even in a test environment.

I think this is the same issue as #2311

@SheaBelsky
Copy link
Author

SheaBelsky commented Jan 21, 2020

I determined that this issue is caused by the main field in the package.json. If I remove the main field, the issues go away. If I have the main field point to a test file, I will see these errors. We haven't had these errors before; has something changed with how that works? I only get this error if the main field points to a JavaScript test file. If it points to any other file, I don't get these errors.

@CompuIves
Copy link
Member

CompuIves commented Jan 26, 2020

Ah, yes, that's very interesting. When the main field is set to a file, it will compile that file in the main "flow". The main flow doesn't have the test globals available yet, which causes it to error in this case. Only in the test step (which is initiated by opening the test tab) it will make the test globals available.

I'm afraid that I broke this, I did an optimization recently to lazy load the test module (which shaves quite some size from the main bundle). The unintended side effect is that the test globals (like test) will only be available in the test phase at the end.

I'm sorry for the sudden change, I hadn't thought of this use case. Would it be possible for you to change the main field to point to something else? You can also drag the test tab to the browser tab, so that will be open by default. Another way to open a specific file is to put ?module=/src/index.js for example in the url.

I'm sorry for the inconvenience for this, hopefully we can find a proper workaround for this!

@SheaBelsky
Copy link
Author

@CompuIves Thank you for the response! It sounds like this isn't something that's going to be fixed, then, so I'll have to use the ?module query string to get these CodeSandboxes working again.

Do you anticipate that the main flow will have test globals available sometime soon? Is there an issue or pull request I can follow on that, or is it super far down the pipeline?

@lbogdan
Copy link
Contributor

lbogdan commented Feb 3, 2020

In my opinion having tests in main, module or whatever other entrypoint setting is not a valid use-case, so I'd say this is a "will not fix". @CompuIves , thoughts?

@SheaBelsky Out of curiosity, why did you set main to a test file in in the first place?

@SheaBelsky
Copy link
Author

@lbogdan I'm the project lead for Awesome Learning, which is a learning platform for front-end technology and code. We have many lessons and courses, which use CodeSandbox's Git wizard to automatically populate a CodeSandbox based on the contents of a GitHub repo.

Example:

https://codesandbox.io/s/github/wayfair/awesome-learning-exercises/tree/master/promises/advanced-promises?fontsize=14&previewwindow=tests

In these situations, we have students write unit tests to confirm their knowledge based on a particular subject. Until this was changed, we used the main field to automatically have CodeSandbox point the user to a test file where they could immediately begin coding (instead of needing to hunt down which file to start on.) We can manually call out the file another way, but it was great when CodeSandbox automatically popped open the file when it loaded.

@lbogdan
Copy link
Contributor

lbogdan commented Feb 3, 2020

Makes sense! In this case, what you were / are doing is more like a workaround, but what you actually need is something along the lines of #514 , right?

@SaraVieira
Copy link
Contributor

Closing this as we have #514 for allowing settings in Jest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants