我一直在遵循教程https://angular.io/docs/ts/latest/guide/testing.html来使用Tour of Heroes教程来实现Jasmine with Angular 2。Angular 2和Jasmine
当我用“hero.spec”测试时,一切正常。但是,当我尝试用名为“app.spec”的单元测试文件测试“app.component.ts”时,系统JS不会向其添加“js”。我收到以下错误:
angular2-polyfills.js:126 GET http://127.0.0.1:8080/src/test/app.component 404 (Not Found)`
system.src.js:1068 GET http://127.0.0.1:8080/angular2/core 404 (Not Found)
的app.spec.ts文件如下:
import { AppComponent } from 'app.component';
describe('AppComponent',() => {
let app : AppComponent;
beforeEach(() => {
app = new AppComponent();
});
it('true to be true',() => {
expect(true).toEqual(true);
});
});
单元测试文件如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Ng App Unit Tests</title>
<link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
</head>
<body>
<!-- #1. add the system.js library -->
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script>
// #2. Configure systemjs to use the .js extension
// for imports from the app folder
System.config({
packages: {
'app': {defaultExtension: 'js'}
}
});
// #3. Import the spec files explicitly
Promise.all([
System.import('app/app.spec'),
System.import('app/app.component')
])
// #4. wait for all imports to load ...
// then re-execute `window.onload` which
// triggers the Jasmine test-runner start
// or explain what went wrong.
.then(window.onload)
.catch(console.error.bind(console));
</script>
</body>
</html>
它看起来像它的系统JS将“js”附加到“app.spec”而不是“app.component”或其他。任何人都可以将我指向正确的方向吗?
感谢
你可以在你的System.config中尝试defaultJSExtensions:true吗? – echonax
@echonax的确看起来不够紧密。好问题 - 我敢打赌,修复它。 – drewmoore