2017-11-18 2 views
1

Ich habe ein Angular CLI-Projekt mit einem NgModule außerhalb des Ordners /src (schließlich wird dieses NgModule als npm-Modul verpackt, aber für jetzt lasse ich es nur außerhalb von /src).Angular CLI - Wie Sie spects-Dateien außerhalb des src-Ordners abholen können?

Ich versuche, Unit-Tests für dieses NgModule zu schreiben, aber ng test scheint nicht .spec.ts Dateien außerhalb der /src Ordner abzuholen.

Ich habe versucht, den Weg in /src/test.ts zu verändern:

// Original 
const context = require.context('./', true, /\.spec\.ts$/); 

// Does not work 
const context = require.context('../', true, /\.spec\.ts$/); 

// Does not work 
const context = require.context('/absolute/path/to/ngmodule', true, /\.spec\.ts$/); 

Aber ich erhalte eine Fehlermeldung: Module build failed: Error: /absolute/path/to/file.spec.ts is not part of the compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

ich auch versucht habe, das Hinzufügen des Weges zu meinem NgModule in der include Eigenschaft tsconfig.spec.json:

"include": [ 
    "../my-ng-module/**/*.spec.ts", // Added this 
    "**/*.spec.ts", 
    "**/*.d.ts" 
] 

Aber kein Erfolg. Irgendwelche Ideen?

Antwort

2

In tsconfig.spec.json

"include": [ 
    "../*.spec.ts", // your spec file location parent of src or ../yourpath/ 
    "**/*.spec.ts", 
    "**/*.d.ts" 
    ] 

In test.ts

if you pick parent dir it picks the other specs as well, hence when picking the parent dir of src then give your component name or specify the exact path of your spec

const context1 = require.context('./', true, /\.spec\.ts$/);// 
const context = require.context('../', true, /app\.component\.spec\.ts$/);// 
context.keys().map(context); 
context1.keys().map(context1); 

Wenn Sie sich in den übergeordneten Ordner von src die gleichen Kontext wird die Dateien Aufnehmen, die in node_modules sind, daher müssen wir den spec.ts Namen nennen.

spec.ts-Dateien, die in src sind, wie laufen, bevor enter image description here

App-Spezifikation ist in Elternteil spec src und Spiel in in src-Ordner ist

If you want to specify only the root directory and from there you want to pick all the spec files then we give a different naming convention which differs from the spec files in node_modules. Say which are out of src as yourapp.componentout.spec.ts and which are inside insideapp.componentin.spec.ts and you can give one path instead of two directories

const context = require.context('../', true, /out\.spec\.ts$|in\.spec\.ts$/);// 
context.keys().map(context); 

oder die andere folgen Sie der Namenskonvention für die Dateien, die nur außerhalb von src liegen und setzen Sie

const context1 = require.context('./', true, /\.spec\.ts$/);// 
const context = require.context('../', true, /out\.spec\.ts$/);// 
context.keys().map(context); 
context1.keys().map(context1); 

Wenn Sie die Spezifikationsdateinamen nicht ändern möchten, geben Sie den Pfad Ihres Modulspeicherorts anstelle des übergeordneten Verzeichnisses an, in dem Sie den Knoten modules überspringen können.

Grundsätzlich können wir die folgenden ändern nach unseren Bedürfnissen

require.context(directory, useSubdirectories = false, regExp = /^\.\//) 

hoffe, das hilft !!!

Ref: https://webpack.js.org/guides/dependency-management/#require-context

+0

Große Antwort! Ich hätte nie an zusätzliche Zusammenhänge in 'tests.ts' gedacht, ich habe nirgendwo Dokumentation darüber gesehen. Vielen Dank für die Veröffentlichung – Ryan

Verwandte Themen