How do you mock process.platform in jasmine tests?
You can use Object.defineProperty()
to set the value process.platform
to beforeAll
and then reset the original to afterAll
after the tests are complete.
If you type Object.getOwnPropertyDescriptor(process, "platform")
to get the descriptor
config platform.process
in the node.js console you get this:
{ value: 'darwin',
writable: false,
enumerable: true,
configurable: true }
As you can see the value is process.platform
not writable (see the docs for more information), so you cannot set it uses the assignment operator. but you can override it with 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
});
});
});
source to share