From cac52fcf0269ffde248006577ec537c6d7b04792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Mon, 17 Feb 2025 17:05:07 +0100 Subject: [PATCH] fix(devkit): handle missing `include` and `exclude` in tsconfig options when updating tsconfig to support js --- .../update-ts-configs-to-js.spec.ts | 90 +++++++++++++++++++ .../src/generators/update-ts-configs-to-js.ts | 4 +- 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 packages/devkit/src/generators/update-ts-configs-to-js.spec.ts diff --git a/packages/devkit/src/generators/update-ts-configs-to-js.spec.ts b/packages/devkit/src/generators/update-ts-configs-to-js.spec.ts new file mode 100644 index 0000000000000..79db3af92b5ab --- /dev/null +++ b/packages/devkit/src/generators/update-ts-configs-to-js.spec.ts @@ -0,0 +1,90 @@ +import type { Tree } from 'nx/src/generators/tree'; +import { updateTsConfigsToJs } from './update-ts-configs-to-js'; +import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace'; + +describe('updateTsConfigsToJs', () => { + let tree: Tree; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + it('should set allowJs to true in tsconfig.json', () => { + tree.write('tsconfig.json', `{}`); + tree.write('tsconfig.lib.json', `{}`); + + updateTsConfigsToJs(tree, { projectRoot: '.' }); + + expect(tree.read('tsconfig.json', 'utf-8')).toMatchInlineSnapshot(` + "{ + "compilerOptions": { + "allowJs": true + } + } + " + `); + }); + + it.each` + tsconfig + ${'tsconfig.app.json'} + ${'tsconfig.lib.json'} + `( + 'should add the relevant include and exclude to $tsconfig', + ({ tsconfig }) => { + tree.write('tsconfig.json', `{}`); + tree.write( + tsconfig, + JSON.stringify({ + include: ['src/**/*.ts'], + exclude: ['src/**/*.spec.ts', 'src/**/*.test.ts'], + }) + ); + + updateTsConfigsToJs(tree, { projectRoot: '.' }); + + expect(tree.read(tsconfig, 'utf-8')).toMatchInlineSnapshot(` + "{ + "include": [ + "src/**/*.ts", + "src/**/*.js" + ], + "exclude": [ + "src/**/*.spec.ts", + "src/**/*.test.ts", + "src/**/*.spec.js", + "src/**/*.test.js" + ] + } + " + `); + } + ); + + it.each` + tsconfig + ${'tsconfig.app.json'} + ${'tsconfig.lib.json'} + `( + 'should update $tsconfig with the relevant include and exclude when those properties are not defined', + ({ tsconfig }) => { + tree.write('tsconfig.json', `{}`); + tree.write(tsconfig, `{}`); + + updateTsConfigsToJs(tree, { projectRoot: '.' }); + + expect(tree.read(tsconfig, 'utf-8')).toMatchInlineSnapshot(` + "{ + "include": [ + "src/**/*.js" + ], + "exclude": [ + "src/**/*.spec.js", + "src/**/*.test.js" + ] + } + " + `); + } + ); +}); diff --git a/packages/devkit/src/generators/update-ts-configs-to-js.ts b/packages/devkit/src/generators/update-ts-configs-to-js.ts index 310f592f78288..e89941459b0a8 100644 --- a/packages/devkit/src/generators/update-ts-configs-to-js.ts +++ b/packages/devkit/src/generators/update-ts-configs-to-js.ts @@ -31,9 +31,9 @@ export function updateTsConfigsToJs( if (updateConfigPath) { updateJson(tree, updateConfigPath, (json) => { - json.include = uniq([...json.include, 'src/**/*.js']); + json.include = uniq([...(json.include ?? []), 'src/**/*.js']); json.exclude = uniq([ - ...json.exclude, + ...(json.exclude ?? []), 'src/**/*.spec.js', 'src/**/*.test.js', ]);