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

out-of-the-box unexpected token with create-react-app #125

Closed
afontcu opened this issue Jul 2, 2017 · 15 comments · Fixed by #162
Closed

out-of-the-box unexpected token with create-react-app #125

afontcu opened this issue Jul 2, 2017 · 15 comments · Fixed by #162
Assignees

Comments

@afontcu
Copy link

afontcu commented Jul 2, 2017

Hi there,

Environment

  1. node -v: v6.10.3

  2. npm -v: 4.6.1

  3. npm ls react-scripts (if you haven’t ejected): 1.0.10

  4. Operating system: MacOS

Steps to Reproduce

  1. install create-react-app
  2. install jest extension for Visual Studio Code (the recommended one)
  3. reload it and try to debug

Expected Behavior

Pass the tests (as it does in CLI)

Actual Behavior

 FAIL  src/App.test.js

  ● Test suite failed to run

    .../src/App.test.js: Unexpected token (7:18)
        5 | it('renders without crashing', () => {
        6 |   const div = document.createElement('div');
      > 7 |   ReactDOM.render(<App />, div);
          |                   ^
        8 | });
        9 | 

Thanks!

@orta
Copy link
Member

orta commented Jul 2, 2017

This is working for me on my default create-react-app example, https://github.com/orta/vscode-jest/tree/master/integrations/create-react-example - which I just updated now to the latest version 52610d0

screen shot 2017-07-02 at 06 59 04

Any chance you can run the extension in debug mode and have a look at what causing it?

@afontcu
Copy link
Author

afontcu commented Jul 3, 2017

Hi there,

I'm sorry, it worked flawlessly when I tried it in another project. Then I downloaded CRA again and it worked as well, so I'm assuming there was something weird in my previous installation/configuration, because it is still not working there.

Thanks for your time.

@Cerberus
Copy link

I got the same error. In my case, the cause was after I installed jest, I've updated package.json from
"test": "react-scripts test --env=jsdom",
to this
"test": "jest"
ref: react-scripts v. 1.0.7

@seanpoulter
Copy link
Member

@orta, can I help get this one fixed? Any tips on what I'm looking for if I get the extension running in debug mode?

It's failing from a clean create-react-app project with [email protected], [email protected], vscode-jest v2.4.2.

@orta
Copy link
Member

orta commented Oct 27, 2017

Honestly, I'm not that sure, maybe that the Jest config isn't being respected somehow?

@seanpoulter
Copy link
Member

seanpoulter commented Oct 30, 2017

You're right @orta. The defaultCreateReactPathWindows isn't valid for the current version of react-scripts. While I get VS Code loading the cloned extension to start debugging:

  • Did you have a strategy in mind for supporting different versions of react-scripts?
  • Would you like me to go through the issues to help indicate which ones are trying to get create-react-app working?

@orta
Copy link
Member

orta commented Oct 30, 2017

For Jest, the extension aims to support major version - 1- and doesn't strive to break backwards compatibility unless forced to (as we have the jest-extension-support lib _inside jest, so sometimes it does).

I'm happy to have a similar aim with CRA, I think it's probably a bit easier to handle as it could be a matter of checking for the right files in the right places.

I'm happy for you to do that yep - I generally run projects like this so I'll give you access rights to handle issues however you like

@djErock
Copy link

djErock commented Feb 27, 2018

So what is the fix here?

@seanpoulter
Copy link
Member

seanpoulter commented Feb 27, 2018

Have you reproduced the issue today @djErock?

@seanpoulter
Copy link
Member

If you're having problems @djErock could you please create a new issue with lots of detail so we can investigate. I've just confirmed create-react-app has worked OK on OS X, Linux, and Windows systems.

If you're asking to summarize the issue, updating the extension should resolve it. There are a couple fixes at play that improved detection of create-react-app, and spawning the runner in a shell on Windows.

@nhooey
Copy link

nhooey commented Mar 14, 2018

I'm getting the same issue with failing tests after creating a project with create-react-app and running tests with jest:

Environment

  1. node -v: v9.8.0
  2. npm -v: 5.6.0
  3. npm ls react-scripts:
[email protected] /private/tmp/react/react-test
└── [email protected]

Expected Behaviour

Pass the tests

Actual Behaviour

I am currently getting Unexpected token when running npm test after installing jest and replacing the package.json test line with jest.

Steps to Reproduce

Here's what happens when I start from scratch:

Install and set up nodeenv:

