Skip to content

Commit

Permalink
Updated Jest In Aws Codebuild Feeling Ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswilty authored and Siteleaf committed Jul 24, 2024
1 parent 937e4e4 commit 0ae9daf
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions _drafts/jest-in-aws-codebuild-feeling-ignored.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ summary: Ignore Patterns in Jest config can lead to "no tests found" in CI
author: cwilton
---

[Jest](https://jestjs.io/) is a widely used JavaScript testing framework. It can be used "zero-config" with its sensible defaults, but you can tweak it with numerous configuration options. It turns out you can also tweak it to mess with your own sanity.
[Jest](https://jestjs.io/) is a widely used JavaScript testing framework. It can be used "zero-config" with its sensible defaults, but you can tweak it with numerous configuration options. It turns out you can also tweak it to mess with your own head.

I recently wasted a morning trying to work out why my Jest tests were running just fine locally, but weren't even being _found_ when run in AWS CodeBuild:

Expand All @@ -41,7 +41,7 @@ Take a look at this seemingly innocuous config snippet:
};
</code></pre>

I normally use [ts-jest transformer](https://kulshekhar.github.io/ts-jest/docs/) so I can write tests _and_ implementation code in TypeScript, but I choose to ignore the build folder to hide it from Jest's module loader, to avoid accidentally importing from any of the built JavaScript files (which my IDE might suggest in its efforts to be helpful, and I might blindly accept in my efforts to be productive).
I normally use [ts-jest transformer](https://kulshekhar.github.io/ts-jest/docs/) so I can write test _and_ implementation code in TypeScript. Historically I have chosen to ignore the build folder to hide it from Jest's module loader, to avoid accidentally importing from any of the built JavaScript files (which my IDE might suggest in its efforts to be helpful, and I might blindly accept in my efforts to be productive).

However, the [Jest configuration docs](https://jestjs.io/docs/configuration#modulepathignorepatterns-arraystring) state clearly that care is needed when defining ignore patterns, else you might end up accidentally ignoring all your tests or modules when run in a Continuous Integration build environment. These patterns are matched anywhere in the _absolute path_ to a resource, not just within the project directory, so the recommendation is to use the `<rootDir>` token to match strictly within your project:

Expand All @@ -65,9 +65,11 @@ As you can see, CodeBuild puts everything under a directory named "codebuild", w

<img src="/uploads/homer-hedge.gif" alt="Homer disappears into a hedge" title="Can I disappear now please" style="display: block; margin: 1rem auto;" />

## Final Words
## Final Thoughts

Even salty old coding dogs need an occasional reminder: [RTFM](https://en.wikipedia.org/wiki/RTFM)!

In fact, when using ts-jest I have no need to exclude the build folder in my config, as I can rely on includes / excludes in my test <span style="">ts-config.json</span>. But I will still need to remember this when working on purely JavaScript projects.

Note that the use of `<rootDir>/` is encouraged for all Jest's path patterns, including `coveragePathIgnorePatterns`, `moduleNameMapper`, `watchPathIgnorePatterns` and more. You have been warned.

0 comments on commit 0ae9daf

Please sign in to comment.