-
Notifications
You must be signed in to change notification settings - Fork 12k
fix(@angular/build): allow TestBed provider configuration with vitest unit-testing #30276
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
base: main
Are you sure you want to change the base?
Conversation
… unit-testing The experimental `unit-test` builder with the `vitest` runner configured can now specify an Angular providers file via the `providersFile` option. This allows the TestBed initialization to use the providers defined in this file for all executed tests. The contents of the providers file should include a default export with an array of one or more Angular providers. As an example: ``` import { provideZonelessChangeDetection } from '@angular/core'; export default [ provideZonelessChangeDetection(), ]; ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a really neat solution for this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See feedback
`import { getTestBed, ɵgetCleanupHook as getCleanupHook } from '@angular/core/testing';`, | ||
`import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing';`, | ||
`import { beforeEach, afterEach } from 'vitest';`, | ||
'', | ||
normalizedOptions.providersFile | ||
? `import providers from './${path.relative(projectSourceRoot, normalizedOptions.providersFile)}'` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of notes:
path.relative
will return backslashes on Windows, which will break the import paths.- The file extension (.ts) is retained. Can this cause issues? (I guess not as this is not typechecked).
- Instead of using
projectSourceRoot
, it probably makes more sense to useworkspaceRoot
, since paths inangular.json
are all relative to the workspace root.
We handle a similar case here for reference:
angular-cli/packages/angular/build/src/tools/esbuild/application-code-bundle.ts
Lines 748 to 755 in 505c836
function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string): string { | |
return ( | |
'./' + | |
relative(workspaceRoot, entryFile) | |
.replace(/.[mc]?ts$/, '') | |
.replace(/\\/g, '/') | |
); | |
} |
"providersFile": { | ||
"type": "string", | ||
"description": "TypeScript file that exports an array of Angular providers to use during test execution. The array must be a default export.", | ||
"minLength": 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is "minLength": 1
intentional?
The experimental
unit-test
builder with thevitest
runner configured can now specify an Angular providers file via theprovidersFile
option. This allows the TestBed initialization to use the providers defined in this file for all executed tests. The contents of the providers file should include a default export with an array of one or more Angular providers. As an example: