My goal is to parse some json file that determines which specific set of 'it' tests should be ran from a larger list of tests contained in another file. Below is an example of my thought process.
This context behind this question is the json file contains information about which parts of the page a given user role (client, admin, owner, etc.) has access to, and I would like to only run tests on the parts of the page the user has access to with the ability to have multiple files like allTestsForPage.js
for each page on the website.
From my testing I can get the functions to run, but not the 'it' test inside of them. I am not sure if this is the correct line of thinking or if there is a better alternative to this problem.
// spec.js
describe("run tests dynamically", () => {
let methods = require('allTestsForPage.js')
before(() => {
cy.visit('/') // visit homepage
cy.fixture('rawData.json').as('data') // get json data
cy.get('@data').then((data) => {
// parse json data into list of tests to run
// Ex: listOfTests = ['a', 'c']
listOfTests.forEach((test) => {
methods[test]() // runs the 'it' tests within a() and c() from allTestsForPage.js
}
}
}
}
// allTestsForPage.js
function a(){
it('should load page', () => {
// do some test
}
}
function b(){
it('click an add button', () => {
// do some test
}
}
function c(){
it('click a remove button', () => {
// do some test
}
}
module.exports = {
a:a,
b:b,
c:c
}