Skip to content

Commit

Permalink
feat(schematics): option to skip creating tests for components (#1663)
Browse files Browse the repository at this point in the history
* You can skip creating tests with `--skip-tests`
  • Loading branch information
dhhyi authored May 22, 2024
1 parent 6ac30c3 commit 5082b78
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/guides/customizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ If possible, use the Jest update feature **update snapshots** when adapting test
When you upgrade the PWA to a new version, those snapshots will most likely have merge conflicts in them.
Here you can just accept either modification and update the test snapshots.

You can disable creating component tests by adding the following snippet to your `angular.json`:

```json
"projects": {
"intershop-pwa": {
"schematics": {
"intershop-schematics:component": {
"skipTests": true
}
},
```

### Styling

Changing the styling by applying changes to SCSS files should be done in the custom theme folder `src/styles/themes/<theme-prefix>`.
Expand Down
10 changes: 8 additions & 2 deletions schematics/src/component/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
filter,
mergeWith,
move,
noop,
url,
} from '@angular-devkit/schematics';
import { PWAComponentOptionsSchema as Options } from 'schemas/component/schema';
Expand Down Expand Up @@ -37,7 +36,14 @@ export function createComponent(options: Options): Rule {
operations.push(
mergeWith(
apply(url('./files'), [
options.styleFile ? noop() : filter(path => !path.includes('.scss')),
filter(path => {
if (path.endsWith('.scss.template')) {
return options.styleFile;
} else if (path.endsWith('.spec.ts.template')) {
return !options.skipTests;
}
return true;
}),
applyTemplates({
...strings,
...options,
Expand Down
11 changes: 11 additions & 0 deletions schematics/src/component/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ describe('Component Schematic', () => {
expect(componentSource).toContain(`styleUrls: ['./foo.component.scss']`);
});

it('should create a component without test file if requested', async () => {
const options = { ...defaultOptions, skipTests: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
expect(tree.files.filter(x => x.search('foo.component') >= 0)).toMatchInlineSnapshot(`
[
"/src/app/foo/foo.component.html",
"/src/app/foo/foo.component.ts",
]
`);
});

it('should find the closest module', async () => {
const options = { ...defaultOptions };
const fooModule = '/src/app/foo/foo.module.ts';
Expand Down
5 changes: 5 additions & 0 deletions schematics/src/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
"description": "When true, does not import this component into the owning NgModule.",
"default": false
},
"skipTests": {
"type": "boolean",
"description": "When true, does not create test.",
"default": false
},
"export": {
"type": "boolean",
"default": false,
Expand Down

0 comments on commit 5082b78

Please sign in to comment.