Skip to content

Commit

Permalink
feat: .defaults, operation
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Feb 15, 2024
1 parent 26c1dc5 commit bf607cc
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 18 deletions.
22 changes: 22 additions & 0 deletions e2e/__fixtures__/27.x.x/env-1/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@
},
"operation": "merge"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "John Doe",
"email": "john.doe@example.com"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "impostor@example.fake"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
22 changes: 22 additions & 0 deletions e2e/__fixtures__/27.x.x/env-N/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@
},
"operation": "merge"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "John Doe",
"email": "john.doe@example.com"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "impostor@example.fake"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
22 changes: 22 additions & 0 deletions e2e/__fixtures__/28.x.x/env-1/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@
},
"operation": "merge"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "John Doe",
"email": "john.doe@example.com"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "impostor@example.fake"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
22 changes: 22 additions & 0 deletions e2e/__fixtures__/28.x.x/env-N/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@
},
"operation": "merge"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "John Doe",
"email": "john.doe@example.com"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "impostor@example.fake"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
22 changes: 22 additions & 0 deletions e2e/__fixtures__/29.x.x/env-1/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@
},
"operation": "merge"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "John Doe",
"email": "john.doe@example.com"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "impostor@example.fake"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
22 changes: 22 additions & 0 deletions e2e/__fixtures__/29.x.x/env-N/hook-nesting.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@
},
"operation": "merge"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "John Doe",
"email": "john.doe@example.com"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "impostor@example.fake"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
5 changes: 4 additions & 1 deletion e2e/__tests__/default/hook-nesting.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { metadata, $Assign, $Push, $Set, $Merge, $Unshift } = require('jest-metadata');
const { metadata, $Assign, $Defaults, $Push, $Set, $Merge, $Unshift } = require('jest-metadata');

let now = 1672524000000;

