Unit testing Nativescript application logic on a browser

删除回忆录丶 提交于 2020-02-04 22:16:44

问题


I am trying to set up unit testing for a Nativescript application, run by ng test on a browser. The problem is that whenever there is a tns-core-modules or another plugin import, the module cannot be resolved because of the platform specific files (e.g. "tns-core-modules/application/application.android.js") that never get compiled into the bundle, thus throwing an error like "Module not found: Error: Can't resolve 'tns-core-modules/application'".

I know there is a built-in unit test support in Nativescript. The problem I have with it is that it can't run on CI. I would like to be a ble to have lightweight tests for my business logic, mocking out all platform dependencies. I have looked for a way to mock the module imports at runtime with no luck. I looked into rewire package but it only runs on node.


回答1:


You simply can not run NativeScript application on Browser.

In case if you are looking for something like headless mode, Appium has one too, isHeadless in capabilities.

Between, may I know why you think you can not run the {N} unit tests on CI? It should work on CI too, after all it's a machine that runs the same commands based on some trigger.




回答2:


I finally managed to get it working. Not a very elegant solution and I have yet to see how much maintenance it requires. Key points here:

  1. Use paths section of the tsconfig.json to add mock import locations
  2. In the mocks directory create files for any unresolved module
  3. Some nativescript modules are referencing helper functions on global scope but they're undefined. My solution was to define them in test.ts like this
window['__decorate'] = () => {};
window['__extends'] = () => {};
window['__metadata'] = () => {};
window['__param'] = () => {};
window['layout_base_1'] = { CSSType: () => {} };
window['Crashlytics'] = {};
window['Fabric'] = {};


来源:https://stackoverflow.com/questions/55458902/unit-testing-nativescript-application-logic-on-a-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!