TDD - Test Driven Development with JS in Max ?
Hi,
I'm currently reading the "Clean Code" book from Robert C. Martin.
I was wondering if a tool existed in Max in order to test my JS source code with a TDD approach ?
Do any of you use TDD to develop in JS with Max ?
I'm developing with Atom editor, maybe someone can advise a good TDD package that works with JS ?
Regards,
Guillaume
run a testing framework like Jest (https://jestjs.io/) on Node either in your editor or using N4M potentially
Thanks for the tip, I'll take a look. :)
I don't know Node at all, can I ask you how to configure it in the Atom editor ?
first you need Node installed locally: https://nodejs.org/en/download/
Atom probably has a number of Packages for integrating Node but here's an option: https://blog.leonhassan.co.uk/running-node-js-in-atom/
You can just interact from a built-in terminal and the JS scripts though given Node is running.
Jest is a Node Package so you can bundle it with NPM or other types of Node bundlers: https://www.npmjs.com/package/jest
Lastly, N4M (Node for Max) will allow you to run scripts and call Jest within Max Patchers. There are a number of N4M resources on this site and video content on Youtube for looking into that.
In general you'd want to maintain a simple method of running tests so decide if that's script level or patcher level, with Max as a whole I'm not sure a JS test framework is going to serve your purpose and probably wiser to roll your own tests etc.
Thank you for this valuable info, I will try to make Jest work in Atom ! 😅
I'm refactoring some JS source code I had developed for a Max for Live device I released in 2021 on maxforlive.com. This is a MIDI editor for Oberheim's Matrix-1000 synthesizer.
This project is largely based on JS code, because I end up using very few classic Max patches, except for the views. It is for this reason that I am now interested in the TDD approach, in order to make my code cleaner and more robust. It's also very new for me, I'm only developing for fun.
Thanks again for your advice ! :)
Hello,
After a few unsuccessful attempts with Atom and the tester-jest extension from yacut, I turned to Visual Studio Code with Node and the Jest extension from Orta. I now manage to get Jest working in VSCode and test a lot of my low-level JS modules, including classic matchers (toBe, toBeTruthy, toStrictEqual, etc.), exception testing, and using mock functions .
I am having a new difficulty with Jest as I now have to test my higher level JS modules. These call Max objects or functions (for example the Folder object or the max.fileformat() function, etc.). In this case Jest does not recognize these parts of my code and it returns an error when running the tests. I don't know how to tell it where to find these JS objects and functions in Max. I'd like to avoid having to mock all Max objects and functions in my Jest tests.
Someone would have any idea ?
Thanks in advance.
Guillaume
Additional note:
My first attempts with Jest in VSCode put a hell of a mess in the self-managed 'code' folder of my M4L device, for lack of separation between the files useful to Max (my JS source code) and those useful to VSCode/Jest (coverage folder , jest.config.js, package.json, my xxx.test.js files, .git folder, .gitignore, etc).
I ended up finding a fairly practical way to avoid these file mix-ups and thus guarantee normal operation of my M4L device.
My development is organized in 2 main folders:
- a 'Max' folder containing my Max for Live device (.amxd file) as well as the self-managed folders of the associated Max project (code, patchers, media, data, etc.)
- a 'VSCode' folder containing a workspace, my JS source files, my test JS files, and everything Jest and Git need to work
I then use VSCode's 'Run on Save' extension to bulk copy my source files from 'VSCode/sources' folder to 'Max/code' folder, and those whenever I save a JS file.
This is how I set up the Run on Save extension in VSCode's settings.json file :
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.js$",
"cmd": "rsync -av --exclude='jest.config.js' *.js ../../Max/code"
}
]
},
Et voilà ! :)
Guillaume