问题
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:
- Use
paths
section of thetsconfig.json
to add mock import locations - In the mocks directory create files for any unresolved module
- 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