nx/e2e/react/src/react-router.test.ts
Nicholas Cunningham 1a235d7236
fix(react): react-router should work with jest out of the box (#30487)
Jest should be compatible with react-router out of the box.

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Currently, there are two issues when using `jest` with react-router out
of the box

1. Test files are not included from `tsconfig`
2. While running the test `jsdom` is missing Node's `TextEncoder` and
`TextDecoder` so compilation fails.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Running a test should work without issues when you create a react-router
app with Jest.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #30387
2025-03-26 10:44:28 -06:00

126 lines
4.9 KiB
TypeScript

import {
checkFilesExist,
cleanupProject,
ensureCypressInstallation,
newProject,
readFile,
runCLI,
uniq,
} from '@nx/e2e/utils';
describe('React Router Applications', () => {
describe('TS paths', () => {
const appName = uniq('app');
beforeAll(() => {
newProject({ packages: ['@nx/react'] });
ensureCypressInstallation();
runCLI(
`generate @nx/react:app ${appName} --use-react-router --routing --linter=eslint --unit-test-runner=vitest --no-interactive`
);
});
afterAll(() => cleanupProject());
it('should generate a react-router application', async () => {
const packageJson = JSON.parse(readFile('package.json'));
expect(packageJson.dependencies['react-router']).toBeDefined();
expect(packageJson.dependencies['@react-router/node']).toBeDefined();
expect(packageJson.dependencies['@react-router/serve']).toBeDefined();
expect(packageJson.dependencies['isbot']).toBeDefined();
checkFilesExist(`${appName}/app/app.tsx`);
checkFilesExist(`${appName}/app/entry.client.tsx`);
checkFilesExist(`${appName}/app/entry.server.tsx`);
checkFilesExist(`${appName}/app/routes.tsx`);
checkFilesExist(`${appName}/react-router.config.ts`);
checkFilesExist(`${appName}/vite.config.ts`);
});
it('should be able to build a react-router application', async () => {
const buildResult = runCLI(`build ${appName}`);
expect(buildResult).toContain('Successfully ran target build');
});
it('should be able to lint a react-router application', async () => {
const lintResult = runCLI(`lint ${appName}`);
expect(lintResult).toContain('Successfully ran target lint');
});
it('should be able to test and typecheck a react-router application', async () => {
const typeCheckResult = runCLI(`typecheck ${appName}`);
expect(typeCheckResult).toContain('Successfully ran target typecheck');
});
it('should be able to test and typecheck a react-router application with jest', async () => {
const jestApp = uniq('jestApp');
runCLI(
`generate @nx/react:app ${jestApp} --use-react-router --routing --unit-test-runner=jest --no-interactive`
);
const testResult = runCLI(`test ${jestApp}`);
expect(testResult).toContain('Successfully ran target test');
const typeCheckResult = runCLI(`typecheck ${jestApp}`);
expect(typeCheckResult).toContain('Successfully ran target typecheck');
});
});
describe('TS Solution', () => {
const appName = uniq('app');
beforeAll(() => {
newProject({ preset: 'ts', packages: ['@nx/react'] });
ensureCypressInstallation();
runCLI(
`generate @nx/react:app ${appName} --use-react-router --routing --linter=eslint --unit-test-runner=vitest --no-interactive`
);
});
afterAll(() => cleanupProject());
it('should generate a react-router application', async () => {
const packageJson = JSON.parse(readFile('package.json'));
expect(packageJson.dependencies['react-router']).toBeDefined();
expect(packageJson.dependencies['@react-router/node']).toBeDefined();
expect(packageJson.dependencies['@react-router/serve']).toBeDefined();
expect(packageJson.dependencies['isbot']).toBeDefined();
checkFilesExist(`${appName}/app/app.tsx`);
checkFilesExist(`${appName}/app/entry.client.tsx`);
checkFilesExist(`${appName}/app/entry.server.tsx`);
checkFilesExist(`${appName}/app/routes.tsx`);
checkFilesExist(`${appName}/react-router.config.ts`);
checkFilesExist(`${appName}/vite.config.ts`);
});
it('should be able to build a react-router application', async () => {
const buildResult = runCLI(`build ${appName}`);
expect(buildResult).toContain('Successfully ran target build');
});
it('should be able to lint a react-router application', async () => {
const lintResult = runCLI(`lint ${appName}`);
expect(lintResult).toContain('Successfully ran target lint');
});
it('should be able to test and typecheck a react-router application', async () => {
const testResult = runCLI(`test ${appName}`);
expect(testResult).toContain('Successfully ran target test');
const typeCheckResult = runCLI(`typecheck ${appName}`);
expect(typeCheckResult).toContain('Successfully ran target typecheck');
});
it('should be able to test and typecheck a react-router application with jest', async () => {
const jestApp = uniq('jestApp');
runCLI(
`generate @nx/react:app ${jestApp} --use-react-router --routing --unit-test-runner=jest --no-interactive`
);
const testResult = runCLI(`test ${jestApp}`);
expect(testResult).toContain('Successfully ran target test');
const typeCheckResult = runCLI(`typecheck ${jestApp}`);
expect(typeCheckResult).toContain('Successfully ran target typecheck');
});
});
});