I am trying to pass my tests using jest for my javascript project. The server runs perfectly fine when running npm start and hitting various endpoints. However, when I run npm test
which calls: cross-env CI=true NODE_ENV=test jest
I get errors when using map, reduce, filter, or flatMap in my code. The error is like this:
TypeError: xxx.flatMap is not a function
or
TypeError: xxx.map is not a function
where xxx is 100% an array of array of strings.
I was running node version 14 but upgraded to 16.17.1 and still have this issue.
This is my jest configuration as well as the dev dependencies inside of package.json
:
"devDependencies": {
"core-js": "^3.31.0",
"jest": "^29.5.0",
"jest-junit": "^12.1.0",
"sinon": "^11.1.1",
"supertest": "^6.1.3"
},
"jest": {
"setupFiles": [
"core-js"
],
"collectCoverage": true,
"testPathIgnorePatterns": [
"target"
],
"collectCoverageFrom": [
"**/*.{js,}",
"!**/node_modules/**",
"!**/coverage/**",
"!**/target/**",
"!**/src/routes/**",
"!**/src/validators/**",
"!**/src/models/**"
],
"coverageReporters": [
"json",
"lcov",
"text",
"cobertura"
],
"reporters": [
"default",
"jest-junit"
]
}
What didn’t work so far:
- Uninstalling jest globally
- Adding
"setupFiles": ["core-js"]
to my jest config - Installing
core-js
and addingrequire('core-js/stable');
at the top of my test file and the file that using map/flatMap/filter/reduce npx jest --clearCache
- Creating a
jest.config.js
file at the root of my project with the config andrequire('core-js/stable');
at the top and passing--config jest.config.js
to mynpm test
script - running
npm install jest@latest --save-dev
andnpm install core-js@latest --save-dev
- deleting
node_modules
and runningnpm install
again after all of the above
Not sure what to do next. I can obviously switch to for loops but this is killing me. Why is jest not working but the server works fine normally with map, filter, reduce, and flatMap??? So lame