Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metadata.defaults and $Defaults #72

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ jobs:
- name: Build package for E2E
run: npm run build:e2e

- name: Packaging tests - Install
uses: bahmutov/npm-install@v1
with:
useLockFile: false
working-directory: package-e2e

- name: Packaging tests - Run
run: npm test
working-directory: package-e2e
- name: Packaging tests
run: scripts/test-package-e2e.sh

- name: E2E tests - Install
uses: bahmutov/npm-install@v1
Expand Down
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": "[email protected]"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "[email protected]"
},
"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": "[email protected]"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "[email protected]"
},
"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": "[email protected]"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "[email protected]"
},
"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": "[email protected]"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "[email protected]"
},
"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": "[email protected]"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "[email protected]"
},
"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": "[email protected]"
},
"operation": "defaults"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_1",
"path": "vendor.author",
"value": {
"name": "Impostor",
"email": "[email protected]"
},
"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', '[email protected]');
$Lead('Samantha Jones', '[email protected]');
$Author('John Doe', '[email protected]');
$Author('Impostor', '[email protected]');
$Description('This is a sample test suite.');
describe('Login flow', () => {
$Description('Prepare the environment');
Expand Down
2 changes: 2 additions & 0 deletions package-e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"test:ts": "tsc"
},
"dependencies": {
"jest": "^29.0.0",
"jest-environment-jsdom": "^29.0.0",
"jest-metadata": "file:../package.tar.gz"
}
}
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
12 changes: 12 additions & 0 deletions scripts/test-package-e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

set -e

TEMP_DIR=$(mktemp -d)
cp package.tar.gz $TEMP_DIR
rm -rf $TEMP_DIR/package-e2e
cp -r package-e2e $TEMP_DIR/package-e2e

cd $TEMP_DIR/package-e2e
npm install
npm test
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": "[email protected]"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "[email protected]"\\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": "[email protected]"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "[email protected]"\\n },\\n "author": {\\n "name": "John Doe",\\n "email": "[email protected]"\\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": "[email protected]"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "[email protected]"\\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": "[email protected]"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "[email protected]"\\n },\\n "author": {\\n "name": "John Doe",\\n "email": "[email protected]"\\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": "[email protected]"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "[email protected]"\\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": "[email protected]"\\n },\\n "lead": {\\n "name": "Samantha Jones",\\n "email": "[email protected]"\\n },\\n "author": {\\n "name": "John Doe",\\n "email": "[email protected]"\\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
Loading
Loading