I have the following in my cypress.config.js file:
clientCertificates: [
{
url: 'https://1.1.1.1',
ca: [],
certs: [
{
cert: 'certs/cert.pem',
key: 'certs/cert.key'
},
],
} ],
This works great when I'm testing against 1.1.1.1 (as example). But I don't want to check-in to gitlab the cert files. And if I'm not testing against 1.1.1.1 and only testing localhost, which doesn't need certs, I don't want Cypress to care if those cert files exist or not. So I'd like other developers to checkout my test repo and test against localhost and not fail because those cert files don't exist. But as of now, if I remove those cert files and then test against localhost, Cypress fails with:
Failed to load client certificates for clientCertificates[0]: ENOENT: no such file or directory, open 'certs/cert.pem'. For more debug details run Cypress with DEBUG=cypress:server:client-certificates*
Why does Cypress care if those files exist if I'm testing against localhost?
So, if I made sense, how do I work around this? I could probably just make dummy files called cert.pem and cert.key and require those files to be replaced with valid certs if someone does testing against 1.1.1.1. But that seems kinda hacky. I even tried "chromeWebSecurity: false" but it still wanted those files to exist. Is there a way to conditionally look at clientCertificates?
Thanks for any suggestions. Jon
You can split out the certificates into a separate file (which you can .gitignore
to prevent from sending to the repository).
A little javascript in cypress.config.js
can conditionally load if the client-certs.json
file exists in project.
const { defineConfig } = require("cypress");
// check for certs to load
let certs
const fs = require('fs')
const certsPath = './client-certs.json'
if (fs.existsSync(certsPath)) {
const json = require(certsPath)
certs = json.clientCertificates
}
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
if (certs) {
config.clientCertificates = certs
}
return config
},
},
})
client-certs.json
{
"clientCertificates": [
{
"url": "https://1.1.1.1",
"ca": [],
"certs": [
{
"cert": "certs/cert.pem",
"key": "certs/cert.key"
}
]
}
]
}