我目前使用angular-cli的混合角度应用程序(2.4.9和1.5.0)。目前,在运行我们的应用程序时,我们能够正确引导1.5应用程序:
// main.ts
import ...
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
angular.element(document).ready(() => {
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.body, ['myApp'], {strictDi: true});
});
});
但是,在我们的test.ts
文件中:
// test.ts
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import ...;
declare var __karma__: any;
declare var require: any;
__karma__.loaded = function () {};
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
// I'm assuming that I need to call 'boostrapModule()' somehow here...
platformBrowserDynamicTesting()
);
const context = require.context('./', true, /\.spec\.ts$/);
context.keys().map(context);
__karma__.start();
我不确定如何将我们的1.5应用程序引导到测试环境中,我所获得的只是Module 'myApp' is not available!
,并且我的Google技能未能尝试找到示例。
答案 0 :(得分:6)
我希望我昨晚添加的赏金意味着我今天早上可以登录为我提供一个很好的解决方案。唉,事实并非如此。因此,我花了一天时间围绕许多SO答案和github问题开始工作。对不起,我没有跟踪帮助我归功于他们的一切,但这是我的解决方案。它可能不太理想,但它到目前为止工作,所以我希望这是一个良好的开端。
This github issue表示downgradeComponent
暂时不起作用,所以我选择使用UpgradeAdapter
的旧技术。请注意,此技术不使用initTestEnvironment
。以下是相关摘要,下面有一些解释:
// downgrade.ts:
export const componentsToDowngrade = {
heroDetail: HeroDetailComponent,
...
};
export function downgradeForApp() {
forOwn(componentsToDowngrade, (component, name) => {
app.directive(name!, downgradeComponent({ component }));
});
}
// main.ts:
downgradeForApp();
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory).then((platformRef) => {
...
});
// test.ts:
require("../src/polyfills.ts");
require("zone.js/dist/proxy");
require('zone.js/dist/sync-test');
require("zone.js/dist/mocha-patch");
// test-helper.ts
let upgradeAdapterRef: UpgradeAdapterRef;
const upgradeAdapter = new UpgradeAdapter(AppModule);
forEach(componentsToDowngrade, (component, selectorName) => {
angular.module("app").directive(
selectorName!,
upgradeAdapter.downgradeNg2Component(component) as any,
);
});
export function useAdaptedModule() {
beforeEach(() => {
upgradeAdapterRef = upgradeAdapter.registerForNg1Tests(["app"]);
});
}
export function it(expectation: string, callback: () => void) {
test(expectation, (done) => {
inject(() => { }); // triggers some kind of needed initialization
upgradeAdapterRef.ready(() => {
try {
callback();
done();
} catch (ex) { done(ex); }
});
});
}
// hero-detail.component.spec.ts
import { it, useAdaptedModule } from "test-helpers/sd-app-helpers";
describe("", () => {
useAdaptedModule();
it("behaves as expected", () => { ... });
});
该代码的一些亮点:
downgrade.ts
main.ts
从downgradeForApp()
降级主要应用的组件,如上所示(用于AOT用于生产包),还有main-jit.ts
,未在上面显示(用于发展)useAdaptedModule()
代替beforeEach(angular.mock.module("app"));
it
,它包裹了Mocha提供的it
。我的测试都不是异步的;如果你有一些它可能需要调整。我不知道它可能需要如何适应Jasmine。 警告:实例化组件必须在it
回调中进行,以便在upgradeAdapterRef.ready(...)
内进行。试图在beforeEach
范围内进行此操作还为时过早。