Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): add root karma config if one isn't present for karma-project generator #9485

Merged
merged 1 commit into from Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,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;