Exadra37

Exadra37

How to have testable Javascript in a Phoenix LiveView Project?

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?

Most Liked

vanderhoop

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

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:

  1. If the JS source code you’ve written for your canvas abstraction is completely authored by your team, I would move the code out of assets/vendor/, as that directory is traditionally used for third-party code. You can instead house your js in assets/js as direct descendant files (or in a subdirectory if you have many modules that you want to semantically group)
  2. You can simplify your tsconfig ←→ vitest.config by 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 like canvas.test.ts

Either way, happy you landed in a decent spot. Test on! :rocket:

Exadra37

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:

  1. Created a pure TypeScript package at assets/vendor/package-name, with strict set to true and with Vitest for the test runner.
  2. I write all my TypeScript code for the LiveView Hooks with tests.
  3. I include each hook in assets/js/app.js.
  4. I assign the hook as usual with phx-hook and if needed I configure it via data-* attributes in the heex template.

The assets/vendor/canvas/package.json:

{
  "type": "module",
  "devDependencies": {
    "@types/jsdom": "^27.0.0",
    "@types/node": "^24.10.1",
    "jsdom": "^27.2.0",
    "typescript": "^5.9.3",
    "vitest": "^4.0.8"
  }
}

The assets/vendor/canvas/tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "allowJs": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "./dist",
    "types": ["vitest/globals", "node"],
    "baseUrl": ".",
    "paths": {
      "@src/*": ["src/*"],
      "@test/*": ["test/*"]
    }
  },
  "include": ["src/**/*.ts", "test/**/*.ts"],
  "exclude": ["node_modules", "dist"]
}

The assets/vendor/canvas/vitest.config.ts:

import { defineConfig } from "vitest/config";
import path from "path";

export default defineConfig({
  test: {
    environment: "jsdom",
  },
  resolve: {
    alias: {
      "@src": path.resolve(__dirname, "./src"),
      "@test": path.resolve(__dirname, "./test"),
    },
  },
});

Last Post!

Exadra37

Exadra37

Thanks for your feedback :wink:

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 :slight_smile:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement