1

I am writing my unit tests for few services that i have written. My services and components are written in Typescript. My understanding is that its unable to parse my typescript. I am importing one such service in my unit test. I am using jest for unit testing. Without import, test is working fine

My

package.json

{
  "name": "web",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@date-io/core": "^1.3.6",
    "@material-ui/core": "^4.0.1",
    "@material-ui/icons": "^4.9.1",
    "@testing-library/dom": ">=5",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "7.2.1",
    "@types/jest": "^24.0.0",
    "@types/js-cookie": "^2.2.6",
    "@types/jwt-decode": "^2.2.1",
    "@types/lodash": "^4.14.150",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-router-dom": "^5.1.4",
    "@types/reactstrap": "^8.4.2",
    "@types/recharts": "^1.8.9",
    "@typescript-eslint/eslint-plugin": "^2.28.0",
    "@typescript-eslint/parser": "^2.28.0",
    "axios": "^0.19.2",
    "bootstrap": "4.4.1",
    "clsx": "^1.1.0",
    "cross-env": "^7.0.2",
    "eslint": "6.8.0",
    "eslint-config-airbnb": "18.1.0",
    "eslint-config-prettier": "^6.10.1",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-prettier": "^3.1.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "2.5.0",
    "jquery": "3.0.0",
    "js-cookie": "^2.2.1",
    "jwt-decode": "^2.2.0",
    "lodash": "^4.17.15",
    "material-table": "1.57.2",
    "moment": "^2.24.0",
    "popper.js": "^1.16.0",
    "prettier": "^2.0.4",
    "prop-types": "^15.7.2",
    "react": "^16.8.4",
    "react-dom": "^16.8.4",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.1",
    "reactstrap": "^8.4.1",
    "recharts": "^1.8.5",
    "typescript": "~3.7.2"
  },
  "scripts": {
    "start": "CI=true react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "tsc --noEmit && eslint \"**/*.{js,jsx,ts,tsx}\" --fix"
  },
  "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"
    ]
  },
  "devDependencies": {}
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ],
  "files": [
    "src/types.d.ts"
  ]
}

test that i created

import axios from 'axios';
test("Check if Company Details exists", () => {
  expect(false).toBe(false);
});

Issue I received

Error

Error

Also, it is clearly visible that test suite hasn't run itself

I tried different things but not to any help so far.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Rohit Mittal
  • 395
  • 1
  • 5
  • 18

3 Answers3

2

made respective changes to package.json and it worked fine @Florian' s suggestion actually gave an idea. Thanks mate


"jest": {
    "moduleFileExtensions": [
      "js",
      "jsx",
      "tsx",
      "ts"
    ],
    "rootDir": "src/tests/",
    "transform": {
      "^.+\\.(ts|tsx)?$": "ts-jest"
    }
  },```
Rohit Mittal
  • 395
  • 1
  • 5
  • 18
1

Do you have a Jest config file (https://jestjs.io/docs/en/configuration) ? If not you should have one, this seems to be the same problem as in :

You should start by adding a Jest config file (assuming your tests files are named Component.test.ts) :

module.exports = {
    rootDir: '..',
    testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
    transform: {
      '^.+\\.(ts|tsx)?$': 'ts-jest',
    },
};
Florian Motteau
  • 3,467
  • 1
  • 22
  • 42
  • Thanks @Florian, but no, still the same issue instead of adding jest.config.js, I made respective changes to package.json file – Rohit Mittal May 18 '20 at 00:50
  • "jest": { "rootDir": "src/**/*", "testMatch": [ "**/tests/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)" ], "transform": { "^.+\\.(ts|tsx)?$": "ts-jest" } },``` – Rohit Mittal May 18 '20 at 00:52
0

What solved my problem was to simply change "YourFileName.test.js" to "YourFileName.test.ts". I was using ts-jest the entire time but was using .js extension...

Seho
  • 61
  • 6