In an angular 4 project (karma & Jasmin as ), I want to keep test files in a custom test folder (instead of the respective component folder).
Here is the default folder structure.
I want to move all "*.spec.ts" file inside test folder (which I created and might have some kind of folder structure inside it)
When I move the app.component.specs.ts/or any other specs.ts to "test" folder, they are not getting detected (because of path declaration in test.ts and tsconfig.specs.json).
here is test.ts
declare const __karma__: any;
declare const require: any;
__karma__.loaded = function () {};
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
)
// This need to be modified but dont know how
const context = require.context('./', true, /\.spec\.ts$/);
context.keys().map(context);
__karma__.start();
and here is tsconfig.specs.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"baseUrl": "./",
"module": "commonjs",
"target": "es5",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}
if I change the context declaration to this and even move only app.component.spec.ts the .specs.ts files to test folder, I got the below error.
const context = require.context('../test', true, /\.spec\.ts$/);
Here is the app.spec.ts (after moving it to custom "test" folder)
import { QuoteTextComponent } from '../src/app/components/quote-text/quote-text.component';
import { TestBed, async } from '@angular/core/testing';
import { APP_BASE_HREF } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from '../src/app/app.component';
import { HomeComponent } from '../src/app/components/home/home.component';
import { AboutComponent } from '../src/app/components/about/about.component';
describe('AppComponent', () => {
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full'}
];
Can you please show some lights on how to achieve this?
Update : After adding a new context like this
const context1 = require.context('../', true, /app\.component\.spec\.ts$/);
migration of app.conponent.spec.ts work as desired, but when I move the other component specifc test.spec.ts file and add a new context like this,
const context2 = require.context('../', true, /.component.spec.ts$/);
The moved file does not get picked. Also added this
"./test/*.spec.ts",
to tsconfig.specs.json.