$ mkdir /tmp/react
$ cd /tmp/react
$ brew install nodeenv
    (installs nodeenv)
$ nodeenv .venv
 * Install prebuilt node (9.8.0) ..... done.
$ source .venv/bin/activate

Create react app:

(.venv) $ npx create-react-app react-test
npx: installed 67 in 5.669s
[...snipped...]
> [email protected] install /private/tmp/react/react-test/node_modules/fsevents
> node install
[fsevents] Success: "/private/tmp/react/react-test/node_modules/fsevents/lib/binding/Release/node-v59-darwin-x64/fse.node" is installed via remote
> [email protected] postinstall /private/tmp/react/react-test/node_modules/uglifyjs-webpack-plugin
> node lib/post_install.js
+ [email protected]
+ [email protected]
+ [email protected]
added 1440 packages in 59.115s
[...snipped...]

Run vanilla react tests:

(.venv) $ cd react-test
(.venv) $ npm test
> [email protected] test /private/tmp/react/react-test
> react-scripts test --env=jsdom
 PASS  src/App.test.js
  ✓ renders without crashing (17ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.925s
Ran all test suites related to changed files.

Install jest:

(.venv) $ npm install --save jest
npm WARN [email protected] requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself.
+ [email protected]
added 112 packages, removed 1 package, updated 35 packages and moved 9 packages in 18.697s

Run tests after installing jest:

(.venv) $ npm test
 FAIL  src/App.test.js
  ● Test suite failed to run

    TypeError: environment.dispose is not a function

      at Promise.resolve.then (node_modules/react-scripts/node_modules/jest/node_modules/jest-cli/build/runTest.js:102:17)
          at <anonymous>

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.791s, estimated 1s
Ran all test suites related to changed files.

packge.json before updated with jest test directive:

{
  "name": "react-test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "jest": "^22.4.2",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-scripts": "1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

Replace test directive with jest:

(.venv) $ sed -i -e 's/"test": .*/"test": "jest",/' package.json
(.venv) $ grep '"test":' package.json
    "test": "jest",

Run npm test with jest:

(.venv) $ npm test

> [email protected] test /private/tmp/react/react-test
> jest

 FAIL  src/App.test.js
  ● Test suite failed to run

    /private/tmp/react/react-test/src/App.test.js: Unexpected token (7:18)
         5 | it('renders without crashing', () => {
         6 |   const div = document.createElement('div');
      >  7 |   ReactDOM.render(<App />, div);
           |                   ^
         8 |   ReactDOM.unmountComponentAtNode(div);
         9 | });
        10 |

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.509s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

@nhooey
Copy link

nhooey commented Mar 15, 2018

@orta @seanpoulter: This issue still persists as of today with the latest versions.

@orta
Copy link
Member

orta commented Mar 15, 2018

I'm a little confused, I don't think this has anything to do with the vscode-jest extension?

@seanpoulter
Copy link
Member

seanpoulter commented Mar 16, 2018

This comment says it all: jestjs/jest#5119 (comment)

It is not supported to update Jest manually in a CRA project. If you have both jest and react-scripts in your package.json you should delete anything with jest in package.json and wait until we update react-scripts ourselves 😉


Your error isn't good but it's expected. Installing jest doesn't ensure that the dependencies for react-scripts are also updated. It seems like different versions of jest-environment-node cause this:

$ yarn jest
yarn run v1.5.1
$ /vscode-jest/issues/125/app/node_modules/.bin/jest
 FAIL  src/App.test.js
  ● Test suite failed to run

    TypeError: environment.setup is not a function
      
      at node_modules/jest-runner/build/run_test.js:71:23

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.612s, estimated 1s
Ran all test suites.

@jest-community jest-community locked as resolved and limited conversation to collaborators Mar 16, 2018
@seanpoulter
Copy link
Member

seanpoulter commented Mar 16, 2018

If you continue to experience problems please create a new issue and reference #125. I've locked the conversation for this issue since we've resolved the original and subsequent issues. 🔒

legend1202 pushed a commit to legend1202/vscode-jest that referenced this issue Jun 18, 2023
When a project is bootstrapped with create-react-app using the latest
versions of node/npm, the react-scripts executable is installed in the
top level node_modules/.bin directory. This has been added as the first
path to check.

This fixes jest-community#161, closes jest-community#39, closes jest-community#113 and resolves facebook/create-react-app#2709, and closes jest-community#125.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants