Skip to content

Commit

Permalink
fix(devkit): handle missing include and exclude in tsconfig optio…
Browse files Browse the repository at this point in the history
…ns when updating tsconfig to support js
  • Loading branch information
leosvelperez committed Feb 17, 2025
1 parent 46cac5b commit cac52fc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
90 changes: 90 additions & 0 deletions packages/devkit/src/generators/update-ts-configs-to-js.spec.ts
Original file line number Diff line number Diff line change
@@ -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"
]
}
"
`);
}
);
});
4 changes: 2 additions & 2 deletions packages/devkit/src/generators/update-ts-configs-to-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand Down

0 comments on commit cac52fc

Please sign in to comment.