Skip to content

Commit

Permalink
fix(testing): allow removing projects in standalone apps
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Apr 5, 2023
1 parent e11e5d1 commit cb72b62
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable */
export default {
displayName: 'node-app',
preset: './jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: './coverage/node-app',
testMatch: [
'<rootDir>/src/**/__tests__/**/*.[jt]s?(x)',
'<rootDir>/src/**/*(*.)@(spec|test).[jt]s?(x)',
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ exports[`updateRootJestConfig should delete lib project ref from root jest confi
"
`;

exports[`updateRootJestConfig should delete project if root jest config is not a multi-project config 1`] = `
"/* eslint-disable */
export default {
displayName: 'node-app',
preset: './jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\\\\\\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: './coverage/node-app',
testMatch: [
'<rootDir>/src/**/__tests__/**/*.[jt]s?(x)',
'<rootDir>/src/**/*(*.)@(spec|test).[jt]s?(x)',
],
};
"
`;

exports[`updateRootJestConfig should not delete lib project ref from root jest config if there is no project jest config 1`] = `
"module.exports = {
projects: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('updateRootJestConfig', () => {

tree.write(
'jest.config.ts',
readFileSync(join(__dirname, './test-files/jest.config.ts'), 'utf-8')
readFileSync(join(__dirname, './__fixtures__/jest.config.ts'), 'utf-8')
);
});

Expand Down Expand Up @@ -71,4 +71,20 @@ describe('updateRootJestConfig', () => {

expect(rootJestConfig).toMatchSnapshot();
});

it('should delete project if root jest config is not a multi-project config', () => {
tree.write(
'jest.config.ts',
readFileSync(
join(__dirname, './__fixtures__/jest-project.config.ts'),
'utf-8'
)
);

updateJestConfig(tree, schema, readProjectConfiguration(tree, 'my-lib'));

const rootJestConfig = tree.read('jest.config.ts', 'utf-8');

expect(rootJestConfig).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ function isUsingUtilityFunction(host: Tree) {
return host.read('jest.config.ts').toString().includes('getJestProjects()');
}

/**
* in a standalone project, the root jest.config.ts is a project config instead
* of multi-project config.
* in that case we do not need to edit it to remove it
**/
function isMonorepoConfig(tree: Tree) {
return tree.read('jest.config.ts', 'utf-8').includes('projects:');
}

/**
* Updates the root jest config projects array and removes the project.
*/
Expand All @@ -44,7 +53,8 @@ export function updateJestConfig(
if (
!tree.exists('jest.config.ts') ||
!tree.exists(join(projectConfig.root, 'jest.config.ts')) ||
isUsingUtilityFunction(tree)
isUsingUtilityFunction(tree) ||
!isMonorepoConfig(tree)
) {
return;
}
Expand Down

0 comments on commit cb72b62

Please sign in to comment.