emberjs / emberjs/ember-test-helpers

Proposal: API for adding test metadata

Open
#721 3 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
188
Forks
254
PR merge metrics
No merged PRs in 30d

Description

Having a mechanism for adding metadata could be quite a useful addition. Information such as test name, test ID, type, and any additional information that can be added to metadata (such as what specific setups were run) that when composed tells a fuller picture about the composition of the test.

One of the key purposes of this addition would be to provide better annotations for the test characteristics in order to log and analyze them in a more parsable and queryable way.

Details
-------
The potential API for this can be quite simple:

Proposed interface:

```ts
interface ITestMetadata {
testName: string | null;
types: string[];
[key: string]: any;
}
```

Proposed API:

```ts
getTestMetadata(context: BaseContext): ITestMetadata;
```

The `getTestMetadata` function would be backed by a `WeakMap`, which would associate the metadata with the test context. Accessing this for the first time, when a prior test context hadn't been added, would lazy initialize and add a test metadata object to the `WeakMap`.

Usage
-----

*Use case: adding test name and test ID in `ember-qunit`*

In [`ember-qunit`](https://github.com/emberjs/ember-qunit/blob/master/addon-test-support/ember-qunit/index.js#L138):

```js
QUnit.testStart(({ name, testId }) => {
let testMetadata = getTestMetadata(QUnit.config.current.testEnvironment);
testMetadata.testName = name;
testMetadata.testId = testId;

Ember.testing = true;
});
```

*Use case: adding metadata for test type*

In `setupRenderingContext`:

```ts
let testMetadata: ITestMetadata = getTestMetadata(context);

testMetadata.types.push('rendering');
```

*Use case: adding metadata for additional setup functions that are invoked as part of the test setup*

In `setupMirage`:

```ts
let testMetadata: ITestMetadata = getTestMetadata(context);

testMetadata.types.push('mirage');
```

*Use case: adding metadata to attribute a test to a sub-team*

```ts
let testMetadata: ITestMetadata = getTestMetadata(context);

testMetadata.team = 'Sub team name';
```

Being able to provide arbitrary values to add to the metadata would allow for a multitude of scenarios.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.