Jasmine Karma fails with 'ReferenceError: process is not defined'
Asked Answered
K

1

9

I recently updated from Angular9 to Angular 12. After the update my tests have started "failing". I have quotes around that because the tests themselves seems to be fine, however after every run there is an "Error" entry that is not very helpful

Uncaught ReferenceError: process is not defined
ReferenceError: process is not defined
    at Object.71732 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/util/util.js:109:1)
    at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
    at Object.89122 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/assert/build/internal/assert/assertion_error.js:35:16)
    at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
    at Object.80469 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/assert/build/assert.js:36:22)
    at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
    at Module.82736 (http://localhost:9876/_karma_webpack_/main.js:2879:64)
    at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
    at webpackContext (http://localhost:9876/_karma_webpack_/webpack:/home/kewur/workplace/Project/project-app/src|sync|/\.spec\.ts$:111:1)
    at Array.map (<anonymous>)

I am kind of stumped as the error is very ambiguous. Am I missing some additional dependency that I need to take on with this upgrade? I am not refrencing process anywhere within my code, so what it's referring to has to be within webpack or karma?

Kermie answered 3/8, 2021 at 3:32 Comment(3)
do you have a compatible nodejs version installed? process reminds me of nodejs.Offshoot
I'm currently using 14.17. this is my 'ng version' output Angular CLI: 12.1.4 Node: 14.17.4 Package Manager: npm 6.14.14 OS: linux x64Kermie
Working for me: https://mcmap.net/q/355723/-karma-jasmine-failure-quot-referenceerror-is-not-defined-quot-in-angular-11/7186739Sludge
K
14

I ended up having to define a "mock" process object. I think Angular's newer versions (I'm currently on 12, up from 9) does not include the nodeJS process object.

Basically, I added a new "test" folder at the root of the app and added a file named global-variables.js with this in it

   const process = {
  env: {
    NODE_ENV :'production'
  }
};

Then I imported this file with karma in karma.conf

files: [
  'test/global-variables.js',
]
Kermie answered 3/8, 2021 at 18:52 Comment(4)
Hi! i'm little courious of how you got hold of this info, do you have a link/reference ?Tsosie
Thanks, It does work! Is anything known about the root cause and right solution?..Incoordination
Thanks, it works. Saved lots of my time!!Epiphytotic
This worked, but is there no other solution? This seems like a hack i'd like to avoid if possibleNuncle

© 2022 - 2024 — McMap. All rights reserved.