A $mol view is a function of its state, and that changes what a test looks like. A user scenario is written as calls to the view's own methods: set the draft, run the action, read what the user would see.1app.draft('Milk')2app.add()3$mol_assert_equal(app.item_title(0),'Milk')No selectors, no browser, no Playwright or Cypress, no waiting for anything. The same file also gives you:Speed. Tests run in Node in milliseconds; a thousand of them take about a minute. nodemy/hello/-/node.test.js runs the ones for a module.Zero setup. A hello.test.ts next to the component is picked up by the builder and compiled into -/node.test.js. Continuous integration with mam_build runs it and fails the build when a test fails.Service substitution through the context. Every test gets its own $, and everything a component reaches through this.$.X is swapped by assigning a test double to that $. That is the reason to write this.$.$mol_fetch rather than $mol_fetch: the first one is replaceable, the second one is not.Mocked time. Timers created through the context do not tick on their own; $mol_after_mock_warp() runs whatever is queued.A real DOM when you need one. The Node bundle carries jsdom, so dom_node() and querySelectorAll work in the same test file.
Take the todo list from the Cookbook: a draft? string, an items list, an add action and a delete action. Its test lives in my/todo/todo.test.ts:1namespace ${2$mol_test({34'add an item and delete it'($){5const app=$my_todo.make({$})67app.draft('Milk')8app.add()9$mol_assert_equal(app.items().length,1)10$mol_assert_equal(app.item_title(0),'Milk')11$mol_assert_equal(app.draft(),'')1213app.delete(0)14$mol_assert_like(app.item_rows(),[])15},1617'blank draft adds nothing'($){18const app=$my_todo.make({$})1920app.draft(' ')21app.add()22$mol_assert_like(app.items(),[])23},2425})26}Edit this page on GitHubWas this helpful?YesNoPreviousRenderingNextDeploymentOn this pageA scenario through view methodsMocking a serviceTimeThrough the DOMWhat a Node test does not coverPractical notesNextType to search the documentation.