Skip to content
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

Closed
golfapipol opened this issue Sep 6, 2016 · 22 comments
Closed

is it possible to loop its in Jest #1619

golfapipol opened this issue Sep 6, 2016 · 22 comments

Comments

@golfapipol
Copy link

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

describe('...', () => {
    // first way
    for (let test in tests) {
        it('...', () => {
            expect(...).toBe(...)
        })
    }
    // second way
    tests.forEach((test) => {
        it('...', () => {
            expect(...).toBe(...)
        })
    })
})
@cpojer
Copy link
Member

cpojer commented Sep 6, 2016

yes it is, just do it like you wrote.

@cpojer cpojer closed this as completed Sep 6, 2016
@golfapipol
Copy link
Author

but I got this error: JavaScript heap out of memory

const testcases = require('./testcases.data') // it's JSON
describe('...', () => {
    testcases.forEach((test) => {
         it('...', () => {
             expect(...).toBe(...)
         })
    })
})

screen shot 2559-09-06 at 16 51 03

@cpojer
Copy link
Member

cpojer commented Sep 7, 2016

that means it is very likely you have an infinite loop somewhere and node just crashes.

@golfapipol
Copy link
Author

Thank @cpojer maybe I do thing wrong

@pedro-mass
Copy link

You don't get the before/after hooks making tests dynamically for some reason. I'm guessing the dynamic functions are out of scope.

@nazreen
Copy link

nazreen commented Dec 15, 2017

@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()

@faceyspacey
Copy link

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.

@ghost
Copy link

ghost commented Jan 18, 2018

I am running into the same problem with Puppeteer and Jest.

@MiguelMadero
Copy link

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

@deftomat
Copy link

deftomat commented Oct 1, 2018

The same here, we need to call beforeAll to upload a files to S3 bucket. However, a dynamically created it(...) is called before result from beforeAll resolves.

@SimenB
Copy link
Member

SimenB commented Oct 2, 2018

All describes, its and tests must be defined synchronously. Doing async work in hooks is possible though, and to use that inside tests. You cannot use it to define tests, though.

@alatras
Copy link

alatras commented Dec 19, 2018

Neither for loop nor forEach are working. Jest is not able to see any test!

fs.readdir('./testingImages/', async (err, images) => {
    if (err) {
      throw err;
    }
    for (const image in images) {
      if (image) {
        const imagePath = './testingImages/' + image;
        it(`convertor should validate the image ${image}`, async () => {
          const convert: any = await imageConvertor(imagePath);
          expect(typeof convert).toBe('string');
          fs.unlinkSync(convert); // clean up
        });
      }
    }
  });

Jest error:

Your test suite must contain at least one test.

@thymikee
Copy link
Collaborator

@alatras
Copy link

alatras commented Dec 19, 2018

@thymikee OK, so there is no way then?

@thymikee
Copy link
Collaborator

There's always a way! You have at least 2 options:

  1. generate tests based on the testingImages/ using a script.
  2. put everything in one test statement

I lean towards 1) because it will result in better test parallelization, while solution 2) will always run tests one-by-one

@alatras
Copy link

alatras commented Dec 19, 2018

@thymikee can you give me a small example of the first 😁

@thymikee
Copy link
Collaborator

No time for that. I'm sure you'll figure it out :)

@SimenB
Copy link
Member

SimenB commented Dec 19, 2018

You can use fs.readdirSync as well, that should be possible

@alatras
Copy link

alatras commented Dec 19, 2018

@thymikee I did it 😈
@SimenB Thanks!

@Relequestual
Copy link

@A-Atrash Care to share your solution to dynamically creating tests?

@sbuckpesch
Copy link

sbuckpesch commented Oct 15, 2020

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 global:

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() :-)
        });
    }
});

       

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests