Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you mock process.platform in jasmine tests?

How do you mock process.platform using jasmine specs?

like image 812
Kyle Kelley Avatar asked May 22 '15 20:05

Kyle Kelley


1 Answers

You can use Object.defineProperty() to set value of process.platform in beforeAll and then reset it to original in afterAll after tests are finished.

If you print of Object.getOwnPropertyDescriptor(process, "platform") to get descriptor configuration of platform.process in node.js console then you will get following:

{ value: 'darwin',
  writable: false,
  enumerable: true,
  configurable: true }

As you can see, value of process.platform is not writable (see docs for more information) so you can't set it using assignment operator. but you can override it using Object.defineProperty.

Jasmine Example

describe('test process platform', function(){
    beforeAll(function(){
       this.originalPlatform = process.platform;
       Object.defineProperty(process, 'platform', {  
         value: 'MockOS'
       });
    });

    it(/*test*/);

    ....

    it(/*test*/);

    afterAll(function(){
       Object.defineProperty(process, 'platform', {  
         value: this.originalPlatform
       });
    });
});

Object.defineProperty() Docs

like image 65
jad-panda Avatar answered Sep 24 '22 23:09

jad-panda