kewur
kewur

Reputation: 491

Jasmine Karma fails with 'ReferenceError: process is not defined'

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?

Upvotes: 9

Views: 5754

Answers (1)

kewur
kewur

Reputation: 491

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',
]

Upvotes: 14

Related Questions