在浏览器上对Nativescript应用程序逻辑进行单元测试 [英] Unit testing Nativescript application logic on a browser

查看:94
本文介绍了在浏览器上对Nativescript应用程序逻辑进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为ng test在浏览器上运行的Nativescript应用程序设置单元测试.问题是,只要有tns-core-modules或其他插件导入,就无法解析该模块,因为平台特定的文件(例如"tns-core-modules/application/application.android.js")永远不会被编译成该文件.捆绑,从而引发类似找不到模块:错误:无法解析'tns-core-modules/application'"之类的错误.

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'".

我知道Nativescript中有内置的单元测试支持.我的问题是它不能在CI上运行.我希望对我的业务逻辑进行轻量级测试,从而模拟出所有平台依赖性. 我一直在寻找一种在运气不佳的情况下在运行时模拟模块导入的方法.我查看了rewire程序包,但它仅在节点上运行.

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.

推荐答案

我终于设法使其正常运行.这不是一个非常好的解决方案,我还没有看到它需要多少维护.这里的要点:

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. 使用tsconfig.jsonpaths部分添加模拟导入 地点
  2. 在模拟目录中,为任何未解决的模块创建文件
  3. 某些nativescript模块在全局上引用辅助函数 作用域,但未定义.我的解决方案是在 test.ts这样
  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'] = {};

这篇关于在浏览器上对Nativescript应用程序逻辑进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