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

[Bug]: Scenario files in a __tests__ directory throws error in Jest #10870

Open
1 task
cannikin opened this issue Jun 21, 2024 · 3 comments
Open
1 task

[Bug]: Scenario files in a __tests__ directory throws error in Jest #10870

cannikin opened this issue Jun 21, 2024 · 3 comments
Labels

Comments

@cannikin
Copy link
Member

What's not working?

By default Redwood generates tests and scenarios as "sidecar" files next to the thing they're testing:

api
└── src
    └── services
        └── users
            ├── users.js
            ├── users.scenarios.js
            └── users.test.js

However, a common pattern in JS land is to gather all your tests in a __tests__ directory (we follow this pattern in the framework itself in many places):

api
└── src
    └── services
        ├── __tests__
        │   ├── users.scenarios.js
        │   └── users.test.js
        └── users
            └── users.js

With this directory structure in an app Jest thinks that everything in __tests__ is a test file and so tries to execute the scenarios file as if it was a test and raises an "empty test file" error:

image

This particular bug only applies to the api side, but I imagine something similar will happen if you place *.mocks.js files from the web side into a __tests__ directory as well.

I was able to work around this by updating the api/jest.config.js file in the api side:

const config = {
  rootDir: '../',
  preset: '@redwoodjs/testing/config/jest/api',
  testPathIgnorePatterns: ['.scenarios.[jt]s$'],
}

But I think ignoring them should be the default behavior of the framework.

The easiest fix would be to update CRA to generate the config file like this, but ideally our internal Jest config would specify this option automatically, leaving this config file nice and clean by default.

This should not be a breaking changes as we already "reserve" the scenarios.js filename and so I don't think anyone would be relying on this (incorrect) behavior currently.

How do we reproduce the bug?

  1. Generate a redwood app
  2. Generate a random service like users
  3. Move the users.test.js and users.scenarios.js files to a __tests__ subdirectory
  4. yarn rw test api

What's your environment? (If it applies)

No response

Are you interested in working on this?

  • I'm interested in working on this
@V1shvesh
Copy link
Contributor

V1shvesh commented Jul 7, 2024

Hey @cannikin! I would love to raise a PR for solving this!

@cannikin
Copy link
Member Author

cannikin commented Jul 8, 2024

That'd be great, go for it! :)

@V1shvesh
Copy link
Contributor

Hey @cannikin!
I've created a PR for solving this issue: #10945

I hope this helps! 🙌

cannikin pushed a commit that referenced this issue Jul 30, 2024
…lder (#11108)

Solves for #10870

### Description:
`./api` directory structure:
<img width="438" alt="image"
src="https://github.com/user-attachments/assets/932e78a8-e105-4a98-a67e-3e71e292f325">
`./web` directory structure:
<img width="594" alt="image"
src="https://github.com/user-attachments/assets/00a12bde-c7dd-4ef5-a6c5-bb403cbde2e0">



Before: 

`yarn rw test api`:
<img width="772" alt="image"
src="https://github.com/user-attachments/assets/1ccbe49a-7702-4134-b07b-cc4a0ad1cce9">

`yarn rw test web`:
<img width="1170" alt="image"
src="https://github.com/user-attachments/assets/814c987b-f562-43cd-a3e2-08ab2ddf0b15">



After:
`yarn rw test api`:
<img width="778" alt="image"
src="https://github.com/user-attachments/assets/55c6b352-1c3c-487c-b7a0-df64b56eda60">

`yarn rw test web`:
<img width="1168" alt="image"
src="https://github.com/user-attachments/assets/e96d7423-826c-4987-8d81-de12f376ff31">

---------

Co-authored-by: Daniel Choudhury <dannychoudhury@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants