Methods
Configuration methods
There are multiple methods for defining Mocks Server configuration. When the same option is defined using multiple methods, the priority is (from lower to higher):
- Configuration received programmatically
- Configuration File
- Environment variables
- Command line arguments
The final configuration applied will be the result of extending all configuration properties received by any method, with the mentioned priority. Note that, when an option is of type Object
or Array
, the result will be also the extension of all different properties when they are defined in different sources.
info
Most of configuration properties can be changed also while the server is running using the programmatic API, the REST API, etc.
Programmatic configuration
When starting the Mocks Server core programmatically, you can provide an object with configuration properties as first argument. In the examples of the options docs page you'll see options like mocks.selected
. When provided programmatically, that option would correspond to object properties, like { mocks: { selected: "foo" }}
:
//mocks.selected option:
new MocksServer({
mocks: {
selected: "foo",
},
});
Configuration file
Mocks Server searches for configuration files in process.cwd()
. Cosmiconfig
is used to provide this feature, so it is compatible with next files formats:
- A
mocks
property in apackage.json
file - A
.mocksrc file with JSON or YAML syntax.
- A
.mocksrc.json
,.mocksrc.yaml
,.mocks.yml
,.mocksrc.js
, or.mocksrc.cjs
file. - A
mocks.config.js
ormocks.config.cjs
CommonJS module exporting the object. - A
mocks.config.js
ormocks.config.cjs
CommonJS module exporting a function. It receives programmatic configuration as first argument. - A
mocks.config.js
ormocks.config.cjs
CommonJS module exporting an async function. It receives programmatic configuration as first argument.
In the examples of the options docs you'll see options like mocks.selected
. When provided in configuration files, that option would correspond to object properties, like { mocks: { selected: "foo" }}
//mocks.selected option:
module.exports = {
mocks: {
selected: "foo",
},
};
Configuration files can also export a function. In that case, the programmatic configuration will be received as first argument, so you can modify it and return the new one. Note that it is not required to return the whole configuration, because the core itself extends the configuration received from all sources (programmatic, file, arguments, etc.)
const FooPlugin = require("mocks-server-plugin-foo");
module.exports = (config) => {
console.log(config);
return {
log: "verbose";
};
};
Environment variables
Options can be also defined using environment variables. In this case, the option name must be provided using "screaming snake case", always with the MOCKS
prefix. So, an example in the options docs like server.delay
, must be defined using environment variables as MOCKS_SERVER_DELAY
.
#server.delay option
MOCKS_SERVER_DELAY=1000 npm run mocks
Boolean values
When an option is of type Boolean
, you can define its value using 0
/1
or false
/true
:
#Disable boolean option `plugins.inquirerCli.enabled`
MOCKS_PLUGINS_INQUIRER_CLI_ENABLED=false npm run mocks
Objects
Values for options of type Object
can be defined using stringified JSONs:
#Provide value for option `server.cors.options`
MOCKS_SERVER_CORS_OPTIONS='{"preflightContinue":false}' npm run mocks
Arrays
Values for options of type Array
can be defined using stringified JSONs:
#Provide value for option `config.fileSearchPlaces`
MOCKS_CONFIG_FILE_SEARCH_PLACES='["myConfigFile.js","myConfigFile.json"]' npm run mocks
Command line arguments
Options can also be defined using command line arguments. Options must be prefixed with a double dash (--
).
In the examples of the options docs you'll see options like mocks.selected
. When provided using command line arguments, use the same format:
//mocks.selected option:
npm run mocks -- --mocks.selected=foo
note
Note the usage of two double dashes in the example. Anything after the first double dashes is not an option of npm, but a parameter for the script that npm executes.
Boolean values
When an option is of type Boolean
, if the default value of the option is true
, then use the --no-
prefix to set it to false
:
#Disable boolean option `plugins.inquirerCli.enabled`
npm run mocks -- --no-plugins.inquirerCli.enabled
If the default value of the option is false
, then use the option name to set it to true
:
#Enable boolean option `config.allowUnknownArguments`
npm run mocks -- --config.allowUnknownArguments
Objects
Values for options of type Object
can be defined using stringified JSONs:
#Provide value for option `server.cors.options`
npm run mocks -- --server.cors.options='{"preflightContinue":false}'
Arrays
Values for options of type Array
can be defined using multiple arguments. A Commander variadic
option is created under the hood to get the values, so array items have to be defined in separated arguments. Read the Commander docs for further info.
#Provide values for option `config.fileSearchPlaces`
npm run mocks -- --config.fileSearchPlaces myConfigFile.js myConfigFile2.js
Further reading
The configuration is provided to Mocks Server using the @mocks-server/config
package. If you want to learn further about how it works, you can read its own docs.