Leosvel Pérez Espinosa d454f25aad
feat(testing): update test generators to exclude test files from the runtime tsconfig file (#27991)
<!-- 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 -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

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

Fixes #
2024-09-23 09:25:12 -04:00

72 lines
2.2 KiB
TypeScript

import {
joinPathFragments,
logger,
readProjectConfiguration,
updateJson,
type Tree,
} from '@nx/devkit';
import type { NormalizedJestProjectSchema } from '../schema';
export function updateTsConfig(
host: Tree,
options: NormalizedJestProjectSchema
) {
const { root, projectType } = readProjectConfiguration(host, options.project);
if (!host.exists(joinPathFragments(root, 'tsconfig.json'))) {
throw new Error(
`Expected ${joinPathFragments(
root,
'tsconfig.json'
)} to exist. Please create one.`
);
}
updateJson(host, joinPathFragments(root, 'tsconfig.json'), (json) => {
if (
json.references &&
!json.references.some((r) => r.path === './tsconfig.spec.json')
) {
json.references.push({
path: './tsconfig.spec.json',
});
}
return json;
});
// fall-back runtime tsconfig file path in case the user didn't provide one
let runtimeTsconfigPath = joinPathFragments(
root,
projectType === 'application' ? 'tsconfig.app.json' : 'tsconfig.lib.json'
);
if (options.runtimeTsconfigFileName) {
runtimeTsconfigPath = joinPathFragments(
root,
options.runtimeTsconfigFileName
);
if (!host.exists(runtimeTsconfigPath)) {
// the user provided a runtimeTsconfigFileName that doesn't exist, so we throw an error
throw new Error(
`Cannot find the provided runtimeTsConfigFileName ("${options.runtimeTsconfigFileName}") at the project root "${root}".`
);
}
}
if (host.exists(runtimeTsconfigPath)) {
updateJson(host, runtimeTsconfigPath, (json) => {
const uniqueExclude = new Set([
...(json.exclude || []),
options.js ? 'jest.config.js' : 'jest.config.ts',
'src/**/*.spec.ts',
'src/**/*.test.ts',
...(options.js ? ['src/**/*.spec.js', 'src/**/*.test.js'] : []),
]);
json.exclude = [...uniqueExclude];
return json;
});
} else {
logger.warn(
`Couldn't find a runtime tsconfig file at ${runtimeTsconfigPath} to exclude the test files from. ` +
`If you're using a different filename for your runtime tsconfig, please provide it with the '--runtimeTsconfigFileName' flag.`
);
}
}