jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 117 forks source link

SyntaxError: Unexpected token [ #4

Open niksajanjic opened 7 years ago

niksajanjic commented 7 years ago

● Test suite failed to run

SyntaxError: Unexpected token [

  at transformAndBuildScript (../node_modules/jest-runtime/build/transform.js:284:10)
  at Object.<anonymous> (setupTests.js:1:809)

package.json

{
  "name": "super-admin-skeleton",
  "version": "1.0.0",
  "description": "ECP skeleton for React/Redux super admin application",
  "scripts": {
    "build": "NODE_ENV=production ./node_modules/webpack/bin/webpack.js --config ./webpack.prod.config.js --progress --profile --colors",
    "prod": "node prod.js",
    "snapshots": "jest -u",
    "start": "node server.js",
    "test": "jest"
  },
  "author": "ECP",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/EastCoastProduct/super-admin-skeleton"
  },
  "devDependencies": {
    "babel-core": "^6.16.0",
    "babel-eslint": "^7.0.0",
    "babel-jest": "^16.0.0",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-react-hmre": "^1.1.1",
    "babel-preset-stage-0": "^6.16.0",
    "connect-history-api-fallback": "^1.3.0",
    "css-loader": "^0.25.0",
    "enzyme": "^2.4.1",
    "eslint": "^3.6.1",
    "eslint-config-airbnb": "^12.0.0",
    "eslint-loader": "^1.5.0",
    "eslint-plugin-import": "^2.0.0",
    "eslint-plugin-jsx-a11y": "^2.2.2",
    "eslint-plugin-react": "^6.3.0",
    "express": "^4.14.0",
    "file-loader": "^0.9.0",
    "html-webpack-plugin": "^2.22.0",
    "jest": "^16.0.0",
    "jest-fetch-mock": "^1.0.6",
    "react-addons-test-utils": "^15.3.2",
    "react-test-renderer": "^15.3.2",
    "redux-mock-store": "^1.2.1",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.7",
    "webpack": "^1.13.2",
    "webpack-dev-middleware": "^1.8.3",
    "webpack-hot-middleware": "^2.12.2"
  },
  "dependencies": {
    "babel-polyfill": "^6.16.0",
    "es6-promise": "^4.0.3",
    "font-awesome": "^4.6.3",
    "immutable": "^3.8.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-redux": "^4.4.5",
    "react-router": "^2.8.1",
    "recompose": "^0.20.2",
    "redux": "^3.6.0",
    "redux-form": "^6.0.5",
    "redux-immutablejs": "0.0.8",
    "redux-thunk": "^2.1.0",
    "store": "^1.3.20",
    "validator": "^6.0.0",
    "whatwg-fetch": "^1.0.0"
  },
  "jest": {
    "cacheDirectory": "../test/cache",
    "collectCoverage": true,
    "collectCoverageFrom": [
      "**/*.js"
    ],
    "coverageDirectory": "../test/coverage",
    "globals": {
      "__APP_URL__": "http://192.168.50.4:9000",
      "__API_URL__": "http://192.168.50.4:3000"
    },
    "setupFiles": [
      "./setupTests.js"
    ],
    "testEnvironment": "node",
    "timers": "fake",
    "rootDir": "src"
  }
}

setupTest.js

import jestMockFetch from 'jest-fetch-mock';

global.fetch = jestMockFetch;

EDIT: Removing testEnvironment and using jsdom doesn't help. I see I can't use your module as I'm running my tests in node environment because it's much faster than jsdom so I'm going to mock it by myself.

niksajanjic commented 7 years ago

I have to point out that I copied your source code directly into my setupTest.js function and it works perfectly so it definitely isn't the code that is the problem. Something else is the cause of the problem which I still haven't figured out, maybe it's the way babel is resolving imports, but this is just a wild guess from my side. There's a good chance it has nothing to do with this module, but for now I'm leaving it open as it does break on it's import.

Anyways, I would suggest to update README and mention that this module works in default jsdom environment, but doesn't for instance in node as it expects window object to be defined with adjoining Response, Header, Request, Promise API.

coolxeo commented 7 years ago

Same for me, copying in my project works

kocokolo commented 7 years ago

I have the same question

niksajanjic commented 7 years ago

Just to let you know guys, I wasn't able to figure out what was the issue and I didn't have time to dig deep and fix it, so I switched to another module called fetch-mock which I also use inside my Jest framework and it work in jsdom and node environment.

IvanJov commented 7 years ago

Hey guys! I am having same issues in Node versions < 7. As @niksajanjic for now, fetch-mock fixed issue, but it will be awesome if we can fix this module too!

Prophet32j commented 7 years ago

+1 for fixing this. I think this is the simplest mock for fetch I have seen.

sturdynut commented 7 years ago

For me it was a node version issue. I was using 4, when I set my node version to 6.9.5 or 7 it worked.

If you use nvm: nvm use 6 or nvm use 7

You may need to install those versions if you don't have them.

IanVS commented 7 years ago

This seems to be because this package uses array destructuring, which wasn't supported until node v6.

I suggest that the engines key in the package.json specify node v6, to alert users to the minimum compatible version. @jefflau, would you accept such a PR? Or, would you rather change the code to work in older versions of node?

aymericbouzy commented 6 years ago

Same issue for me. My node version in production is 4.8.4 (because of Meteor), installing node packages with a later version of node leads to incompatibility issues, so I cannot upgrade my node version.

Is there any other solution than upgrading node version?

jefflau commented 6 years ago

Sorry for the late responses. I originally wrote the package with only the browser in mind and since most people use babel, I didn't think it would be an issue.

@IanVS I would accept a PR to do that. I have been off the ball a bit lately. Also I am unfamiliar with how the engines key works so I'll have to do some reading, but I think it sounds like a solid fix. I'm too used to ES6+ syntax so I wouldn't want to go backwards :)

@aymericbouzy If you still need this package, I think the best method would be to use babel with Jest to make sure jest runs through babel first. I think that should work, but let me know if it doesn't