Get Jasmine Configuration During Runtime

- 1 answer

do extend my jasmine.json file with some custom properties such as a server config for the server I want to send my requests to. As I also want to use this config in my tests, I need to read the jasmine.json properties during runtime.

Of course I can require(path.join(process.cwd(), 'jasmine.json')). But this soultion would not work when specifying a different location of jasmine.json when executing jasmine.

I assume there is at least one way to read the config during runtime, but it is definetly NOTjasmine.configor jasmine.getConfig().

Any ideas how to retrieve the configuration?




AFAIK it's not possible to access jasmine config through it's built-in API.

However there is still an option to achieve this through a workaround, as there are 3 possibilities to find the config:

const path = require('path');

const getJasmineConfig = () => {
    // by default jasmine is using below path to find config file
    let configPath = 'spec/support/jasmine.json';

    const configArg = process.argv.find(arg => arg.indexOf('--config') > -1);

    if (configArg) {
        // if config path is specified by --config arg in cli
        configPath = configArg.replace('--config=', '');
    else if (process.env.JASMINE_CONFIG_PATH) {
        // if config path is specified by environment variable
        configPath = process.env.JASMINE_CONFIG_PATH;

    return require(path.resolve(process.cwd(), configPath));

But yet again - it's not officially supported nor recommended by Jasmine.

Instead of using jasmine.json for this kind of things, you could create your own, project specific config file, i.e. project.config.js

and create a service that would serve you the config when needed.