undefined returned when using typescript with jest
See original GitHub issueI 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:
- Created 5 years ago
- Reactions:1
- Comments:12 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@abhishek-raj Did you try to enable
"esModuleInterop"
property in yourtsconfig.json
?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” }