-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
is it possible to loop its in Jest #1619
Comments
yes it is, just do it like you wrote. |
that means it is very likely you have an infinite loop somewhere and node just crashes. |
Thank @cpojer maybe I do thing wrong |
You don't get the before/after hooks making tests dynamically for some reason. I'm guessing the dynamic functions are out of scope. |
@cpojer is it also doable with forEach? I'm encountering a race to the finish situation I have a beforeAll() that fetches info from a local API endpoint. the it() inside my forEach seems to sometimes run before the done() is called in the beforeAll() |
What you can't do unfortunately is create tests dynamically where you have to do some async setup work, eg: const createTest = async (config) => {
const foo = await api(config.url)
for (let test in config.tests) {
it('...', () => {
expect(...).toBe(...)
})
}
}
createTest(config) That would work if there was no async/await. |
I am running into the same problem with Puppeteer and Jest. |
I just ran into this same issue. My setup needs to be async, but I guess jest needs to discover all the it blocks ahead of time |
The same here, we need to call |
All |
Neither for loop nor forEach are working. Jest is not able to see any test!
Jest error:
|
@A-Atrash see: https://jestjs.io/docs/en/troubleshooting#defining-tests |
@thymikee OK, so there is no way then? |
There's always a way! You have at least 2 options:
I lean towards 1) because it will result in better test parallelization, while solution 2) will always run tests one-by-one |
@thymikee can you give me a small example of the first 😁 |
No time for that. I'm sure you'll figure it out :) |
You can use |
@A-Atrash Care to share your solution to dynamically creating tests? |
Dirty hack: Run your tests via runCLI. Before the call you can do you async requests and wait for it. Then you pass in a static data to you Jest config as test.ts import {runCLI} from 'jest';
import projectConfig from '../../../jest.config.js';
export const test = async (specs: string) => {
// Do your async preparation
const requests = await dereference(specs);
// Run jest
const result = await runCLI({
...projectConfig,
globals: JSON.stringify({requests}) // <--- Here we pass it in
}, [join(__dirname, '../project/root/')]);
} mydynamic.test.ts // Write your test
describe(`Test endpoints of ${specsPath}`, () => {
// @ts-ignore
const requests = global.requests; // <--- Here you go. Your async data
for (const request of requests) {
it(`${request.method} ${request.path}`, async () => {
// Yeah, now you can access even everything initialized in beforeAll() :-)
});
}
});
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
feature
What is the expected behavior?
is it possible to loop its in Jest
The text was updated successfully, but these errors were encountered: