Skip to content

Instantly share code, notes, and snippets.

@devgripes
Last active October 9, 2019 03:37
Show Gist options
  • Save devgripes/6930cd0c645d3bc2805a31ac2b1cf7c6 to your computer and use it in GitHub Desktop.
Save devgripes/6930cd0c645d3bc2805a31ac2b1cf7c6 to your computer and use it in GitHub Desktop.

LoginForm.js

import React, { useState } from "react";

import useGlobal from "../../store";

function LoginForm() {
  const [loginUser, setUser] = useState();
  const [globalState, globalActions] = useGlobal();
  const { status, errorMessage } = globalState;

  const onChange = e => {
    e.persist();
    setUser({ ...loginUser, [e.target.name]: e.target.value });
  };

  const onLoginSubmit = e => {
    e.preventDefault();
    globalActions.login.userLogin(loginUser);
  };

  return (
    <div className="container">
      <form onSubmit={onLoginSubmit}>
        <div className="form-group">
          <label htmlFor="usernameInput">Username</label>
          <input
            name="username"
            type="text"
            className="form-control"
            placeholder="Enter username"
            onChange={onChange}
          />
        </div>
        <div className="form-group">
          <label htmlFor="inputPassword">Password</label>
          <input
            name="password"
            type="password"
            className="form-control"
            placeholder="Password"
            onChange={onChange}
          />
        </div>
        <button type="submit" className="btn btn-primary">
          Submit
        </button>
      </form>
      {status === "LOADING" ? <div>Loading...</div> : null}
      {status === "SUCCESS" ? (
        <div>Successfull login</div>
      ) : (
        <div>{errorMessage}</div>
      )}
    </div>
  );
}

export default LoginForm;

LoginForm.test.js

import React from "react";
import { render, cleanup } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";

import useGlobal from "../../../store";

import LoginForm from "../LoginForm";

afterEach(cleanup);

it("render component", () => {
  const { container, getByText } = render(<Page />);
  expect(getByText("Username")).toBeInTheDocument();
});

error:

 FAIL  src/components/login/__tests__/Form.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/kuronaru/Projects/mansoler_workspace/erams-ts/node_modules/use-global-hook/index.js:63
    export default useStore;
    ^^^^^^

    SyntaxError: Unexpected token export

      1 | import React from "react";
    > 2 | import useGlobalHook from "use-global-hook";
        | ^
      3 |
      4 | import * as actions from "../actions";
      5 |

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/store/index.js:2:1)
@nmccready
Copy link

Your problem has nothing to do with use-global-hook. You need to configure jest and babel correctly.

@devgripes
Copy link
Author

devgripes commented Oct 9, 2019

@nmccready i did eject my project and this is what package.json looks like

{
  "name": "erams-ts",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/core": "7.5.5",
    "@svgr/webpack": "4.3.2",
    "@types/hookrouter": "^2.2.1",
    "@types/jest": "^24.0.18",
    "@types/jwt-decode": "^2.2.1",
    "@types/node": "^12.7.5",
    "@types/react": "^16.9.2",
    "@types/react-dom": "^16.9.0",
    "@typescript-eslint/eslint-plugin": "1.13.0",
    "@typescript-eslint/parser": "1.13.0",
    "axios": "^0.19.0",
    "babel-eslint": "10.0.2",
    "babel-jest": "^24.8.0",
    "babel-loader": "8.0.6",
    "babel-plugin-named-asset-import": "^0.3.3",
    "babel-preset-react-app": "^9.0.1",
    "camelcase": "^5.2.0",
    "case-sensitive-paths-webpack-plugin": "2.2.0",
    "css-loader": "2.1.1",
    "dotenv": "6.2.0",
    "dotenv-expand": "4.2.0",
    "eslint": "^6.1.0",
    "eslint-config-react-app": "^5.0.1",
    "eslint-loader": "2.2.1",
    "eslint-plugin-flowtype": "3.13.0",
    "eslint-plugin-import": "2.18.2",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-react": "7.14.3",
    "eslint-plugin-react-hooks": "^1.6.1",
    "file-loader": "3.0.1",
    "fs-extra": "7.0.1",
    "hookrouter": "^1.2.3",
    "html-webpack-plugin": "4.0.0-beta.5",
    "identity-obj-proxy": "3.0.0",
    "is-wsl": "^1.1.0",
    "jest": "24.8.0",
    "jest-environment-jsdom-fourteen": "0.1.0",
    "jest-resolve": "24.8.0",
    "jest-watch-typeahead": "0.3.1",
    "jwt-decode": "^2.2.0",
    "mini-css-extract-plugin": "0.5.0",
    "optimize-css-assets-webpack-plugin": "5.0.3",
    "pnp-webpack-plugin": "1.5.0",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-normalize": "7.0.1",
    "postcss-preset-env": "6.7.0",
    "postcss-safe-parser": "4.0.1",
    "react": "^16.9.0",
    "react-app-polyfill": "^1.0.2",
    "react-cookie": "^4.0.1",
    "react-dev-utils": "^9.0.3",
    "react-dom": "^16.9.0",
    "react-router-dom": "^5.0.1",
    "resolve": "1.12.0",
    "resolve-url-loader": "3.1.0",
    "sass-loader": "7.2.0",
    "semver": "6.3.0",
    "socket.io": "^2.3.0",
    "style-loader": "1.0.0",
    "terser-webpack-plugin": "1.4.1",
    "ts-pnp": "1.1.2",
    "typescript": "^3.6.3",
    "url-loader": "2.1.0",
    "use-global-hook": "^0.1.11",
    "use-socketio": "^1.0.1",
    "webpack": "4.39.1",
    "webpack-dev-server": "3.2.1",
    "webpack-manifest-plugin": "2.0.4",
    "workbox-webpack-plugin": "4.3.1"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "roots": [
      "<rootDir>/src"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jest-environment-jsdom-fourteen",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "!node_modules/"
    ],
    "verbose": true,
    "modulePaths": [],
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ]
  },
  "babel": {
    "presets": [
      "react-app",
      "@babel/preset-env"
    ]
  },
  "devDependencies": {
    "@babel/plugin-transform-modules-commonjs": "^7.6.0",
    "@babel/preset-env": "^7.6.2",
    "@babel/preset-react": "^7.0.0",
    "@testing-library/jest-dom": "^4.1.0",
    "@testing-library/react": "^9.2.0",
    "@testing-library/react-hooks": "^2.0.1",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
    "react-hooks-testing-library": "^0.6.0",
    "react-test-renderer": "^16.10.1"
  }
}

i looked for the solution in stackoverflow but no luck.

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