Cannot find module ‘./lib/source-map-generator’ from ‘source-map.js’
Solution: add other extensions like so: “moduleFileExtensions”: [“ts”,”tsx”,”js”,”jsx”]
SyntaxError: Cannot use import statement outside a module
Solution Steps:
1. Upgrade to Jest 26,
2. npm install @types/jest (Other solutions to add “type”: “module” in package.json did not work for me.
3. Create a file config.json in root directory with below contents:
{
“preset”: “ts-jest”,
“verbose”: true,
“roots” : [“./src/app/services”],
“testURL”: “http://localhost/”,
“moduleFileExtensions”: [“ts”,”tsx”,”js”,”jsx”],
“testMatch”: [ “**/__tests__/**/.ts?(x)”, “/?(.)(spec|test).ts?(x)”]
}
4. Update package.json for test like so :
“test”: “jest –config config.json”,
5. Run npm test
Test typescript fails to compile with error:
SyntaxError: <…..>\src\app\services\utility.service.spec.ts: Unexpected token (16:15)
at Parser._raise (node_modules/@babel/parser/src/parser/error.js:60:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:55:17)
at Parser.raise (node_modules/@babel/parser/src/parser/error.js:39:17)
at Parser.unexpected (node_modules/@babel/parser/src/parser/util.js:139:16)
at Parser.parseVar (node_modules/@babel/parser/src/parser/statement.js:1019:18)
at Parser.parseVarStatement (node_modules/@babel/parser/src/parser/statement.js:694:10)
at Parser.parseStatementContent (node_modules/@babel/parser/src/parser/statement.js:211:21)
at Parser.parseStatement (node_modules/@babel/parser/src/parser/statement.js:151:17)
at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/parser/src/parser/statement.js:871:25)
at Parser.parseBlockBody (node_modules/@babel/parser/src/parser/statement.js:841:10)
Solution: Make sure ts-jest is installed and add “preset”: “ts-jest” into config.json