Bit testers test components during the component development and during the component build process. They are often used to integrate popular test runners such as Jest, Mocha, and Cypress into Bit, but can also be used to implement custom testers.
Run the following to fork a demo tester that you can use as a template for your own tester:
Alternatively, run the following to create a workspace with a tester, an env that uses the tester, and a sample component that uses that env (replace my-org.my-scope with your own bit.cloud org/username and scope name):
The test method is executed during development and during build.
Use the context.rootPath
to determine the location of the test run, or whether the test is executed in the workspace or in the capsule.
Use the context.components
to get the components to test.
The method should return an object that contains the test results (in a standard format) for the entire test run.
async test(context: TesterContext): Promise<Tests> { let componentTestResults: ComponentResults = []; /* get the components to test */ const components = context.components; /* run tests for each component */ for await (const component of components) { /* get the test files for a component */ const testFiles = context.patterns.get(component); // implement the test logic here... /* set the component's test results in a standard format */ const componentTestResult = { componentId, results: [{ testFiles: [ { file, tests, pass, failed, pending } ] }], errors: [], }; /* add this component's test results to the total test results */ componentTestResults.push(componentTestResult); } /* return the test results */ return { components: componentTestResults, errors: [] }; }
To make the tester available as a standard build task that can be registered by env, wrap it with the tester-task build task.
The tester-task wraps the test
method with a standard build task interface, and some additional logic that's required for testing during build.
/* @filename: test-task.ts */ import { TesterTask } from '@teambit/defender.tester-task'; import { TaskHandler } from '@teambit/builder'; /* import the tester */ import { JestTester } from './jest.tester'; export type JestOptions = { id: string; description?: string; } /** * wrap the jest tester with a test build task. * this allows envs to register it as a build task. */ export const JestTask = { from: (options: JestOptions): TaskHandler => { return TesterTask.from({ /* set the tester property with a tester instance */ tester: JestTester.from(options), }); }, };
Method Signature | Description |
---|---|
watch?(context: TesterContext): Promise<Tests> | Watch tests on all components (auto-test modified components) |
onTestRunComplete?(callback: CallbackFn): Promise<void> | Invoke a function on test run complete (applies only during watch). |
version(): string | Returns the tester version. |
displayConfig?(): string | The displayConfig function helps the user see the tester config when using the bit env <comp-id> command. It should return a string representation of the tester config. (for example JSON.stringify of the tsconfig file) |
configPath?: string | A path to the config of the specific tester in the filesystem. For example, path to the jest config file. |
Compilers are used by components via the components' envs. To use your compiler, follow these steps (or use the workspace starter at the top of this page):
- Create a new env or use an existing one.
- Set the compiler as the env's compiler.
- Create a component using that env or set an existing component to use that env.
Learn about debugging components in the workspace in this blog.
Run the following to test the tester component in development (in the workspace):
Learn more about running the tester in the Running testers page.
Verify your test task is set correctly by listing the relevant component build tasks:
The output should include the name of your compiler task:
teambit.harmony/aspect:CoreExporter
teambit.compilation/compiler:TestComponents
...
Run the following to run the test in A capsule. Specify the test task name to skip other tasks: