Cordova Plugin Test Framework
The `cordova-plugin-test-framework` provides a structured approach for Cordova plugin developers to create and run tests directly within their Cordova projects. It establishes an interface where plugins can bundle their tests as nested plugins within a `/tests` directory, using a `plugin.xml` and `package.json` for definition and dependency management. The framework then offers a test harness to execute these tests, utilizing the Jasmine 2.0 format for defining test suites (`describe`, `it`). This allows for rapid iteration between development and testing, ensuring tests run against the exact versions of plugins and platforms in use. The latest published version is 1.1.6. However, the project appears to be largely unmaintained, with its last significant activity on npm and GitHub over seven years ago, suggesting an effectively abandoned status. It differentiates itself by integrating tests directly into the Cordova build lifecycle, rather than relying on external, separate test runners.
Common errors
-
Plugin doesn't have a package.json
cause The nested test plugin within your main plugin's `/tests` directory is missing a `package.json` file, which is now required by Cordova tooling.fixCreate a `package.json` file inside your plugin's `/tests` directory. This file should define the test plugin's ID (e.g., `plugin-id-tests`) and any necessary `devDependencies`. -
describe is not defined
cause The Jasmine test runner, which provides the `describe`, `it`, and `expect` functions, has not been correctly initialized or the test environment is not properly set up by the `cordova-plugin-test-framework`. This could happen if the test harness isn't loaded.fixEnsure `cordova-plugin-test-framework` is added to your project and that your app's `config.xml` is configured to load `cdvtests/index.html` as its start page. Also, verify your test file is correctly exported via a `<js-module>` in the nested `tests/plugin.xml`. -
Error: Cannot find module 'my-test-dependency'
cause An npm dependency required by your test files (e.g., for mocking or utility functions) has not been installed.fixAdd the missing dependency to the `devDependencies` section of the `package.json` located within your plugin's `/tests` directory, and then run `npm install` inside that `/tests` directory.
Warnings
- breaking Older versions of Cordova plugins' test structures might not include a `package.json` file within their `/tests` directory. Modern Cordova tooling, and specifically this framework, now requires a nested `package.json` for test plugins to manage their own npm dependencies.
- gotcha This plugin appears to be unmaintained. Its last significant update was over seven years ago (version 1.1.6 published 8 years ago), indicating potential incompatibility with newer Cordova versions, platforms, or modern JavaScript features (e.g., ESM).
- gotcha The framework relies on making Jasmine 2.0 globals (`describe`, `it`, `expect`) available in the test execution context. This global pollution might conflict with other testing setups or lead to unexpected behavior if not managed carefully.
- gotcha To run tests, you must manually configure your Cordova app's `config.xml` to point its start page to `cdvtests/index.html`. Forgetting this step will prevent the test harness from loading and displaying test results.
Install
-
npm install cordova-plugin-test-framework -
yarn add cordova-plugin-test-framework -
pnpm add cordova-plugin-test-framework
Imports
- defineAutoTests
import { defineAutoTests } from 'my-plugin-tests';exports.defineAutoTests = function() { /* ... */ } - describe
import { describe } from 'jasmine';describe('My Test Suite', function() { /* ... */ }); - Test Harness
npm install cordova-plugin-test-framework
cordova plugin add cordova-plugin-test-framework
Quickstart
/* In your plugin's 'tests/tests.js' file */
exports.defineAutoTests = function() {
describe('Cordova Test Framework Basic Tests', function() {
beforeEach(function() {
// Optional: Setup code before each test
console.log('Running test setup...');
});
it('should confirm window.cordova exists', function() {
expect(window.cordova).toBeDefined();
expect(typeof window.cordova).toBe('object');
});
it('should allow adding other plugins for testing', function() {
// Example for a hypothetical device plugin
// In a real scenario, cordova-plugin-device-tests would be added
if (window.cordova && window.cordova.plugins && window.cordova.plugins.device) {
expect(window.cordova.plugins.device).toBeDefined();
expect(typeof window.cordova.plugins.device.platform).toBe('string');
} else {
pending('cordova-plugin-device is not available for testing.');
}
});
// You can also define manual tests
// exports.defineManualTests = function() { /* ... */ };
});
// To run this, you would add your plugin's tests as a sub-plugin:
// cordova plugin add https://github.com/apache/cordova-plugin-device.git#:/tests
// Then add the test framework:
// cordova plugin add cordova-plugin-test-framework
// Finally, set your config.xml to load the test harness:
// <content src="cdvtests/index.html" />
};