Skip to content

uid11/create-test-id

Repository files navigation

create-test-id

NPM version dependencies: none code style: prettier TypeScript Conventional Commits License MIT

The package is deprecated. Use the https://www.npmjs.com/package/create-locator package instead.

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';
// or
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<{header: unknown; label: typeof labelTestId}>('fooWebApp');

appTestId.label = labelTestId;

For production you can import createTestId from create-test-id/production. This function has the same API as dev createTestId, but does not create new objects, and its testId is always equal to the empty string.

import {createTestId as createTestIdForDev} from 'create-test-id';
import {createTestId as createTestIdForProduction} from 'create-test-id/production';

export const createTestId = __IS_PRODUCTION__ ? createTestIdForProduction : createTestIdForDev;

License

MIT

Releases

No releases published

Packages

No packages published