最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular testing - test.ts not found - Stack Overflow

programmeradmin0浏览0评论

I'm trying to follow the official Angular docs to set up testing for an Angular project -

I've downloaded the sample Angular project from the page above, using the first (top) link. I've done an npm install and when I run ng serve it builds fine.

When I run ng test using the CLI, I get the message:

ERROR in Entry module not found: Error: Cant' resolve 'C:Code\testing\src\test.ts' in 'C:\Code\testing'

ERROR in error TS6053: File 'C:Code\testing\src\test.ts' not found.

I looked at this question - How to resolve test.ts when running ng test?, but in that case the file actually exists, but in the Angular example project it doesn't exist at all.

(When I first ran ng test I originally got a message about Jasmine Marbles being missing, which I resolved using:)

npm install jasmine-marbles --save

The documentation says:

You can fine-tune many options by editing the karma.conf.js and the test.ts files in the src/ folder.

So I know the test.cs is some kind of configuration file, but how do I generate it? It doesn't exist in the Angular 'live example' project either. And how do I know that a test.cs I generate will work reliably with this project?

I'm trying to follow the official Angular docs to set up testing for an Angular project - https://angular.io/guide/testing#service-tests

I've downloaded the sample Angular project from the page above, using the first (top) link. I've done an npm install and when I run ng serve it builds fine.

When I run ng test using the CLI, I get the message:

ERROR in Entry module not found: Error: Cant' resolve 'C:Code\testing\src\test.ts' in 'C:\Code\testing'

ERROR in error TS6053: File 'C:Code\testing\src\test.ts' not found.

I looked at this question - How to resolve test.ts when running ng test?, but in that case the file actually exists, but in the Angular example project it doesn't exist at all.

(When I first ran ng test I originally got a message about Jasmine Marbles being missing, which I resolved using:)

npm install jasmine-marbles --save

The documentation says:

You can fine-tune many options by editing the karma.conf.js and the test.ts files in the src/ folder.

So I know the test.cs is some kind of configuration file, but how do I generate it? It doesn't exist in the Angular 'live example' project either. And how do I know that a test.cs I generate will work reliably with this project?

Share Improve this question asked May 16, 2018 at 22:38 Chris HalcrowChris Halcrow 32.1k19 gold badges199 silver badges233 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I used @joshbaeha's suggestion to ng new a new Angular 5 project and copied the test.ts file, which appears to be pletely 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

发布评论

评论列表(0)

  1. 暂无评论