Accessing values from Config file from Cypress (e2e framework)

Hi all,

Sorry noob here in JS and K6.
So, I have these config files on my Cypress folder (config/dev.json) that I want to use for my k6 tests too.
dev.json file contains the following:

{
“env”: {
“host”: “http://sample.com”,
“description”: “test”
}
}

I usually access this on my Cypress spec / test files like the example below:
const url = Cypress.env('host')

I am trying to make this my config file for k6 too but it shows me an error when running via CLI even if it is a valid json format:
k6 --config "../../cypress/config/dev.json" run script.js
ERRO[0000] invalid character ‘ï’ looking for beginning of value

I did the following on my k6 script.js and didn’t work either:

import {env} from “…/…/cypress/config/dev.json”;
const url = env.host;

export default function() {
http.get(url);
sleep(1);
}

Hi, welcome to the forum :slight_smile:

You’re confusing a couple of things here:

  • The k6 --config option is used to specify a k6-specific configuration file, not a Cypress config or any custom JSON files.
    See the documentation and an example of the contents.

  • Since k6 does not behave like a browser or NodeJS, the JavaScript used in scripts is a bit different and your attempt to import the JSON directly like that won’t work.
    You can load a JSON file with open() instead. See the example in the docs.

2 Likes

Hi @imiric Thanks!
1st point: Yes, I realised how silly I am to think that one config file can be used for two totally different framework / tools.
2nd point: "k6 not being a NodeJS is my “aha” moment. I should be reading the docs (well, don’t we all hate documentation?) before asking silly questions here.

1 Like