Skip to content

Commit

Permalink
feat(schematics): introduce unitTestRunner option to support Vitest (#…
Browse files Browse the repository at this point in the history
…688)

* Added support for vitest as a unit test runner.
* Introduced a new unitTestRunner option to specify
the test runner (jasmine, jest, vitest).
* Deprecated the jest option to avoid redundancy
and encourage the use of the new unitTestRunner option.
  • Loading branch information
Tommy228 authored Dec 28, 2024
1 parent 012b6bb commit c924703
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 27 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1186,15 +1186,17 @@ describe('AuthService', () => {
});
```

When using the component schematic you can specify the `--jest` flag to have the Jest imports used. In order to Jest imports the default, update `angular.json`:
When using the component schematic you can specify the `--unitTestRunner` flag to `jest` to have the Jest imports used. In order to Jest imports the default, update `angular.json`:
```json
"schematics": {
"@ngneat/spectator:spectator-component": {
"jest": true
"unitTestRunner": "jest"
}
}
```

Please note that the previous `--jest` flag is deprecated and will be removed in the future.

## Vitest Support
Like Jest, Spectator also supports Vitest.

Expand Down Expand Up @@ -1246,6 +1248,15 @@ describe('AuthService', () => {
});
```

When using the component schematic you can specify the `--unitTestRunner` flag to `vitest` to have the Vitest imports used. In order to Vitest imports the default, update `angular.json`:
```json
"schematics": {
"@ngneat/spectator:spectator-component": {
"unitTestRunner": "vitest"
}
}
```

## Testing with HTTP
Spectator makes testing data services, which use the Angular HTTP module, a lot easier. For example, let's say that you have service with three methods, one performs a GET, one a POST and one performs
concurrent requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@
"jest": {
"type": "boolean",
"default": false,
"description": "When true, uses Jest to create mocks."
"description": "When true, uses Jest to create mocks. Deprecated: use unitTestRunner instead.",
"deprecated": true
},
"unitTestRunner": {
"type": "string",
"enum": ["jasmine", "jest", "vitest"],
"description": "Test runner to use to create mocks.",
"default": "jasmine",
"alias": "u"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@
"jest": {
"type": "boolean",
"default": false,
"description": "When true, uses Jest to create mocks."
"description": "When true, uses Jest to create mocks. Deprecated: use unitTestRunner instead.",
"deprecated": true
},
"unitTestRunner": {
"type": "string",
"enum": ["jasmine", "jest", "vitest"],
"description": "Test runner to use to create mocks.",
"default": "jasmine",
"alias": "u"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import { createHostFactory, SpectatorHost } from '@ngneat/spectator<% if (jest) { %>/jest<% } %>';
import { createHostFactory, SpectatorHost } from '@ngneat/spectator<% if (secondaryEntryPoint) { %>/<%= secondaryEntryPoint%><% } %>';

import { <%= classify(name)%>Component } from './<%= dasherize(name)%>.component';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHostFactory, SpectatorHost } from '@ngneat/spectator<% if (jest) { %>/jest<% } %>';
import { createHostFactory, SpectatorHost } from '@ngneat/spectator<% if (secondaryEntryPoint) { %>/<%= secondaryEntryPoint%><% } %>';

import { <%= classify(name)%>Component } from './<%= dasherize(name)%>.component';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Spectator, createComponentFactory } from '@ngneat/spectator<% if (jest) { %>/jest<% } %>';
import { Spectator, createComponentFactory } from '@ngneat/spectator<% if (secondaryEntryPoint) { %>/<%= secondaryEntryPoint%><% } %>';

import { <%= classify(name)%>Component } from './<%= dasherize(name)%>.component';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHttpFactory, HttpMethod, SpectatorHttp } from '@ngneat/spectator';
import { createHttpFactory, HttpMethod, SpectatorHttp } from '@ngneat/spectator<% if (secondaryEntryPoint) { %>/<%= secondaryEntryPoint%><% } %>';
import { <%= classify(name)%>Service } from './<%= dasherize(name)%>.service';

describe('<%= classify(name)%>Service', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator<% if (jest) { %>/jest<% } %>';
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator<% if (secondaryEntryPoint) { %>/<%= secondaryEntryPoint%><% } %>';

import { <%= classify(name)%>Directive } from './<%= dasherize(name)%>.directive';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPipeFactory, SpectatorPipe } from '@ngneat/spectator<% if (jest) { %>/jest<% } %>';
import { createPipeFactory, SpectatorPipe } from '@ngneat/spectator<% if (secondaryEntryPoint) { %>/<%= secondaryEntryPoint%><% } %>';

import { <%= classify(name)%>Pipe } from './<%= dasherize(name)%>.pipe';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createServiceFactory, SpectatorService } from '@ngneat/spectator<% if (jest) { %>/jest<% } %>';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator<% if (secondaryEntryPoint) { %>/<%= secondaryEntryPoint%><% } %>';
import { <%= classify(name)%>Service } from './<%= dasherize(name)%>.service';

describe('<%= classify(name)%>Service', () => {
Expand All @@ -10,4 +10,4 @@ describe('<%= classify(name)%>Service', () => {
it('should...', () => {
expect(spectator.service).toBeTruthy();
});
});
});
24 changes: 20 additions & 4 deletions projects/spectator/schematics/src/spectator/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/spectator/schematics/src/spectator/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c924703

Please sign in to comment.