fix(js): infer typecheck task for buildable libraries with tsc (#30505)

## Current Behavior

Buildable libraries using `tsc` for `build` don't get a `typecheck`
task, which results in test files not being type-checked.

## Expected Behavior

The `typecheck` task should be inferred, and test files should be
type-checked.

## Related Issue(s)

Fixes #
This commit is contained in:
Leosvel Pérez Espinosa 2025-03-27 09:35:17 +01:00 committed by GitHub
parent 1a235d7236
commit e4e9973db3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 12 additions and 38 deletions

View File

@ -132,16 +132,19 @@ ${content}`
// check typecheck
expect(runCLI(`typecheck ${esbuildParentLib}`)).toContain(
`Successfully ran target typecheck for project @proj/${esbuildParentLib} and 4 tasks it depends on`
`Successfully ran target typecheck for project @proj/${esbuildParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${rollupParentLib}`)).toContain(
`Successfully ran target typecheck for project @proj/${rollupParentLib} and 4 tasks it depends on`
`Successfully ran target typecheck for project @proj/${rollupParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${swcParentLib}`)).toContain(
`Successfully ran target typecheck for project @proj/${swcParentLib} and 4 tasks it depends on`
`Successfully ran target typecheck for project @proj/${swcParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${tscParentLib}`)).toContain(
`Successfully ran target typecheck for project @proj/${tscParentLib} and 5 tasks it depends on`
);
expect(runCLI(`typecheck ${viteParentLib}`)).toContain(
`Successfully ran target typecheck for project @proj/${viteParentLib} and 4 tasks it depends on`
`Successfully ran target typecheck for project @proj/${viteParentLib} and 5 tasks it depends on`
);
// check lint

View File

@ -106,6 +106,7 @@ describe('Node Applications', () => {
expect(() => runCLI(`lint ${nodelib}`)).not.toThrow();
expect(() => runCLI(`test ${nodelib}`)).not.toThrow();
expect(() => runCLI(`build ${nodelib}`)).not.toThrow();
expect(() => runCLI(`typecheck ${nodelib}`)).not.toThrow();
const p = await runCommandUntil(
`serve ${nodeapp}`,

View File

@ -47,6 +47,9 @@ describe('Nx Plugin (TS solution)', () => {
expect(runCLI(`lint @proj/${plugin}`)).toContain(
`Successfully ran target lint for project @proj/${plugin}`
);
expect(runCLI(`typecheck @proj/${plugin}`)).toContain(
`Successfully ran target typecheck for project @proj/${plugin}`
);
expect(runCLI(`build @proj/${plugin}`)).toContain(
`Successfully ran target build for project @proj/${plugin}`
);

View File

@ -106,7 +106,7 @@ ${content}`
// check typecheck
expect(runCLI(`typecheck ${reactApp}`)).toContain(
`Successfully ran target typecheck for project @proj/${reactApp} and 5 tasks it depends on`
`Successfully ran target typecheck for project @proj/${reactApp} and 6 tasks it depends on`
);
}, 300_000);
});

View File

@ -2347,24 +2347,6 @@ describe('lib', () => {
]);
});
it('should add nx.addTypecheckTarget to tsconfig.json when using tsc to build to avoid duplicated typechecks', async () => {
await libraryGenerator(tree, {
...defaultOptions,
useProjectJson: false,
directory: 'my-ts-lib',
bundler: 'tsc',
unitTestRunner: 'none',
linter: 'none',
});
expect(readJson(tree, 'my-ts-lib/tsconfig.json').nx)
.toMatchInlineSnapshot(`
{
"addTypecheckTarget": false,
}
`);
});
it('should set "nx.name" in package.json when the user provides a name that is different than the package name and "useProjectJson" is "false"', async () => {
await libraryGenerator(tree, {
...defaultOptions,

View File

@ -1057,12 +1057,6 @@ function createProjectTsConfigs(
json.references.push({
path: './tsconfig.lib.json',
});
// If using `tsc` to build, then we do not want a typecheck target that duplicates the work, since both run `tsc`.
// This applies to `@nx/js/typescript` plugin only.
if (options.bundler === 'tsc') {
json['nx'] ??= {};
json['nx'].addTypecheckTarget = false;
}
return json;
});
} else {
@ -1073,12 +1067,6 @@ function createProjectTsConfigs(
include: [],
references: [{ path: './tsconfig.lib.json' }],
};
// If using `tsc` to build, then we do not want a typecheck target that duplicates the work, since both run `tsc`.
// This applies to `@nx/js/typescript` plugin only.
if (options.bundler === 'tsc') {
tsconfig['nx'] ??= {};
tsconfig['nx'].addTypecheckTarget = false;
}
writeJson(
tree,
joinPathFragments(options.projectRoot, 'tsconfig.json'),

View File

@ -466,9 +466,6 @@ describe('NxPlugin Plugin Generator', () => {
"extends": "../tsconfig.base.json",
"files": [],
"include": [],
"nx": {
"addTypecheckTarget": false,
},
"references": [
{
"path": "./tsconfig.lib.json",