-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to include css while running tests in CRA? #10483
Comments
Hi @petermarcoen, I've had a look at the demo you provided (thanks for that) and you're right. It looks like the test failure is actually correct. You need to first open the accordion before you can find the button with RTL. it("renders", async () => {
render(<App />);
// Get the first header (button) and click to open the accordion
const firstHeader = screen.getAllByText("Click me!")[0];
fireEvent.click(firstHeader);
// Fails only if the above import of bootstrap.min.css is there
expect(await screen.findByRole("button", { name: /Secret button/ }));
}); You can create a test setup file and import your CSS there (so you don't have to do it per test). Without the CSS, it looks like the below which is why the current test passes when you remove the CSS. I hope this helps, please let me know if you need any further help here! |
Hi @mrmckeb, thank you for taking a look at this. Could you please take a look at my create-react-app example at https://github.com/petermarcoen/cra-css-problem? The exact steps I took for creating this example are the following:
When I start my app I see the Accordion collapsed as it should be. |
Thanks @petermarcoen, sorry I misunderstood your original post. I've reproduced this, and can see that when running this in both environments, I see CSS in the head on CodeSandbox, and not locally. I even pulled the CodeSandbox project and still saw the same result.
I'll ask others in the team if they have any ideas, as I can't seem to find the cause here - and I'm not 100% sure on what the expected behaviour is. |
I am still confused. But in my opinion it will pass because bootstrap just hides your secret button (meaning its there in DOM but just hidden using css Check how to setup jest-dom here if not done already Check this sandbox I am searching for your secret button using NOTE: Bootstrap does not do conditional render. |
Hi @technikhil314, thank you for your time. In your example the test succeeds but I actually want this test to fail. This must be possible because in my codesandbox example it correctly marks the test as failed, since the element is not visible. |
You are right you should check for And by fail I am assuming you are expecting the secret button not visible to user. |
Hi @technikhil314, thanks for taking your time with this issue. It does work correctly on Codesandbox: |
@petermarcoen, I've been talking to the team and we're all confused by this, as none of us expected this to work. For a long time, we've transformed CSS (and CSS modules) with this simple transform: I also noted the flag in the test script, but this again doesn't modify the behaviour above. I saw no difference with that flag on or off.
We believe this difference is actually because CodeSandbox are running the tests in a browser, you can see If you want to test proper CSS behaviour, I'd recommend looking at Puppeteer or Playwright (the latter supports Webkit). Both have Jest integrations, and there's also the new Playwright test runner. Another option would be Cypress. That being said, if you have the time and are interested in trying to solve this for CRA, let me know! |
Hi @mrmckeb, thank you for your effort! As a workaround I now test for the CSS classnames themselves (see code below). Not sure if you want to close this issue or leave it open for a better solution in the future. App.test.js
App.js
|
Hmm I missed the line which says |
@petermarcoen @mrmckeb Its indeed an issue with jsdom it seem the cascade of css is not working well with @tsting-library/jest-dom package which is internally using jsdom. See more here testing-library/jest-dom#209 |
We can leave this open for now @petermarcoen. I agree (as does the community) that it's less than ideal to test based on classnames... It would be great to solve this in future - I'll see if I can make some time in the next weeks to rethink this. |
Thanks for jumping in @technikhil314. I'll also take a look through RTL issues in the coming weeks and see if anything sticks out. |
@mrmckeb I too would like to contribute to thie repo.It would be great if you can please give me some pointers to start looking into it? |
Hi @technikhil314, that would be great. I haven't given it too much thought yet, but you can see here that we're simply throwing away the CSS today. You can see the difference in the |
Seems like we need a css and scss transformer for jest. Although jest's cache will kick in but I feel it will hamper test runners performance. WDYT? @mrmckeb |
Hey @mrmckeb I changed the // @remove-on-eject-begin
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @remove-on-eject-end
'use strict';
// This is a custom Jest transformer turning style imports into empty objects.
// http://facebook.github.io/jest/docs/en/webpack.html
module.exports = {
process(fileContent) {
console.log("object");
const demo = `module.exports = (function demo() {
const style = document.createElement("style");
style.setAttribute("type", "text/css");
style.innerHTML = "${fileContent.replace(/[\n]/g, " ")}";
document.body.appendChild(style);
return null;
})()`;
console.log(demo);
return demo;
},
getCacheKey(fileContent, fileAbsolutePath) {
// The output is always the same.
return fileAbsolutePath;
},
}; And its working for |
When I test a component containing a React Bootstrap Accordion component I cannot check whether or not the Accordion Cards are opened or closed. According to my tests they are always visible, even if they shouldn't.
I strongly suspect that this is because the bootstrap css is not considered.
The React Bootstrap Accordion component uses css classes to show or hide the content and I think that the test is not including this css, even though I import it in the test class.
The weird thing is, this does work in codesandbox:
https://codesandbox.io/s/ecstatic-platform-tt3on?file=/src/App.test.js
So I think this has something to do with CRA or webpack.
I tried the exact same thing as in the codesandbox in a brand new CRA app and I verified that the libraries are the same exact version (react-scripts 4.0.1).
Here are the relevant bits:
App.js
App.test.js
If I remove the import of the bootstrap.min.css in the file above the test will always succeed, even on codesandbox but with this import included it should fail which it does in codesandbox but not in my create-react-app
The text was updated successfully, but these errors were encountered: