You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Relatively new webapp, just added ng-mocks. I need to add a globalKeep() for a service.
Problem: src/test.ts does nothing. It's not included, it's not running. I added a giant console.log to the file and it outputs nothing when I do ng test.
Angular version: 19.1.5
ng-mocks: 14.13.2
What did I try:
Followed the instruction of "simply install ng-mocks as a dev-dependency", then "simply add src/test.ts"
Looked for more instructions but couldn't find any
So now what? After hours of googling and wrangling various gtps, I finally got it running.
How did I get it running?
I eventually stumbled upon the angular 18 example app. It does a few important things that are not mentioned in the ngmocks setup guide (unless I missed them):
Add this to your angular json: projects.{yourProject}.architect.test.options.main: "src/test.ts"
Add this to your tsconfig.spec.json "include": "src/test.ts"
Add this as a dev dependency: @angular/platform-browser-dynamic
Make sure this is in your test.ts file:
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
// ... ngMocks.autospy('jasmine'), globalKeep(), etc here
// Add this at the bottom of test.ts
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
},
);
From my observations: Until these steps are performed, the test.ts file will never be included.
Why did I need all these extra steps? I don't know. I just followed the official setup guide for angular 19 not too long ago, when the webapp was born, and the ngmocks instructions today. Maybe I missed a step somewhere? Am I just bad at reading documentation?
Anyway, I wanted to document my solution, hoping it helps someone in a similar situation :)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Relatively new webapp, just added ng-mocks. I need to add a globalKeep() for a service.
Problem: src/test.ts does nothing. It's not included, it's not running. I added a giant console.log to the file and it outputs nothing when I do
ng test
.Angular version: 19.1.5
ng-mocks: 14.13.2
What did I try:
So now what? After hours of googling and wrangling various gtps, I finally got it running.
How did I get it running?
I eventually stumbled upon the angular 18 example app. It does a few important things that are not mentioned in the ngmocks setup guide (unless I missed them):
projects.{yourProject}.architect.test.options.main: "src/test.ts"
"src/test.ts"
@angular/platform-browser-dynamic
From my observations: Until these steps are performed, the
test.ts
file will never be included.Why did I need all these extra steps? I don't know. I just followed the official setup guide for angular 19 not too long ago, when the webapp was born, and the ngmocks instructions today. Maybe I missed a step somewhere? Am I just bad at reading documentation?
Anyway, I wanted to document my solution, hoping it helps someone in a similar situation :)
Beta Was this translation helpful? Give feedback.
All reactions