How do you mock process.platform using jasmine specs?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With