Skip to content

Latest commit

 

History

History

process-globals

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Mocking Example: A global process attribute

In get-platform.js we have the following function:

export default function getPlatform() {
    return process.platform;
}

The challenge

We want mock out the value of process.platform per test.

Gotchas

You can't directly overwrite the value and restore (e.g. process.platform = 'foo') because if you inspect the object property, 'writeable' is set to false:

$ node -p "Object.getOwnPropertyDescriptor(process, 'platform')"
{
  value: 'darwin',
  writable: false,
  enumerable: true,
  configurable: true
}

Solution

See the tests

Discussion

See: jestjs/jest#2227

A generalized version of this has been suggested here: jestjs/jest#2227 (comment)