-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to generate dynamic tests from data fetched async #3114
Comments
Related mocha thread: mochajs/mocha#2221 |
@chrisbreiding , Hello and sorry to bother. I see that there was some work being done in order to upgrade mocha to the latest version #2703. Is this going to enable support for This seems like a big blocker for my team, is there a workaround that we can use in the meantime? Thanks! |
Support for the I created a proposal to outline what needs to be done. As far as a workaround, the only thing I can think of is to utilize the Preprocessor API to modify the spec file on the fly and write the necessary data into the file, so you can synchronously iterate over it in the spec file to create the tests. |
@jean-moldovan did you manage to get this working? I am trying to do the same, but using cy.request instead of axios. |
@thatwpdeveloper you can use the Preprocessor API to fetch the data before the test cases run |
@AttackOnMorty Thanks! |
Looks like this is something that confuses a lot of people, so I'll post a workaround here:
No guarantee this would work the same on the latest versions. In my Simplified example: const fs = require('fs')
const path = require('path')
const axios = require('axios')
const browserify = require('@cypress/browserify-preprocessor')
module.exports = (on, config) => {
const baseSpecFilePath = path.resolve(__dirname, '../integration/mytest.template.js')
const runSpecFilePath = path.resolve(__dirname, '../integration/mytest.spec.js')
// https://github.com/cypress-io/cypress/issues/3114#issuecomment-459481491
on('file:preprocessor', (file) => {
if (!file.filePath.includes('mytest')) {
return browserify()(file)
}
return new Promise((resolve, reject) => {
axios.get('/users')
.then(res => {
const testdata = JSON.stringify(res.data)
fs.readFile(baseSpecFilePath, { encoding: 'utf-8' }, (err, data) => {
if (!err) {
const stream = fs.createWriteStream(runSpecFilePath)
stream.once('open', () => {
stream.write(`var testData = ${testdata}\n`)
stream.write(`\n`)
stream.write(data)
stream.end()
resolve(runSpecFilePath)
})
} else {
throw err
}
})
})
.catch(err => reject(err))
})
})
} Basically what happens is:
describe('my case', function () {
testData.forEach((item, i) => {
it(`should work for ${item}`, function () {
// item is available here
})
})
})
|
This could be implemented without using Mocha module.exports = async () => {
const data = await /* ... */;
describe('test', () => {
data.forEach( item => {
it(item.name, () => {
/* ... */
});
});
});
}; This way there'd be no need for extra API. The above pattern is kinda universal and intuitive, and just by looking at it everyone knows what's going on. Though, more work would be needed on Cypress part in order to change how files are being added to Mocha instance. |
You're right, exporting a promise/async function and awaiting its resolution would be more intuitive. Question but where can I dig into the cypress mechanism for adding files to the mocha instance? Once I understand that I would be glad to make an effort at an implementation sketch. |
Can someone help me understand how to modify @jean-moldovan 's solution to not break import/exports in the mytest.spec.js file? The recipe in the cypress repo does not address what happens if you modify the preprocessor and I cannot figure out how to run the resulting file (runSpecFilePath) through browserify to avoid the "cannot use import statement outside a module" error EDIT: I am now running into an issue where the test seems to refresh, and the preprocessor runs repeatedly once i start running the test in the browser. I am looking to debug this now but i no longer need help resolving the original issue i asked for. Looks like i need to understand the effects of
EDIT (for posterity): on('file:preprocessor', (file) => {
if (!file.filePath.includes('mytest')) {
return browserify()(file);
}
if (!fetched) {
fetched = true;
fetching = new Promise((resolve, reject) => {
fetchFunction(fetchConfig)
.then((initialData) => {
const stringifiedInitialData = JSON.stringify(initialData);
// eslint-disable-next-line no-undef
fs.writeFile(path.resolve(__dirname, '../support/data.json'), stringifiedMasterEligibility, (err) => {
if (err) {
reject(err);
}
resolve(browserify()(file));
});
});
});
return fetching;
}
return fetching;
});``` |
Does anyone have an updated solution or a simpler workaround for this? I'm also running into "No tests found" when using async fetch in my describe block... |
Yes, the simplest solution we found was to fetch the data before running the tests. Just run a pre-run step in your npm start that fetches and formats all the data required. When you need to run your tests, just require them in; ideally, it should be a simple JSON file you created with a very simple JS script. I hope it helps! |
Thanks @leonfs ! What a game changer! Ended up creating separate JS set up scripts and executing them with Had so much trouble trying to fit into the restrictions of how to do it within cypress, I trapped myself into that box. This "Think outside the box" approach provides way more flexibility and freedom 👍 Kudos again! |
I created a separated script that fetches the data from the API and saves the results as JSON in the fixture folder. I run that script in the config file. I require that fixture in the suite file and I loop through it to generate the tests. |
Hello!
I think this is somewhat related to https://stackoverflow.com/questions/48578761/run-tests-on-dynamic-file-in-cypress-io and #835.
In my scenario I need to fetch test data from the api and generate tests from that data. If I define data as a local var (no async) it works all fine, but if I need to prefetch it from the api (async) I get:
No tests found in your file:
Thanks!
The text was updated successfully, but these errors were encountered: