question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

undefined returned when using typescript with jest

See original GitHub issue

I am using jest with typescript in my projects. I am getting undefined for all my .ts files using identity-obj-proxy but .js files work as expected.

This is my tsconfig.json:

  "compilerOptions": {
    "target": "es5",
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "outDir": "lib",
    "typeRoots": [
      "./node_modules/@types",
      "./node_modules/@microsoft"
    ],
    "types": [
      "es6-promise",
      "webpack-env"
    ],
    "lib": [
      "es5",
      "dom",
      "es2015.collection"
    ]
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "lib"
  ]
}

This is my jest configuration:

"jest": {
    "unmockedModulePathPatterns": [
      "React"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "^.+\\.(d\\.ts|ts|tsx)$": "ts-jest"
    },
    "testMatch": [
      "**/src/**/*.test.+(ts|tsx|js)"
    ],
    "setupFiles": [
      "raf/polyfill"
    ],
    "collectCoverage": true,
    "coverageReporters": [
      "json",
      "lcov",
      "text",
      "cobertura"
    ],
    "coverageDirectory": "<rootDir>/jest",
    "collectCoverageFrom": [
      "**/*.{ts,tsx}",
      "!**/*.d.{ts,tsx}",
      "!**/*.scss.ts",
      "!**/models/**",
      "!**/node_modules*/**"
      "!**/services/http.ts"
    ],
    "moduleNameMapper": {
      "\\.(css|less|scss|sass)$": "identity-obj-proxy",
      "^resx-strings/en-us.json": "<rootDir>/node_modules/@microsoft/sp-core-library/lib/resx-strings/en-us.json"
   },
    "reporters": [
      "default",
      "jest-junit"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 50,
        "functions": 75,
        "lines": 75,
        "statements": 75
      }
    }
  }

My test file(.ts):

import styles from './Somefile.module.scss';

describe('Test identity proxy', () => {
  test('undefined returned', () => {
    expect(styles.notundefined).toBe(undefined);
  }
});

If I save the file as .js then everything seems to work but not in .ts or .tsx files.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
andribocommented, Nov 17, 2018

@abhishek-raj Did you try to enable "esModuleInterop" property in your tsconfig.json?

"esModuleInterop": true,
0reactions
Link631commented, Nov 27, 2019

Hi everyone. I am dependeing on Typescript 2.4.2. How can i get scss evaluated within my jest tests?

The Configuration with moduleNameMapper here does not work. My scss varaibles are all undefined within a testrun. Is it possible, that the expression under moduleNameMapper simply does not hit filese like: my.module.scss ? “jest”: { “moduleFileExtensions”: [ “ts”, “tsx”, “js” ], “transform”: { “^.+\.(ts|tsx)$”: “ts-jest” }, “testMatch”: [ “/src//*.test.+(ts|tsx|js)” ], “moduleNameMapper”: { \.(css|less|scss|sass)$": "identity-obj-proxy } }, “jest-junit”: { “output”: “./jest/summary-jest-junit.xml” }

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest.mock() method returns undefined when passing a valid ...
I have found a workaround here: https://github.com/kulshekhar/ts-jest/issues/120. It seems to be related with the way Typescript deals with ...
Read more >
Mock Functions - Jest
The value property contains the value that was thrown or returned. value is undefined when type === 'incomplete' . For example: A mock...
Read more >
Configuring Jest
Or a function returning an object: JavaScript; TypeScript. /** @returns {Promise<import('jest').Config>} */ module.exports = async () => {
Read more >
Testing Asynchronous Code - Jest
Promises​. Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test...
Read more >
ES6 Class Mocks - Jest
sound-player') returns a useful "automatic mock" you can use to spy on ... of its methods with mock functions that always return undefined...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found