ETS Interop Testing
This skill provides a structured workflow and templates for creating comprehensive interop tests between ArkTS (ETS) and JavaScript/TypeScript.
Core Concepts
Interop tests typically involve three layers:
- C++ Runner: GTest-based entry point that manages the VM lifecycle.
- ArkTS (ETS) Logic: The main test code written in ArkTS.
- JS/TS Module: The JavaScript or TypeScript code being called or calling into ArkTS.
Directory Structure
A typical test case directory (e.g., plugins/ets/tests/interop_js/tests/my_test/) should contain:
CMakeLists.txt: Build configuration.arktsconfig.in.json: ArkTS compiler configuration with dependencies.my_test.cpp: C++ GTest runner.my_test.ets: ArkTS test logic.my_test.tsormy_test.js: JS/TS side of interop.my_test.d.ets: (Optional) Declaration file for JS/TS exports.
Workflow: Adding a New Test Case
1. Initialize Directory
Create a new unique directory in plugins/ets/tests/interop_js/tests/.
2. Define JS/TS Interface
Write your JS/TS code in a .ts or .js file.
Use assets/interop_js.ts.template as a starting point.
3. Create ArkTS Declarations
If your JS/TS code exports functions/classes, create a .d.ets file to declare them for ArkTS.
Template: assets/interop_decl.d.ets.template.
4. Implement ArkTS Test Logic
Write the test logic in ArkTS. This logic should return true on success.
Template: assets/interop_test.ets.template.
5. Configure Build and Compiler
- arktsconfig.in.json: Map the JS/TS module to its
.d.etsdeclaration. Template:assets/arktsconfig.in.json.template. - CMakeLists.txt: Use the
panda_ets_interop_js_gtestmacro. Template:assets/CMakeLists.txt.template.
6. Create C++ Runner
Inherit from EtsInteropTest and use CallEtsFunction to trigger your test.
Template: assets/test_runner.cpp.template.
Verification and Best Practices
- Unique Naming: Ensure your target name in
CMakeLists.txtand directory name are unique to avoid collisions. - Isolation: Each test should be self-contained in its directory.
- Error Handling: Prefer returning
booleanfrom ETS or throwing exceptions that C++ can catch. - Formatting: Always run
bash code-format.sh format-changedafter adding new files.
References
- See
ohos-dev-arkruntime-interop-developmentfor architectural details. - Root
CMakeLists.txtininterop_js/tests/automatically includes subdirectories viaSUBDIRLIST.