create-test-id

Creates typed testId for autotests.
Install
Requires node version 16 or higher:
npm install create-test-id
Usage
In TypeScript module label.tsx
with component Label
:
import createTestId from 'create-test-id';
import {createTestId} from 'create-test-id';
import {inputTestId} from 'components/Input';
export const labelTestId = createTestId<{input: typeof inputTestId; text: unknown}>();
labelTestId.input = inputTestId;
export const Label = () => {
...
return (
<div data-testid={labelTestId}>
...
<span data-testid={labelTestId.text}>...<span>
</div>
);
};
In TypeScript module App.tsx
with root component App
:
import {labelTestId} from 'components/Label';
export const appTestId = createTestId<{label: typeof labelTestId; header: unknown}>('fooWebApp');
appTestId.label = labelTestId;
For production you can use createTestIdForProduction
. This function has the same API as createTestId
,
but does not create new objects, and its testId
is always equal to the empty string.
import {createTestId as createTestIdForDev, createTestIdForProduction} from 'create-test-id';
export const createTestId = __IS_PRODUCTION__ ? createTestIdForProduction : createTestIdForDev;
License
MIT