Skip to content

Commit

Permalink
fix: add_hook and add_test handling
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Apr 27, 2024
1 parent 0f3552f commit f21fd9e
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 1 deletion.
8 changes: 8 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 @@ -18,6 +18,14 @@
"hookId": "hook_0",
"hookType": "beforeEach"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_0",
"path": "vendor.filename",
"value": "hook-nesting.js",
"operation": "set"
},
{
"type": "start_describe_definition",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
8 changes: 8 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 @@ -18,6 +18,14 @@
"hookId": "hook_0",
"hookType": "beforeEach"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_0",
"path": "vendor.filename",
"value": "hook-nesting.js",
"operation": "set"
},
{
"type": "start_describe_definition",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
8 changes: 8 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 @@ -18,6 +18,14 @@
"hookId": "hook_0",
"hookType": "beforeEach"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_0",
"path": "vendor.filename",
"value": "hook-nesting.js",
"operation": "set"
},
{
"type": "start_describe_definition",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
8 changes: 8 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 @@ -18,6 +18,14 @@
"hookId": "hook_0",
"hookType": "beforeEach"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_0",
"path": "vendor.filename",
"value": "hook-nesting.js",
"operation": "set"
},
{
"type": "start_describe_definition",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
8 changes: 8 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 @@ -18,6 +18,14 @@
"hookId": "hook_0",
"hookType": "beforeEach"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_0",
"path": "vendor.filename",
"value": "hook-nesting.js",
"operation": "set"
},
{
"type": "start_describe_definition",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
8 changes: 8 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 @@ -18,6 +18,14 @@
"hookId": "hook_0",
"hookType": "beforeEach"
},
{
"type": "write_metadata",
"testFilePath": "__tests__/default/hook-nesting.js",
"targetId": "describe_0",
"path": "vendor.filename",
"value": "hook-nesting.js",
"operation": "set"
},
{
"type": "start_describe_definition",
"testFilePath": "__tests__/default/hook-nesting.js",
Expand Down
7 changes: 6 additions & 1 deletion e2e/__tests__/default/hook-nesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ const $Flaky = () => $Unshift(['vendor', 'labels'], 'flaky');

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

// Should be attached to ROOT_DESCRIBE_BLOCK
metadata.set('vendor.filename', 'hook-nesting.js');

$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', () => {
// ↑ Should be attached to `describe` block
metadata.set('vendor.description', 'This is a sample test suite.');

$Description('Prepare the environment');
beforeAll(() => {
step('Open the browser');
Expand Down
6 changes: 6 additions & 0 deletions src/environment-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const listener: EnvironmentListenerFn = (context) => {
realm.environmentHandler.handleTestEvent(event, state);
};

const backToDescribe = () => {
realm.metadataHandler.backToDescribe(testFilePath);
};

const flushHandler = () => realm.ipc.flush();

context.testEvents
Expand Down Expand Up @@ -79,6 +83,8 @@ const listener: EnvironmentListenerFn = (context) => {
.on('finish_describe_definition', testEventHandler, Number.MAX_SAFE_INTEGER)
.on('add_hook', testEventHandler, -1)
.on('add_test', testEventHandler, -1)
.on('add_hook', backToDescribe, Number.MAX_SAFE_INTEGER)
.on('add_test', backToDescribe, Number.MAX_SAFE_INTEGER)
.on('run_start', testEventHandler, -1)
.on('run_start', flushHandler, Number.MAX_SAFE_INTEGER)
.on('run_describe_start', testEventHandler, -1)
Expand Down
10 changes: 10 additions & 0 deletions src/metadata/MetadataEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,14 @@ export class MetadataEventHandler {
const handler = this._handlers[event.type] as (event: MetadataEvent) => void;
handler(event);
};

backToDescribe = (testFilePath: string): void => {
const file = this._metadata.getTestFileMetadata(testFilePath);
const currentDescribeBlock = file.current.describeBlock;
if (!currentDescribeBlock) {
throw new JestMetadataError('No current describe block');
}

file[internal.currentMetadata] = currentDescribeBlock;
};
}
3 changes: 3 additions & 0 deletions src/metadata/__tests__/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ global_metadata : id = "globalMetadata"
object "TestFileMetadata" as tests_default_hook_nesting_js #def
object "DescribeBlockMetadata" as tests_default_hook_nesting_js_describe_0 #ded
tests_default_hook_nesting_js_describe_0 : id = "describe_0"
tests_default_hook_nesting_js_describe_0 : data = {\\n "vendor": {\\n "filename": "hook-nesting.js"\\n }\\n}
object "HookDefinitionMetadata" as tests_default_hook_nesting_js_hook_0 #fdd
tests_default_hook_nesting_js_hook_0 : id = "hook_0"
tests_default_hook_nesting_js_hook_0 : hookType = "beforeEach"
Expand Down Expand Up @@ -1557,6 +1558,7 @@ global_metadata : id = "globalMetadata"
object "TestFileMetadata" as tests_default_hook_nesting_js #def
object "DescribeBlockMetadata" as tests_default_hook_nesting_js_describe_0 #ded
tests_default_hook_nesting_js_describe_0 : id = "describe_0"
tests_default_hook_nesting_js_describe_0 : data = {\\n "vendor": {\\n "filename": "hook-nesting.js"\\n }\\n}
object "HookDefinitionMetadata" as tests_default_hook_nesting_js_hook_0 #fdd
tests_default_hook_nesting_js_hook_0 : id = "hook_0"
tests_default_hook_nesting_js_hook_0 : hookType = "beforeEach"
Expand Down Expand Up @@ -2531,6 +2533,7 @@ global_metadata : id = "globalMetadata"
object "TestFileMetadata" as tests_default_hook_nesting_js #def
object "DescribeBlockMetadata" as tests_default_hook_nesting_js_describe_0 #ded
tests_default_hook_nesting_js_describe_0 : id = "describe_0"
tests_default_hook_nesting_js_describe_0 : data = {\\n "vendor": {\\n "filename": "hook-nesting.js"\\n }\\n}
object "HookDefinitionMetadata" as tests_default_hook_nesting_js_hook_0 #fdd
tests_default_hook_nesting_js_hook_0 : id = "hook_0"
tests_default_hook_nesting_js_hook_0 : hookType = "beforeEach"
Expand Down

0 comments on commit f21fd9e

Please sign in to comment.