问题
My unit tests are contained into a module that loads from an HTML page using SystemJS.
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<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>
<script>
System.config({
defaultJSExtensions: true,
map: {
"angular2": 'http://localhost:8000/angular2',
"rxjs": 'node_modules/rxjs'
},
packages: {
angular2: {
defaultExtension: 'js',
}
}
});
System.import('angular2/test/http/backends/xhr_backend_spec')
.then((src) => {
src.main();
});
</script>
My page doesn't display any tests whereas my main
method contains a test suite:
export function main() {
console.log('in main');
describe('XHRBackend', () => {
(...)
});
}
I put some traces and I check that the main
function and the callback defined in the describe function are called. But the tests themselves aren't executed within the describe
callback.
I guess that I need to wait for the module to be loaded before running Jasmine but I don't know how to configure this.
Thanks for your help!
回答1:
Tell Jasmine to run the imported tests with .then (window.onload)
<script>
System.config({
(...)
System.import('angular2/test/http/backends/xhr_backend_spec')
// wait for all imports to load ...
// then re-execute `window.onload` which
// triggers the Jasmine test-runner start
.then(window.onload)
(...)
</script>
I read how to run Jasmine Tests on angular.io
https://angular.io/docs/ts/latest/testing/first-app-tests.html
来源:https://stackoverflow.com/questions/35608358/wait-for-a-module-to-be-loaded-to-execute-the-tests-it-contains