allure-framework / allure-js

Allure integrations for JavaScript test frameworks
https://allurereport.org/
Apache License 2.0
217 stars 112 forks source link

Change link template config type from an array to a record. Support link template functions #1008

Closed delatrie closed 2 weeks ago

delatrie commented 2 weeks ago

Context

The PR changes the link patterns configuration type from the array to the record whose keys are either LinkType values or arbitrary strings. A link's template can now be an arbitrary function from string to string. LinkConfig is now defined as follows:

export type LinkConfig<TOpts extends LinkTypeOptions = LinkTypeOptions> =
  Partial<Record<LinkType, TOpts>> & Record<string, TOpts>;
  1. A generic type argument is used to allow integrations to extend link options. Allure-cucumber does that to add an extra pattern field to the options.

  2. Defining the config as an intersection allows us to:

    • keep IDE autocompletion
    • index the values with arbitrary string keys (TS infers the resulting type to be TOpts instead of TOpts | undefined in such cases, but that's how it works for any record without the noUncheckedIndexedAccess TS option).

Some examples:

import type { Config } from "allure-js-commons/sdk/reporter";
import { LinkType } from "allure-js-commons";

const config: Config = {
  issue: { // Hinted by IDE autocompletion
    urlTemplate: "https://foo/%s"
  },
  [LinkType.DEFAULT]: { // Enum as the key
    urlTemplate: "https://bar/%s"
  },
  custom: { // An arbitrary string
    urlTemplate: "https://baz/%s",
    nameTemplate: "CUSTOM-%s",
  },
  tms: { // Template functions
    urlTemplate: (v) => `https://baz/${v}`,
    nameTemplate: (v) => `CUSTOM-${v}`,
  }
};

Checklist