Expand All @@ -14,6 +14,7 @@ const actions = {

const $Description = (text) => $Set('vendor.description', text);
const $Maintainer = (name, email) => $Assign('vendor.maintainer', { name, email });
const $Author = (name, email) => $Defaults('vendor.author', { name, email });
const $Lead = (name, email) => $Merge('vendor.lead', { name, email });
const $Tag = (value) => $Push(['vendor', 'labels'], value);
const $Flaky = () => $Unshift(['vendor', 'labels'], 'flaky');
Expand All @@ -22,6 +23,8 @@ const step = (text) => metadata.push('vendor.steps', [{ text, startedAt: now }])

$Maintainer('Jane Smith', 'jane.smith@example.com');
$Lead('Samantha Jones', 'samantha.jones@example.com');
$Author('John Doe', 'john.doe@example.com');
$Author('Impostor', 'impostor@example.fake');
$Description('This is a sample test suite.');
describe('Login flow', () => {
$Description('Prepare the environment');
Expand Down
3 changes: 2 additions & 1 deletion package-e2e/test.cjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const assert = require('assert');

const { metadata, state, $Set, $Push, $Merge, $Assign, $Unshift } = require('jest-metadata');
const { metadata, state, $Set, $Push, $Merge, $Assign, $Defaults, $Unshift } = require('jest-metadata');
assert(typeof metadata === 'object', 'jest-metadata should export `metadata` object');
assert(typeof state === 'object', 'jest-metadata should export `state` object');
assert(typeof $Set === 'function', 'jest-metadata should export $Set function as a named export');
assert(typeof $Push === 'function', 'jest-metadata should export $Push function as a named export');
assert(typeof $Merge === 'function', 'jest-metadata should export $Merge function as a named export');
assert(typeof $Assign === 'function', 'jest-metadata should export $Assign function as a named export');
assert(typeof $Defaults === 'function', 'jest-metadata should export $Defaults function as a named export');
assert(typeof $Unshift === 'function', 'jest-metadata should export $Unshift function as a named export');

const { events } = require('jest-metadata/debug');
Expand Down
3 changes: 2 additions & 1 deletion package-e2e/test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import { $Set, $Push, $Merge, $Assign, $Unshift, state, metadata } from 'jest-metadata';
import { $Set, $Push, $Merge, $Assign, $Defaults, $Unshift, state, metadata } from 'jest-metadata';
import { events } from 'jest-metadata/debug';
import JsdomTestEnvironment from 'jest-metadata/environment-jsdom';
import NodeTestEnvironment from 'jest-metadata/environment-node';
Expand All @@ -12,6 +12,7 @@ assert(typeof $Set === 'function', 'jest-metadata should export `$Set` function
assert(typeof $Push === 'function', 'jest-metadata should export `$Push` function as a named export');
assert(typeof $Merge === 'function', 'jest-metadata should export `$Merge` function as a named export');
assert(typeof $Assign === 'function', 'jest-metadata should export `$Assign` function as a named export');
assert(typeof $Defaults === 'function', 'jest-metadata should export `$Defaults` function as a named export');
assert(typeof $Unshift === 'function', 'jest-metadata should export `$Unshift` function as a named export');

assert(typeof events === 'object', 'jest-metadata/debug should export `events` object');
Expand Down
25 changes: 19 additions & 6 deletions package-e2e/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $Set, $Push, $Merge, $Assign, $Unshift, state, metadata } from 'jest-metadata';
import { $Set, $Push, $Merge, $Assign, $Defaults, $Unshift, state, metadata } from 'jest-metadata';
import { events } from 'jest-metadata/debug';
import type { GlobalMetadata, Metadata } from 'jest-metadata';
import JestMetadataReporter, { query, JestMetadataReporter as JestMetadataReporterNamed } from 'jest-metadata/reporter';
Expand All @@ -12,11 +12,24 @@ function assertType<T>(_actual: T, _other?: T): void {

assertType<GlobalMetadata>(state);
assertType<Metadata>(metadata);
assertType<Function>($Set);
assertType<Function>($Push);
assertType<Function>($Merge);
assertType<Function>($Assign);
assertType<Function>($Unshift);

$Set('path', 'value' as unknown);
$Set(['path'], 'value' as unknown);

$Push('path', -1, '2', true);
$Push(['path'], -1, '2', true);

$Unshift('path', -1, '2', true);
$Unshift(['path'], -1, '2', true);

$Merge('path', { key: 'value' });
$Merge(['path'], { key: 'value' });

$Assign('path', { key: 'value' });
$Assign(['path'], { key: 'value' });

$Defaults('path', { key: 'value' });
$Defaults(['path'], { key: 'value' });

assertType<object>(events);

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@
},
"homepage": "https://github.com/wix-incubator/jest-metadata#readme",
"dependencies": {
"bunyamin": "^1.5.0",
"bunyamin": "^1.5.2",
"funpermaproxy": "^1.1.0",
"jest-environment-emit": "^1.0.1",
"jest-environment-emit": "^1.0.6",
"lodash.merge": "^4.6.2",
"node-ipc": "9.2.1",
"strip-ansi": "^6.0.0",
Expand Down Expand Up @@ -133,7 +133,6 @@
"@jest/reporters": "^29.3.1",
"@jest/types": "^29.3.1",
"@types/bunyan": "^1.8.8",
"@types/glob": "^8.0.0",
"@types/jest": "^29.2.5",
"@types/lodash": "^4.14.191",
"@types/lodash.get": "^4.4.7",
Expand All @@ -157,6 +156,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-unicorn": "^48.0.1",
"globby":"^11.1.0",
"http-server": "^14.1.1",
"husky": "^8.0.3",
"is-ci": "^3.0.1",
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export const $Unshift = realm.metadataDSL.$Unshift;
*/
export const $Assign = realm.metadataDSL.$Assign;

/**
* Pseudo-annotation that allows to associate metadata with a test block.
* It is not an ECMAScript decorator, but it behaves similarly.
* Use it to ensure multiple placeholder values to an object in metadata.
*/
export const $Defaults = realm.metadataDSL.$Defaults;

/**
* Pseudo-annotation that allows to associate metadata with a test block.
* It is not an ECMAScript decorator, but it behaves similarly.
Expand Down
6 changes: 3 additions & 3 deletions src/metadata/__tests__/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ tests_default_hook_nesting_js_hook_0 : id = "hook_0"
tests_default_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_default_hook_nesting_js_describe_1 #ded
tests_default_hook_nesting_js_describe_1 : id = "describe_1"
tests_default_hook_nesting_js_describe_1 : data = {\\n "vendor": {\\n "maintainer": {\\n "name": "Jane Smith",\\n "email": "jane.smith@example.com"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "samantha.jones@example.com"\\n },\\n "description": "This is a sample test suite."\\n }\\n}
tests_default_hook_nesting_js_describe_1 : data = {\\n "vendor": {\\n "maintainer": {\\n "name": "Jane Smith",\\n "email": "jane.smith@example.com"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "samantha.jones@example.com"\\n },\\n "author": {\\n "name": "John Doe",\\n "email": "john.doe@example.com"\\n },\\n "description": "This is a sample test suite."\\n }\\n}
object "HookDefinitionMetadata" as tests_default_hook_nesting_js_hook_1 #fdd
tests_default_hook_nesting_js_hook_1 : id = "hook_1"
tests_default_hook_nesting_js_hook_1 : data = {\\n "vendor": {\\n "description": "Prepare the environment"\\n }\\n}
Expand Down Expand Up @@ -1562,7 +1562,7 @@ tests_default_hook_nesting_js_hook_0 : id = "hook_0"
tests_default_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_default_hook_nesting_js_describe_1 #ded
tests_default_hook_nesting_js_describe_1 : id = "describe_1"
tests_default_hook_nesting_js_describe_1 : data = {\\n "vendor": {\\n "maintainer": {\\n "name": "Jane Smith",\\n "email": "jane.smith@example.com"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "samantha.jones@example.com"\\n },\\n "description": "This is a sample test suite."\\n }\\n}
tests_default_hook_nesting_js_describe_1 : data = {\\n "vendor": {\\n "maintainer": {\\n "name": "Jane Smith",\\n "email": "jane.smith@example.com"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "samantha.jones@example.com"\\n },\\n "author": {\\n "name": "John Doe",\\n "email": "john.doe@example.com"\\n },\\n "description": "This is a sample test suite."\\n }\\n}
object "HookDefinitionMetadata" as tests_default_hook_nesting_js_hook_1 #fdd
tests_default_hook_nesting_js_hook_1 : id = "hook_1"
tests_default_hook_nesting_js_hook_1 : data = {\\n "vendor": {\\n "description": "Prepare the environment"\\n }\\n}
Expand Down Expand Up @@ -2536,7 +2536,7 @@ tests_default_hook_nesting_js_hook_0 : id = "hook_0"
tests_default_hook_nesting_js_hook_0 : hookType = "beforeEach"
object "DescribeBlockMetadata" as tests_default_hook_nesting_js_describe_1 #ded
tests_default_hook_nesting_js_describe_1 : id = "describe_1"
tests_default_hook_nesting_js_describe_1 : data = {\\n "vendor": {\\n "maintainer": {\\n "name": "Jane Smith",\\n "email": "jane.smith@example.com"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "samantha.jones@example.com"\\n },\\n "description": "This is a sample test suite."\\n }\\n}
tests_default_hook_nesting_js_describe_1 : data = {\\n "vendor": {\\n "maintainer": {\\n "name": "Jane Smith",\\n "email": "jane.smith@example.com"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "samantha.jones@example.com"\\n },\\n "author": {\\n "name": "John Doe",\\n "email": "john.doe@example.com"\\n },\\n "description": "This is a sample test suite."\\n }\\n}
object "HookDefinitionMetadata" as tests_default_hook_nesting_js_hook_1 #fdd
tests_default_hook_nesting_js_hook_1 : id = "hook_1"
tests_default_hook_nesting_js_hook_1 : data = {\\n "vendor": {\\n "description": "Prepare the environment"\\n }\\n}
Expand Down
24 changes: 24 additions & 0 deletions src/metadata/containers/BaseMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ export abstract class BaseMetadata implements Metadata {
return this;
}

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

this[symbols.context].emitter.emit({
type: 'write_metadata',
testFilePath: this[symbols.id].testFilePath,
targetId: this[symbols.id].identifier,
path,
value,
operation: 'defaults',
});

return this;
}

merge(path: undefined | string | readonly string[], value: object): this {
const oldValue = this.#get(path, {});
const source = oldValue && typeof oldValue === 'object' ? oldValue : {};
Expand Down
9 changes: 9 additions & 0 deletions src/metadata/dsl/MetadataDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ export class MetadataDSL {
});
};

$Defaults = (path: string | readonly string[] | undefined, value: Data): void => {
this.#assertPath(path);
this.#assertValue(value);

this.schedule(() => {
this.#metadata().defaults(path, value);
});
};

$Merge = (path: string | readonly string[] | undefined, value: Data): void => {
this.#assertPath(path);
this.#assertValue(value);
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/events/WriteMetadataEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export type WriteMetadataEvent = {
targetId: string; // instance ID
path?: string | readonly string[];
value: unknown;
operation: 'set' | 'assign' | 'merge' | 'push' | 'unshift';
operation: 'set' | 'assign' | 'defaults' | 'merge' | 'push' | 'unshift';
};
Loading

0 comments on commit bf607cc

Please sign in to comment.