Skip to content

Commit

Permalink
fix: assign and merge operations (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Sep 1, 2023
1 parent e771ae4 commit fc8a5c0
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 24 deletions.
13 changes: 12 additions & 1 deletion packages/fixtures/27.x.x/env-worker-1/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@
"name": "Jane Smith",
"email": "jane.smith@example.com"
},
"operation": "set"
"operation": "assign"
},
{
"type": "set_metadata",
"testFilePath": "__tests__/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.lead",
"value": {
"name": "Samantha Jones",
"email": "samantha.jones@example.com"
},
"operation": "merge"
},
{
"type": "set_metadata",
Expand Down
13 changes: 12 additions & 1 deletion packages/fixtures/27.x.x/env-worker-N/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@
"name": "Jane Smith",
"email": "jane.smith@example.com"
},
"operation": "set"
"operation": "assign"
},
{
"type": "set_metadata",
"testFilePath": "__tests__/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.lead",
"value": {
"name": "Samantha Jones",
"email": "samantha.jones@example.com"
},
"operation": "merge"
},
{
"type": "set_metadata",
Expand Down
13 changes: 12 additions & 1 deletion packages/fixtures/28.x.x/env-worker-1/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@
"name": "Jane Smith",
"email": "jane.smith@example.com"
},
"operation": "set"
"operation": "assign"
},
{
"type": "set_metadata",
"testFilePath": "__tests__/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.lead",
"value": {
"name": "Samantha Jones",
"email": "samantha.jones@example.com"
},
"operation": "merge"
},
{
"type": "set_metadata",
Expand Down
13 changes: 12 additions & 1 deletion packages/fixtures/28.x.x/env-worker-N/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@
"name": "Jane Smith",
"email": "jane.smith@example.com"
},
"operation": "set"
"operation": "assign"
},
{
"type": "set_metadata",
"testFilePath": "__tests__/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.lead",
"value": {
"name": "Samantha Jones",
"email": "samantha.jones@example.com"
},
"operation": "merge"
},
{
"type": "set_metadata",
Expand Down
13 changes: 12 additions & 1 deletion packages/fixtures/29.x.x/env-worker-1/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@
"name": "Jane Smith",
"email": "jane.smith@example.com"
},
"operation": "set"
"operation": "assign"
},
{
"type": "set_metadata",
"testFilePath": "__tests__/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.lead",
"value": {
"name": "Samantha Jones",
"email": "samantha.jones@example.com"
},
"operation": "merge"
},
{
"type": "set_metadata",
Expand Down
13 changes: 12 additions & 1 deletion packages/fixtures/29.x.x/env-worker-N/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@
"name": "Jane Smith",
"email": "jane.smith@example.com"
},
"operation": "set"
"operation": "assign"
},
{
"type": "set_metadata",
"testFilePath": "__tests__/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.lead",
"value": {
"name": "Samantha Jones",
"email": "samantha.jones@example.com"
},
"operation": "merge"
},
{
"type": "set_metadata",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ tests_hook_nesting_js_hook_0 : id = "hook_0"
tests_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_hook_nesting_js_describe_1 #ded
tests_hook_nesting_js_describe_1 : id = "describe_1"
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"description":"This is a sample test suite."}}
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"lead":{"name":"Samantha Jones","email":"samantha.jones@example.com"},"description":"This is a sample test suite."}}
object "HookDefinitionMetadata" as tests_hook_nesting_js_hook_1 #fdd
tests_hook_nesting_js_hook_1 : id = "hook_1"
tests_hook_nesting_js_hook_1 : data = {"vendor":{"description":"Prepare the environment"}}
Expand Down Expand Up @@ -1288,7 +1288,7 @@ tests_hook_nesting_js_hook_0 : id = "hook_0"
tests_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_hook_nesting_js_describe_1 #ded
tests_hook_nesting_js_describe_1 : id = "describe_1"
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"description":"This is a sample test suite."}}
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"lead":{"name":"Samantha Jones","email":"samantha.jones@example.com"},"description":"This is a sample test suite."}}
object "HookDefinitionMetadata" as tests_hook_nesting_js_hook_1 #fdd
tests_hook_nesting_js_hook_1 : id = "hook_1"
tests_hook_nesting_js_hook_1 : data = {"vendor":{"description":"Prepare the environment"}}
Expand Down Expand Up @@ -2817,7 +2817,7 @@ tests_hook_nesting_js_hook_0 : id = "hook_0"
tests_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_hook_nesting_js_describe_1 #ded
tests_hook_nesting_js_describe_1 : id = "describe_1"
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"description":"This is a sample test suite."}}
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"lead":{"name":"Samantha Jones","email":"samantha.jones@example.com"},"description":"This is a sample test suite."}}
object "HookDefinitionMetadata" as tests_hook_nesting_js_hook_1 #fdd
tests_hook_nesting_js_hook_1 : id = "hook_1"
tests_hook_nesting_js_hook_1 : data = {"vendor":{"description":"Prepare the environment"}}
Expand Down Expand Up @@ -3654,7 +3654,7 @@ tests_hook_nesting_js_hook_0 : id = "hook_0"
tests_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_hook_nesting_js_describe_1 #ded
tests_hook_nesting_js_describe_1 : id = "describe_1"
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"description":"This is a sample test suite."}}
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"lead":{"name":"Samantha Jones","email":"samantha.jones@example.com"},"description":"This is a sample test suite."}}
object "HookDefinitionMetadata" as tests_hook_nesting_js_hook_1 #fdd
tests_hook_nesting_js_hook_1 : id = "hook_1"
tests_hook_nesting_js_hook_1 : data = {"vendor":{"description":"Prepare the environment"}}
Expand Down Expand Up @@ -5163,7 +5163,7 @@ tests_hook_nesting_js_hook_0 : id = "hook_0"
tests_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_hook_nesting_js_describe_1 #ded
tests_hook_nesting_js_describe_1 : id = "describe_1"
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"description":"This is a sample test suite."}}
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"lead":{"name":"Samantha Jones","email":"samantha.jones@example.com"},"description":"This is a sample test suite."}}
object "HookDefinitionMetadata" as tests_hook_nesting_js_hook_1 #fdd
tests_hook_nesting_js_hook_1 : id = "hook_1"
tests_hook_nesting_js_hook_1 : data = {"vendor":{"description":"Prepare the environment"}}
Expand Down Expand Up @@ -6000,7 +6000,7 @@ tests_hook_nesting_js_hook_0 : id = "hook_0"
tests_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_hook_nesting_js_describe_1 #ded
tests_hook_nesting_js_describe_1 : id = "describe_1"
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"description":"This is a sample test suite."}}
tests_hook_nesting_js_describe_1 : data = {"vendor":{"maintainer":{"name":"Jane Smith","email":"jane.smith@example.com"},"lead":{"name":"Samantha Jones","email":"samantha.jones@example.com"},"description":"This is a sample test suite."}}
object "HookDefinitionMetadata" as tests_hook_nesting_js_hook_1 #fdd
tests_hook_nesting_js_hook_1 : id = "hook_1"
tests_hook_nesting_js_hook_1 : data = {"vendor":{"description":"Prepare the environment"}}
Expand Down
32 changes: 22 additions & 10 deletions packages/library/src/metadata/containers/BaseMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ export abstract class BaseMetadata implements Metadata {
get(): Readonly<Data>;
get(path: string | readonly string[], fallbackValue?: unknown): unknown;
get(path?: string | readonly string[], fallbackValue?: unknown): Data | unknown {
return Object.freeze(
path ? lodashGet(this[symbols.data], path, fallbackValue) : this[symbols.data],
);
return Object.freeze(this.#get(path, fallbackValue));
}

set(path: string | readonly string[], value: unknown): this {
if (path == null) {
throw new TypeError('Path is required for set operation');
}

lodashSet(this[symbols.data], path, value);
this.#set(path, value);

this[symbols.context].emitter.emit({
type: 'set_metadata',
Expand All @@ -59,7 +57,7 @@ export abstract class BaseMetadata implements Metadata {

push(path: string | readonly string[], values: unknown[]): this {
if (path == null) {
throw new TypeError('Path is required for set operation');
throw new TypeError('Path is required for push operation');
}

if (!Array.isArray(values)) {
Expand All @@ -76,7 +74,7 @@ export abstract class BaseMetadata implements Metadata {
}

array.push(...values);
lodashSet(this[symbols.data], path, array);
this.#set(path, array);

this[symbols.context].emitter.emit({
type: 'set_metadata',
Expand All @@ -90,10 +88,13 @@ export abstract class BaseMetadata implements Metadata {
return this;
}

assign(path: undefined | string | readonly string[], value: Data): this {
const oldValue = path === undefined ? this.get() : this.get(path, {});
assign(path: undefined | string | readonly string[], value: object): this {
const oldValue = this.#get(path, {});
const source = oldValue && typeof oldValue === 'object' ? oldValue : {};
Object.assign(source, value);
if (path != null) {
this.#set(path, source);
}

this[symbols.context].emitter.emit({
type: 'set_metadata',
Expand All @@ -107,10 +108,13 @@ export abstract class BaseMetadata implements Metadata {
return this;
}

merge(path: undefined | string | readonly string[], value: Data): this {
const oldValue = path === undefined ? this.get() : this.get(path, {});
merge(path: undefined | string | readonly string[], value: object): this {
const oldValue = this.#get(path, {});
const source = oldValue && typeof oldValue === 'object' ? oldValue : {};
lodashMerge(source, value);
if (path != null) {
this.#set(path, source);
}

this[symbols.context].emitter.emit({
type: 'set_metadata',
Expand All @@ -123,4 +127,12 @@ export abstract class BaseMetadata implements Metadata {

return this;
}

#get(path?: string | readonly string[], fallbackValue?: unknown): Data | unknown {
return path == null ? this[symbols.data] : lodashGet(this[symbols.data], path, fallbackValue);
}

#set(path: string | readonly string[], value: unknown): void {
lodashSet(this[symbols.data], path, value);
}
}
6 changes: 4 additions & 2 deletions packages/recorder/__tests__/hook-nesting.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { metadata, $Set, $Push } = require('jest-metadata');
const { metadata, $Assign, $Push, $Set, $Merge } = require('jest-metadata');

let now = 1672524000000;

Expand All @@ -13,13 +13,15 @@ const actions = {
}

const $Description = (text) => $Set('vendor.description', text);
const $Maintainer = (name, email) => $Set('vendor.maintainer', { name, email });
const $Maintainer = (name, email) => $Assign('vendor.maintainer', { name, email });
const $Lead = (name, email) => $Merge('vendor.lead', { name, email });
const $Tag = (value) => $Push(['vendor', 'labels'], value);
const $Flaky = () => $Tag('flaky');

const step = (text) => metadata.push('vendor.steps', [{ text, startedAt: now }]);

$Maintainer('Jane Smith', 'jane.smith@example.com');
$Lead('Samantha Jones', 'samantha.jones@example.com');
$Description('This is a sample test suite.');
describe('Login flow', () => {
$Description('Prepare the environment');
Expand Down

0 comments on commit fc8a5c0

Please sign in to comment.