Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(testing): add root karma config if one isn't present when using k…
…arma-project generator (#9485)

when using the karma-project generator directory ensure the karma generator (install deps/root
config) is called if the root karma config isn't present

ISSUES CLOSED: #9234
  • Loading branch information
barbados-clemens committed Mar 25, 2022
1 parent 167b57a commit c12a647
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 22 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/angular.json
Expand Up @@ -661,6 +661,11 @@
"description": "Skip formatting files.",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Skip updating package.json.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down
Expand Up @@ -41,11 +41,13 @@ describe('karmaProject', () => {
});

it('should generate files', async () => {
expect(tree.exists('karma.conf.js')).toBeFalsy();
await karmaProjectGenerator(tree, { project: 'lib1' });

expect(tree.exists('/libs/lib1/karma.conf.js')).toBeTruthy();
expect(tree.exists('/libs/lib1/tsconfig.spec.json')).toBeTruthy();
expect(tree.exists('/libs/lib1/src/test.ts')).toBeTruthy();
expect(tree.exists('karma.conf.js')).toBeTruthy();
});

it('should create a karma.conf.js', async () => {
Expand Down
Expand Up @@ -5,11 +5,13 @@ import { generateKarmaProjectFiles } from './lib/generate-karma-project-files';
import { updateTsConfigs } from './lib/update-tsconfig';
import { updateWorkspaceConfig } from './lib/update-workspace-config';
import type { KarmaProjectOptions } from './schema';
import { karmaGenerator } from '../karma/karma';

export async function karmaProjectGenerator(
tree: Tree,
options: KarmaProjectOptions
) {
karmaGenerator(tree, options);
checkProjectTestTarget(tree, options.project);
generateKarmaProjectFiles(tree, options.project);
updateTsConfigs(tree, options.project);
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/karma-project/schema.d.ts
@@ -1,4 +1,5 @@
export interface KarmaProjectOptions {
project: string;
skipFormat?: boolean;
skipPackageJson?: boolean;
}
5 changes: 5 additions & 0 deletions packages/angular/src/generators/karma-project/schema.json
Expand Up @@ -17,6 +17,11 @@
"description": "Skip formatting files.",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Skip updating package.json.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down
18 changes: 16 additions & 2 deletions packages/angular/src/generators/karma/karma.spec.ts
Expand Up @@ -9,20 +9,34 @@ describe('karma', () => {
tree = createTreeWithEmptyWorkspace();
});

it('should do nothing when karma is already installed', () => {
it('should do nothing when karma is already installed and karma.conf.js exists', () => {
jest.spyOn(devkit, 'generateFiles');
jest.spyOn(devkit, 'addDependenciesToPackageJson');
devkit.updateJson(tree, 'package.json', (json) => {
json.devDependencies = { karma: '~5.0.0' };
return json;
});

tree.write('karma.conf.js', '');
karmaGenerator(tree, {});

expect(devkit.generateFiles).not.toHaveBeenCalled();
expect(devkit.addDependenciesToPackageJson).not.toHaveBeenCalled();
});

it('should create karma.conf.js when karma is installed', () => {
jest.spyOn(devkit, 'generateFiles');
jest.spyOn(devkit, 'addDependenciesToPackageJson');
devkit.updateJson(tree, 'package.json', (json) => {
json.devDependencies = { karma: '~5.0.0' };
return json;
});

karmaGenerator(tree, {});

expect(devkit.generateFiles).toHaveBeenCalled();
expect(devkit.addDependenciesToPackageJson).not.toHaveBeenCalled();
});

it('should add karma dependencies', () => {
karmaGenerator(tree, {});

Expand Down
42 changes: 22 additions & 20 deletions packages/angular/src/generators/karma/karma.ts
Expand Up @@ -9,28 +9,30 @@ import { GeneratorOptions } from './schema';

export function karmaGenerator(tree: Tree, options: GeneratorOptions) {
const packageJson = readJson(tree, 'package.json');
if (packageJson.devDependencies['karma']) {
return;
}

generateFiles(tree, joinPathFragments(__dirname, 'files'), '.', { tmpl: '' });
if (!tree.exists('karma.conf.js')) {
generateFiles(tree, joinPathFragments(__dirname, 'files'), '.', {
tmpl: '',
});
}

return !options.skipPackageJson
? addDependenciesToPackageJson(
tree,
{},
{
karma: '~6.3.0',
'karma-chrome-launcher': '~3.1.0',
'karma-coverage': '~2.2.0',
'karma-jasmine': '~4.0.0',
'karma-jasmine-html-reporter': '~1.7.0',
'jasmine-core': '~3.10.0',
'jasmine-spec-reporter': '~5.0.0',
'@types/jasmine': '~3.5.0',
}
)
: () => {};
if (options.skipPackageJson || packageJson.devDependencies['karma']) {
return () => {};
}
return addDependenciesToPackageJson(
tree,
{},
{
karma: '~6.3.0',
'karma-chrome-launcher': '~3.1.0',
'karma-coverage': '~2.2.0',
'karma-jasmine': '~4.0.0',
'karma-jasmine-html-reporter': '~1.7.0',
'jasmine-core': '~3.10.0',
'jasmine-spec-reporter': '~5.0.0',
'@types/jasmine': '~3.5.0',
}
);
}

export default karmaGenerator;

0 comments on commit c12a647

Please sign in to comment.