Live data from Hacker News

Delightful React file/directory structure

joshwcomeau.com

51–53 of 53 posts

Re: Delightful React file/directory structure

#51
post #6

Kinda wish Josh would mention how he structures his tests in his projects. Something I'm currently struggling at work is that within our code repos, the pattern that everyone seems to copy is mimic the src/ directory for the test/ directory, rather than co-locating tests along with components. This means a structure for components: src/ components/ Button/ Button.tsx Is just copied ad-hoc for tests, so we get this: t…

That's exactly what Deno does [0] in its standard library. Colocate tests with code. It just works. Some people also like to go further the bad way and group code by type rather than feature. src/ components/ componentA.tsx componentB.tsx hooks/ hookA.ts hookB.ts tests/ componentA.test.tsx componentB.test.tsx Angular pre 1.5 liked this a lot. Must be a Java thing. https://deno.land/std@0.129.0/collections

I do wonder if it's Java as well, I've never been exposed to that ecosystem but it seems many bad (IMO at least) practices seem to originate from Java.

Another one I despise is the page object pattern for E2E tests. Basically introducing a pseudo-DSL tied to something that is ever changing. I like the Cypress approach of using app actions instead:

https://www.cypress.io/blog/2019/01/03/stop-using-page-objec...

I utilize this in my playwright tests since I found it very useful when writing cypress tests previously.

Re: Delightful React file/directory structure

#52

Earlier quoted context omitted.

Co-location does not matter much if you are used to JetBrains IDE shortcuts to navigate the project ex: CTR + E : Recent files and etc. Checkout talk about Mouseless Driven Development by Hadi Hariri[1] [1] https://www.youtube.com/watch?v=UH6YVv9js3s

Never seen this talk, will definitely watch it later today. One thing I am reluctant about is having a project dictated by your IDE/editor. I think this is an antipattern. Not everyone uses JetBrains products, I myself use vim. It just makes me sad that some people prefer to have their linting, testing, and other automated checks handled by their IDE rather than using proper git hooks or scripting in general. I'm mos…

> Not everyone uses JetBrains products

Yes, you are right

> It just makes me sad that some people prefer to have their linting, testing, and other automated checks handled by their IDE rather than using proper git hooks or scripting in general.

I agree with you things like linting, testing should check in CI.

there is actually benefit of co-locating whether you use JetBrains IDE or not like easier to explore the code base when you diving into existing project.

Re: Delightful React file/directory structure

#53

Earlier quoted context omitted.

hmm. I honestly never used CRA outside of an interview assessment and don't really remember much. Early on in my web career I was encouraged to create my own scaffolding tools, that practice just always stuck with me. Kinda curious to learn how they enforced directory structures. I know now they co-locate tests, but IIRC CRA always used jest and with jest you just set the globs you want to use in the config file. Har…

Could also be that I'm remembering it wrong, but I recall having some issues with CRA and structuring tests at some point. In any case, CRA sets up a separate src and test folder, so I guess a lot of people just think they should structure their tests that way.

CRA doesn't enforce any structure, outside of "code lives in arc/ and public files go in public/"

The default templates are just a single folder, with tests located adjacent to their source files.

CRA configures jest to look for test files in arc/, but let's users override that.

https://github.com/facebook/create-react-app/tree/main/packa...

https://github.com/facebook/create-react-app/tree/main/packa...

Post reply on HN