I used @joshbaeha's suggestion to ng new a new Angular 5 project and copied the test.ts file, which appears to be completely generic and not reliant on project structure or anything else. Everything is now working. Here it is:
test.ts
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
Answer from Chris Halcrow on Stack OverflowI used @joshbaeha's suggestion to ng new a new Angular 5 project and copied the test.ts file, which appears to be completely generic and not reliant on project structure or anything else. Everything is now working. Here it is:
test.ts
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
I don't know whether you can generate just the test.ts file or not, but as far as i know this file is automatically generated when you create a new angular project using angular-cli. So you can just create a new project using angular-cli then copy the src/test.ts file from that new project
Build issue during Karma Test execution => index.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property
angular - How to resolve test.ts when running ng test? - Stack Overflow
npm test fails after upgrade from angular4 to angular 5 "test.ts is missing from the TypeScript compilation" - Stack Overflow
src/test.ts not found
Videos
05-09-2020
Angular 10
My issue was an invalid case of the component path due to copy/paste error :D
Wrong:
import { RegisterComponent } from './Register/Register.component';
Correct:
import { RegisterComponent } from './register/register.component';
I had same problem, after 2 hour searching i finally found solution. In my case (i'm using Angular), i just set typescript related path into tsconfig.app.json(ATTENTION: file name is not tsconfig.json). another way is set entire typescript path into include section in tsconfig.app.json file (see below example).
in tsconfig.app.json change:
"files": [
...,
"node_modules/jqwidgets-scripts/jqwidgets-ts/angular_jqxbargauge.ts"
],
or
"include": [
...,
"node_modules/jqwidgets-scripts/jqwidgets-ts/**/*.ts"
],
Simple solution: just remove the call to require.context
I just faced the same problem, converting an app from Angular 14 to 15, and it turns out the solution is even simpler: just delete the two lines at the bottom of src/test.ts. The call to require.context() is no longer needed.
I used to have
const context = require.context("./", true, /\.spec\.ts$/);
context.keys().forEach(context);
at the end of src/test.ts, but now I have deleted those two lines and all my tests run just like they used to with Angular 14.
I could not find proper documentation for this change (that's why I landed on this question in the first place) but my assumption is that Karma now automatically finds all the *.spec.ts files and we no longer need to direct it to do so.
Narrowing the test suites with "include"
The "include" property, added under "test":/"options":, can be used to restrict the list of spec files to run.
According to my tests, configuring angular.json like this:
"test": {
"options": {
"include": ["**/*.spec.ts"],
...
},
...
is the same as without "include": all spec files are exercised.
But configuring angular.json like this:
"test": {
"options": {
"include": ["**/app.component.spec.ts"],
...
},
...
would only run spec files called app.component.spec.ts.
test.ts file is no longer generated by angular, and the property main used to link the file is no longer allowed in angular.json, you can use the include property in angular.json to add patterns or file name
"test": {
...
"options": {
"include": [
"**/app.component.spec.ts"
]
...
}
}
I deal with this compilation problem for whole day already but no progress, I am at my wits end.
Error: C:\Users\Steve\Documents\lessonbite-web\src\app\theme\pages\webView\webViewMain.module.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
at AngularCompilerPlugin.getCompiledFile (C:\Users\Steve\Documents\lessonbite-web\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:951:23)
at C:\Users\Steve\Documents\lessonbite-web\node_modules\@ngtools\webpack\src\loader.js:43:31
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)What I am sure:
-
The compilation problem has occured this morning, when I turn on my computer. I do not change anything
-
There was no problem yesterday. The last thing I changed is some component.ts and html.
-
This Angular project works for months for me already
-
In above error, it mentioned "webViewMain.module", but I am sure it is not about this specific component, because all of the component have the same error mesage. (This makes that bug more difficult to solve)
What I think:
-
It is likely caused by the changes I did yesterday, probably the last half hour of yesterday.
What I tried:
-
Revert all the code changes, no use
-
Go SO to search for solution, they say it may be because of import name typo, but don't work for me. My project has worked for months, and nothing is changed in tsconfig.json, tsconfig.app.json, package.json
-
Delete node_moudles, and reinstall, don't work
Any idea? I am really at my wits end. I wonder how could his bug be so hard to solve.