redwoodjs / redwood

The App Framework for Startups
https://redwoodjs.com
MIT License
17.19k stars 984 forks source link

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

Open cannikin opened 3 months ago

cannikin commented 3 months ago

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?

V1shvesh commented 3 months ago

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

cannikin commented 2 months ago

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

V1shvesh commented 2 months ago

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

I hope this helps! 🙌