|
| 1 | +# Plugins |
| 2 | + |
| 3 | +Harness plugins are the way to extend Harness beyond what it offers out of the box. |
| 4 | + |
| 5 | +Use them to add project-specific capabilities on top of Harness, such as native coverage collection, custom reporting, artifact collection, or workflow-specific automation. |
| 6 | + |
| 7 | +A plugin can react to events such as: |
| 8 | + |
| 9 | +- Harness startup and teardown |
| 10 | +- run start and finish |
| 11 | +- Metro initialization and bundle events |
| 12 | +- app lifecycle signals |
| 13 | +- collection, suite, and test execution events |
| 14 | + |
| 15 | +## Defining a Plugin |
| 16 | + |
| 17 | +Use `definePlugin()` from `@react-native-harness/plugins` and register the plugin in your Harness config. |
| 18 | + |
| 19 | +```ts |
| 20 | +import { definePlugin } from '@react-native-harness/plugins'; |
| 21 | + |
| 22 | +export const loggingPlugin = () => |
| 23 | + definePlugin({ |
| 24 | + name: 'logging-plugin', |
| 25 | + hooks: { |
| 26 | + harness: { |
| 27 | + beforeCreation: async (ctx) => { |
| 28 | + ctx.logger.info('Harness is starting for', ctx.platform.platformId); |
| 29 | + }, |
| 30 | + }, |
| 31 | + run: { |
| 32 | + started: async (ctx) => { |
| 33 | + ctx.logger.info('Run started', ctx.runId); |
| 34 | + }, |
| 35 | + finished: async (ctx) => { |
| 36 | + ctx.logger.info('Run finished', ctx.runId, ctx.status); |
| 37 | + }, |
| 38 | + }, |
| 39 | + testFile: { |
| 40 | + finished: async (ctx) => { |
| 41 | + ctx.logger.info('Finished test file', ctx.file, ctx.status); |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | +``` |
| 47 | + |
| 48 | +Then register it in `rn-harness.config.mjs`: |
| 49 | + |
| 50 | +```ts |
| 51 | +import { loggingPlugin } from './logging-plugin'; |
| 52 | + |
| 53 | +export default { |
| 54 | + entryPoint: './src/test.ts', |
| 55 | + appRegistryComponentName: 'App', |
| 56 | + runners: [ |
| 57 | + { |
| 58 | + name: 'ios', |
| 59 | + runner: '@react-native-harness/platform-ios', |
| 60 | + platformId: 'ios', |
| 61 | + config: {}, |
| 62 | + }, |
| 63 | + ], |
| 64 | + plugins: [loggingPlugin()], |
| 65 | +}; |
| 66 | +``` |
| 67 | + |
| 68 | +## Available Events |
| 69 | + |
| 70 | +The full list of plugin events is defined in the source here: |
| 71 | + |
| 72 | +- [packages/plugins/src/types.ts](https://github.com/callstackincubator/react-native-harness/blob/main/packages/plugins/src/types.ts) |
| 73 | + |
| 74 | +The public plugin shape is based on nested objects like `run.started`, `metro.bundleFinished`, or `harness.beforeDispose`. |
0 commit comments