@lynx-js/testing-environment is a pure-JavaScript implementation of the Lynx Spec, notably the Element PAPI and Dual-threaded Model for use with Node.js. In general, the goal of the project is to emulate enough of a subset of a Lynx environment to be useful for testing.
The Element PAPI implementation is based on jsdom, for example __CreateElement will return a LynxElement, which extends HTMLElement from jsdom. You can reuse the testing utilities that are commonly used for DOM testing, such as @testing-library/dom (for DOM querying) and @testing-library/jest-dom (custom jest matchers for the DOM), etc.
To use @lynx-js/testing-environment, you will primarily use the LynxTestingEnv constructor, which is a named export of the package. You will get back a LynxTestingEnv instance, which has a number of methods of useful properties, notably switchToMainThread and switchToBackgroundThread, which allow you to switch between the main thread and background thread.
Use the background thread API:
Use the main thread API:
Note that you can still access the other thread's globals without switching threads:
It is recommended to configure as Vitest's test environment, for example:
After configuration, you can directly access the lynxTestingEnv object globally in the test.
If you want to use @lynx-js/testing-environment for unit testing in ReactLynx, you usually don't need to specify this configuration manually.
Please refer to ReactLynx Testing Library to inherit the configuration from @lynx-js/react/testing-library.
Thanks to:
A pure-JavaScript implementation of the Lynx Spec, notably the Element PAPI and Dual-threaded Model for use with Node.js.
| Class | 描述 |
|---|---|
| LynxTestingEnv | A pure-JavaScript implementation of the Lynx Spec, notably the Element PAPI and Dual-threaded Model for use with Node.js. |
| Interface | 描述 |
|---|---|
| LynxElement | Any Lynx Element, such as view, text, image, etc. |
| LynxGlobalThis | The globalThis object of Lynx dual thread environment. |
| Type alias | 描述 |
|---|---|
| ElementTree | The lynx element tree |
| ElementTreeGlobals | The Element PAPI Types |
| FilterUnderscoreKeys | - |
| PickUnderscoreKeys | - |
| Function | 描述 |
|---|---|
| initElementTree | - |