@nrwl/react/plugins/component-testing is lacking stuff from react/plugins/webpack
See original GitHub issueCurrent Behavior
@nrwl/react/plugins/component-testing
is lacking webpack config parts that exists in @nrwl/react/plugins/webpack
Which leads to that code that builds and runs using regular react webpack setup does not build with it out of the box.
Mostly the svgr loader is missing, hot reload, and maybe more.
Also there is something fishy with the imports, see the workaround to understand what’s fishy about them.
Expected Behavior
Code that works with@nrwl/react/plugins/webpack
should just work with the @nrwl/react/plugins/component-testing
preset!
My workaround cypress.config.ts
:
import { nxComponentTestingPreset } from '@nrwl/react/plugins/component-testing';
import * as getWebpackConfigTypeIsBroken from '@nrwl/react/plugins/webpack';
import { defineConfig } from 'cypress';
import type { Configuration } from 'webpack';
const component = nxComponentTestingPreset(__dirname);
fixPartialWebpackConfig(component.devServer.webpackConfig);
export default defineConfig({
component,
});
/**
The webpack config in @nrwl/react/plugins/component-testing is incomplete
Until it will be fixed, we manually add augment it using @nrwl/react/plugins/webpack
But unfortunately there is a miss-match in the es transpile module configurations and importing it in the normal way is broken,
so we have a workaround also for that
*/
function fixPartialWebpackConfig(webpackConfig: Configuration) {
// The exports here are wacked https://github.com/nrwl/nx/blob/12f0f19/packages/react/plugins/webpack.ts
// mix of ES export + module.exports
const getWebpackConfig: typeof getWebpackConfigTypeIsBroken.getWebpackConfig =
// @ts-expect-error see above comments
getWebpackConfigTypeIsBroken.default;
const svgToString = /\.svg$/.toString();
const doWeHaveSvgLoader = webpackConfig.module?.rules?.some((rule) => {
if (typeof rule === 'string') {
return false;
} else {
return rule.test?.toString() === svgToString;
}
});
/* Kill switch, maybe this is not longer needed */
if (doWeHaveSvgLoader) {
throw new Error('---fixPartialWebpackConfig is may no longer needed---');
}
// this function actually mutates the config in-place
getWebpackConfig(webpackConfig);
}
Worth noting that my workaround does not make fast refresh to work and maybe more issues.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top Results From Across the Web
The React CLI you always wanted but didn't know about
In this article, I'd like to specifically talk about developer tooling, why it is so massively important and how you might have missed...
Read more >@nrwl/react:component-test | Nx
This generator is used to create a Cypress component test file for a given React component. nx g @nrwl/react:component-test --project=my-cool-react ...
Read more >Nx/community - nrwl - Gitter
I just use a standard nx layout which seems to use "webpackConfig": "@nrwl/react/plugins/webpack" as webpack config. Manuel Bojato. @KingDarBoja. I am ...
Read more >Enable Hot Module Replacement(HMR) in Nrwl/Nx React ...
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload.
Read more >Effective React Development with Nx
Lack of consistency in linting, testing, and release processes. ... provide technology specific features (such as @nrwl/react for React ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@barbados-clemens Apologies, I’ve made the repo public now. I’m using 14.7.4 there, so I believe the issue is still persisting.
PR for loading custom webpacks