Exadra37
Why
I have prototyped an Event Modelling Canvas app with Phoenix 1.8 LiveView, and I use a lot of custom JavaScript, which I include in app.js, but this JavaScript is not tested and its already difficult to work on, because changing one thing easily breaks another.
What
I want to be able to create a testable javascript project inside the Phoenix app, preferable without using any Javascript UI framework.
I am also not looking for canvas packages, because my app isn’t a generic canvas app, rather a specific one, and I only want to allow to draw in the canvas what is really necessary for Event Modelling, therefore my custom approach.
How
I would love for you to share with me your approaches to include testable JavaScript in your Phoenix LiveView projects.
For example, do you create a brand new JavaScript project inside the vendor folder for all the custom JavaScript to be testable and then just use it from the Phoenix Hooks? Something else?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Exadra37
I was really surprised that no one had an approach to share on how to have testable JavaScript in a Phoenix project.
I will share my approach:
assets/vendor/package-name, with strict set to true and with Vitest for the test runner.assets/js/app.js.phx-hookand if needed I configure it viadata-*attributes in theheextemplate.The
assets/vendor/canvas/package.json:The
assets/vendor/canvas/tsconfig.json:The
assets/vendor/canvas/vitest.config.ts:vanderhoop
If the JS is written by your team, you can create sibling files to app.js that export testable public functions. Unit test those functions with a JS testing framework like vitest. If you want to test your JS’s integration with your backend, use an end-to-end testing framework like Wallaby or Playwright.
As a general rule, write unit tests that test “when [input], then [output]” for the all of the input cases your app needs to handle.
And if you do add end-to-end tests, only write tests for the happy path, as e2e tests are far slower and often have some flakiness to them, even when skilled people write them.
Happy testing!
vanderhoop
Hey @Exadra37 , looks like I was responding to you just as you wrote your follow-up. Overall, I think you landed in a good place for unit testing your JS.
I would make a few tweaks, but feel free to ignore them if you’re happily on your way:
canvasabstraction is completely authored by your team, I would move the code out ofassets/vendor/, as that directory is traditionally used for third-party code. You can instead house your js inassets/jsas direct descendant files (or in a subdirectory if you have many modules that you want to semantically group)tsconfig ←→ vitest.configby creating your test files as direct siblings of the modules they test. Then your test files can import the modules under test using very simple relative paths, i.e.import { funcToTest } from "./canvas". That will allow you to remove the various aliasing configs you’ve set up, which, I can tell you from experience, can lead to configuration maintenance headaches over time. If you want to go that route, I’d use file infixing for the test files, where your test files would look likecanvas.test.tsEither way, happy you landed in a decent spot. Test on!
Exadra37
Thanks for your feedback
The reason to have it in the vendor folder is that I want the package to be reusable in other projects, therefore agnostic from the current application.
Regarding the rest of the setup it was mainly Gemini Pro who decided on it, because I am a beginner when it comes to TypeScript. I just asked to create a package that followed the best practices in TypeScript, but to be honest I didn’t check what they were, because the proposal mirrored what I am used to see across programming languages, tests and source code separated.
Your proposal of joining tests alongside the module under test are also interesting, because its immediately obvious when some source code is not being tested at all. I will give it a though