Closed silbinarywolf closed 5 years ago
It is difficult to suggest any workaround without seeing the code for unit testing. However, there are a few things you can try as workaround.
manuallyHandleLifecycle
to have a better control on the lifecycle, if needed.function wait(timeInMs = 1000) {
return new Promise((resolve) => { setTimeout(() => { resolve(); }, timeInMs); });
}
Then, you can use it as follows:
await component.bind(...);
await wait();
Hope this helps.
Thanks for the advice! I opted to go with 1.
My current solution (which I literally just created a few mins ago, so might not be robust) is to do something like this.
Im not sure why simply using a few promises gives it enough time and I might want to add some code to guarantee that the
function workaroundComposeMountIssue(): Promise<void> {
return component.manuallyHandleLifecycle().create(bootstrap)
.then(() => (component as any).bind())
.then(() => {
component.attached()
});
};
it('trying to get tests working', (done) => {
workaroundComposeMountIssue().then(() => {
expect(component.viewModel.value).toBe('2018-09-19');
done();
})
});
@silbinarywolf By doing so, you are basically telling jasmine to wait. Or in other words, your assertions are only executed once the previous promises (involving the lifecycle hooks of the custom element) are resolved.
@silbinarywolf can this be closed?
Sure. I'll close it.
I'm submitting a bug report
Library Version:
Please tell us about your environment:
Operating System: Windows 10
Node Version: 8.11.1
Yarn Version: 1.5.1
JSPM OR Webpack AND Version JSPM 0.16.32 | webpack 2.1.0-beta.17
Browser: N/A
Language: TypeScript 3.0.1
Current behavior:
attached()
method. I confirm this by doingelement.innerHTML
, whereelement
is an @inject DOM.Element.attached()
method is called. By applying some logging toaurelia-loader-nodejs
, I determined that thisExpected/desired behavior:
Example code (stripped down)
In the browser, my
attached()
method works fine. However in Jest, it does not and my error code is thrown.datepicker.ts
field.html
datepicker.html (ie. fieldView)
Console output when running Jest
As you can see from the stack trace below, my
throw new Error
code executes and then NodeJsLoader.prototype.loadModule() is called.Modified NodeJsLoader
node_modules\aurelia-loader-nodejs\dist\commonjs\aurelia-loader-nodejs.js
Related issues: https://github.com/aurelia/templating-resources/issues/64#issuecomment-147269442