Demo test code here:
https://gist.dumber.app/?gist=79b0cbde9118a15c467357c381bf9201&open=test%2Fhelper.ts&open=test%2Fapp.spec.ts
What changed
In this repo, current unit tests template renders into html body, which requires clean up.
We can instead render into a detached <div>, so that JavaScript GC will clean it up automatically.
I want to ask whether this works in Aurelia 2 as I expected.
export async function render(template: string, ...deps: readonly unknown[]) {
const wrapper = CustomElement.define({name: 'wrapper', template});
const div = document.createElement('div');
const au = Aurelia.register(deps).app({
host: div,
component: wrapper
});
await au.start().wait();
return div;
}
Usage is simple:
const div = await render('<my-app></my-app>', MyApp);
expect(div.textContent.trim()).to.equal('Hello Aurelia 2!');
Note, there is no au.stop() to clean up. As const div will be garbage collected by JS, au instance should follow it to annihilation.
Enhance @aurelia/testing?
If this is a good idea, we probably should add some helper to @aurelia/testing, something like render() or mount() (these two are very common in other frameworks' test support) to enable easy writing of test code.
Demo test code here:
https://gist.dumber.app/?gist=79b0cbde9118a15c467357c381bf9201&open=test%2Fhelper.ts&open=test%2Fapp.spec.ts
What changed
In this repo, current unit tests template renders into html body, which requires clean up.
We can instead render into a detached
<div>, so that JavaScript GC will clean it up automatically.I want to ask whether this works in Aurelia 2 as I expected.
Usage is simple:
Note, there is no
au.stop()to clean up. Asconst divwill be garbage collected by JS,auinstance should follow it to annihilation.Enhance
@aurelia/testing?If this is a good idea, we probably should add some helper to
@aurelia/testing, something likerender()ormount()(these two are very common in other frameworks' test support) to enable easy writing of test code.