Comments (11)
@nioe yes, I will make sure to run your reproduction case as well.
from stencil.
Thanks for your investigation and your answer.
As we did not customize the behaviour we came up with a solution that works for us:
We extended the E2EPage object and added a new function called findComponent
. It's similar to the original find
function but it waits for the given selector to have the hydrated
class.
Like this, the changes in our tests are rather small and it seems to work for us in all scenarios.
from stencil.
Thanks @nioe for reporting the issue. I will close this as
duplicate of #5457
And asked to add this issue into our next sprint for us to take a look. In the meantime please pin the stencil dependency to v4.12.1
. At the time where I worked on the PR I didn't recognised this would cause an issue like this.
from stencil.
Thanks @christian-bromann for the quick reply. I came across the other issue but figured that even if it's possibly the same root cause, it's a different problem which needs to be addressed.
It's ok for me to close my issue, as long as both problems get solved eventually.
from stencil.
Thanks a lot for your effort @christian-bromann.
from stencil.
@nioe sorry for the delayed response. I was able to find the root cause of this issue while understanding the details of similar issues reported to Stencil. In your reproduction case you have a global script that is being invoked when the application is loaded. This means that the component rendering is also delayed for the time being until the global script has finished. You can fix this problem using one of the following two options:
- delay test execution after you set the content until the global script is done, e.g.
diff --git a/src/components/my-component/my-component.e2e.ts b/src/components/my-component/my-component.e2e.ts
index d7ff311..fd3545b 100644
--- a/src/components/my-component/my-component.e2e.ts
+++ b/src/components/my-component/my-component.e2e.ts
@@ -5,6 +5,7 @@ describe('my-component', () => {
const page = await newE2EPage();
await page.setContent('<my-component></my-component>');
+ await new Promise(resolve => setTimeout(resolve, 1100));
const element = await page.find('my-component');
expect(element).toHaveClass('hydrated');
});
@@ -13,6 +14,7 @@ describe('my-component', () => {
const page = await newE2EPage();
await page.setContent('<my-component></my-component>');
+ await new Promise(resolve => setTimeout(resolve, 1100));
const component = await page.find('my-component');
const element = await page.find('my-component >>> div');
expect(element.textContent).toEqual(`Hello, World! I'm `);
- keep your global script synchronous and run the asynchronous part in an extra function:
diff --git a/src/global/global-script.ts b/src/global/global-script.ts
index 05d71d8..f156bf8 100644
--- a/src/global/global-script.ts
+++ b/src/global/global-script.ts
@@ -1,9 +1,13 @@
-const globalScript = async (): Promise<void> => {
- console.log('Global script started');
+const asyncGlobalScript = async (): Promise<void> => {
return new Promise<void>(resolve => setTimeout(() => {
- console.log('Global script ended');
+ console.log('Async Global script ended');
resolve();
}, 1000));
+}
+
+const globalScript = async (): Promise<void> => {
+ console.log('Global script started');
+ asyncGlobalScript();
};
-export default globalScript;
\ No newline at end of file
+export default globalScript;
I will check in with the team to see how we can be more explicit in our docs about this behavior.
from stencil.
Thank you for the detailed answer.
Both solutions seem kind of hacky to me.
Since the E2E Testing approach is baked into Stencil and an async global script is clearly mentioned as a valid case, I would assume that the testing framework handels this case automatically.
That said, our components do not function correctly as long as the async part of the global script has finished and waiting for an arbitrary amount of time in our tests will slow down our builds...
Both solutions are therefore not really practical for us.
Is there no way the the E2EPage could wait for the async global script to finish?
from stencil.
Is there no way the the E2EPage could wait for the async global script to finish?
Unfortunately not, once the file with the component source code is loaded, everything comes to an halt. I would strongly advise to use any global scripts with async procedures as these will also delay the rendering of the components in your application. Therefor I suggested to run an asyncGlobalScript
function without awaiting it.
from stencil.
Since the components add hydrated
to its class list once they are completely loaded, you could at least provide a helper method on the E2EPage
:
const waitForComponentToBeReady = (selector: string) => this.waitForSelector(`${selector}.hydrated`);
Like this it would be a rather simple change within our tests. After setting the page content we could call the wait function:
const page = await newE2EPage();
await page.setContent(`<my-component></my-component>`);
await page.waitForComponentToBeReady('my-component');
// test logic
What do you think?
from stencil.
@nioe that seems a viable idea to look into. I raised a PR but need to investigate why some end-to-end tests are failing.
from stencil.
@nioe unfortunately this can't be build into Stencil because it is not guaranteed that the element is hydrated with a .hydrated
class as the user can customize this behavior. I will raise a PR in the docs to better document this.
from stencil.
Related Issues (20)
- bug: css custom properties documentation is not generated on `stencil docs` HOT 1
- bug: beforeHydrateFn and afterHydrateFn are containing functions result instead of function references
- bug: chrome crashes with "Aw, snap" message HOT 2
- bug: Changes to the @Component decorator's styles do not trigger a hot module reload HOT 6
- bug: Cannot find module '@stencil/core/testing/jest-preset' after upgrading to Stencil 4.19.2 HOT 4
- bug: [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './screenshot' is not defined by "exports" in …/node_modules/@stencil/core/package.json HOT 1
- bug: dist-custom-elements generated types missing some types when run in GitHub Actions HOT 15
- bug: incorrect TestingConfig/JestConfig type HOT 1
- feat: make position of `<style>` tags configurable in `hydrateDocument()` HOT 4
- bug: Hydrate: serializeShadowRoot: false does not render content of the shadow dom of components. HOT 6
- bug: stencil stats does not give stats.json file if file path specified HOT 1
- bug: Adding style directly to children in customElement is not possible. HOT 3
- bug: can't stop propagation of react synthetic events HOT 1
- feat: Give the ability to stop Stencil from expanding types HOT 3
- bug: project builds failing after upgrading to 4.20.0 HOT 2
- bug: stencil test with `e2e` trigger documentation build HOT 1
- bug: All stencil components are imported in Angular even if only one is called HOT 7
- bug: HOT 4
- bug: Copy operation is not copying the file to a different file name HOT 1
- bug: building project fails after the first build HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stencil.