angular-cli/packages/schematics/angular/migrations/update-14/replace-default-collection-option_spec.ts
Alan Agius 366cabc66c feat(@angular/cli): add support for multiple schematics collections
The `schematicCollections` can be placed under the `cli` option in the global `.angular.json` configuration, at the root or at project level in `angular.json` .

```jsonc
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "cli": {
    "schematicCollections": ["@schematics/angular", "@angular/material"]
  }
  // ...
}
```

**Rationale**
When this option is not configured and a user would like to run a schematic which is not part of `@schematics/angular`,
the collection name needs to be provided to `ng generate` command in the form of `[collection-name:schematic-name]`. This make the `ng generate` command too verbose for repeated usages.

This is where `schematicCollections` comes handle. When adding `@angular/material` to the list of `schematicCollections`, the generate command will try to locate the schematic in the specified collections.

```
ng generate navigation
```

is equivalent to:

```
ng generate @angular/material:navigation
```

**Conflicting schematic names**
When multiple collections have a schematic with the same name. Both `ng generate` and `ng new` will run the first schematic matched based on the ordering (as specified) of `schematicCollections`.

DEPRECATED:

The `defaultCollection` workspace option has been deprecated in favor of `schematicCollections`.

Before
```json
"defaultCollection": "@angular/material"
```

After
```json
"schematicCollections": ["@angular/material"]
```

Closes #12157
2022-03-22 09:37:57 -07:00

102 lines
3.3 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { ProjectType, WorkspaceSchema } from '../../utility/workspace-models';
describe(`Migration to replace 'defaultCollection' option.`, () => {
const schematicName = 'replace-default-collection-option';
const schematicRunner = new SchematicTestRunner(
'migrations',
require.resolve('../migration-collection.json'),
);
let tree: UnitTestTree;
beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
});
it(`should replace 'defaultCollection' with 'schematicCollections' at the root level`, async () => {
const angularConfig: WorkspaceSchema = {
version: 1,
projects: {},
cli: {
defaultCollection: 'foo',
},
};
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { cli } = JSON.parse(newTree.readContent('/angular.json'));
expect(cli.defaultCollection).toBeUndefined();
expect(cli.schematicCollections).toEqual(['foo']);
});
it(`should not error when 'cli' is not defined`, async () => {
const angularConfig: WorkspaceSchema = {
version: 1,
projects: {},
};
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { cli } = JSON.parse(newTree.readContent('/angular.json'));
expect(cli).toBeUndefined();
});
it(`should replace 'defaultCollection' with 'schematicCollections' at the project level`, async () => {
const angularConfig: WorkspaceSchema = {
version: 1,
cli: {
defaultCollection: 'foo',
},
projects: {
test: {
sourceRoot: '',
root: '',
prefix: '',
projectType: ProjectType.Application,
cli: {
defaultCollection: 'bar',
},
},
},
};
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const {
projects: { test },
} = JSON.parse(newTree.readContent('/angular.json'));
expect(test.cli.defaultCollection).toBeUndefined();
expect(test.cli.schematicCollections).toEqual(['bar']);
});
it(`should not replace 'defaultCollection' with 'schematicCollections', when it is already defined`, async () => {
const angularConfig: WorkspaceSchema = {
version: 1,
projects: {},
cli: {
defaultCollection: 'foo',
schematicCollections: ['bar'],
},
};
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { cli } = JSON.parse(newTree.readContent('/angular.json'));
expect(cli.defaultCollection).toBeUndefined();
expect(cli.schematicCollections).toEqual(['bar']);
});
});