diff --git a/.changeset/lcam-1282-test-metadata-uuid-fallback.md b/.changeset/lcam-1282-test-metadata-uuid-fallback.md new file mode 100644 index 00000000..eeaa3a30 --- /dev/null +++ b/.changeset/lcam-1282-test-metadata-uuid-fallback.md @@ -0,0 +1,5 @@ +--- +"@wdio/browserstack-service": patch +--- + +- Fixed custom test metadata set via `BrowserStackSDK.setTestMetadata()` being dropped when the metadata is set before the test-run UUID is assigned. A per-UUID lookup now falls back to the current-run metadata again, restoring the behaviour that shipped before 8.51.0. diff --git a/.changeset/pr-243.md b/.changeset/pr-243.md new file mode 100644 index 00000000..0d2a0a70 --- /dev/null +++ b/.changeset/pr-243.md @@ -0,0 +1,5 @@ +--- +"@wdio/browserstack-service": patch +--- + +- Fixed custom test metadata set via `BrowserStackSDK.setTestMetadata()` before the test-run UUID is assigned being dropped for App Low-Code runs. diff --git a/packages/browserstack-service/src/metadata.ts b/packages/browserstack-service/src/metadata.ts index 1554da6e..3ce83bb1 100644 --- a/packages/browserstack-service/src/metadata.ts +++ b/packages/browserstack-service/src/metadata.ts @@ -14,6 +14,7 @@ class TestMetadata { static set(metadata: Metadata = {}) { if (!getCentralUser().app_lcnc) { + BStackLogger.warn(`setTestMetadata: ignored, BROWSERSTACK_CENTRAL_USER=${process.env.BROWSERSTACK_CENTRAL_USER}`) return } @@ -31,6 +32,7 @@ class TestMetadata { if (TestMetadata.currentTestRunUuid) { TestMetadata.metadataByTestRunUuid[TestMetadata.currentTestRunUuid] = metadata } + BStackLogger.debug(`setTestMetadata: identifier=${testRunIdentifier} storedUnderUuid=${TestMetadata.currentTestRunUuid} store=[${Object.keys(TestMetadata.metadataByTestRunUuid)}]`) } static get(testRunUuid?: string): Metadata { @@ -39,7 +41,9 @@ class TestMetadata { } if (testRunUuid) { - return TestMetadata.metadataByTestRunUuid[testRunUuid] || {} + const metadata = TestMetadata.metadataByTestRunUuid[testRunUuid] || TestMetadata.fallbackMetadata || {} + BStackLogger.debug(`TestMetadata.get: uuid=${testRunUuid} identifier=${metadata.identifier} store=[${Object.keys(TestMetadata.metadataByTestRunUuid)}]`) + return metadata } return TestMetadata.fallbackMetadata || {} diff --git a/packages/browserstack-service/tests/metadata.test.ts b/packages/browserstack-service/tests/metadata.test.ts index 2d591676..a6ad2b6b 100644 --- a/packages/browserstack-service/tests/metadata.test.ts +++ b/packages/browserstack-service/tests/metadata.test.ts @@ -68,11 +68,14 @@ describe('TestMetadata', () => { }) describe('fallback vs per-uuid storage', () => { - it('stores as fallback for the no-uuid (current-run) lookup, but not for unknown uuids', () => { + it('stores as fallback for the no-uuid (current-run) lookup', () => { TestMetadata.set({ identifier: 'run-1' }) expect(TestMetadata.get()).toEqual({ identifier: 'run-1' }) - // A per-uuid lookup must not leak the current-run fallback. - expect(TestMetadata.get('unknown-uuid')).toEqual({}) + }) + + it('serves the fallback to a uuid lookup when the uuid was learned after set()', () => { + TestMetadata.set({ identifier: 'run-1' }) + expect(TestMetadata.get('uuid-assigned-later')).toEqual({ identifier: 'run-1' }) }) it('stores per-uuid when a current test-run uuid is set', () => { @@ -81,9 +84,17 @@ describe('TestMetadata', () => { expect(TestMetadata.get('uuid-1')).toEqual({ identifier: 'run-1' }) }) - it('returns {} for an unknown uuid instead of leaking another run\'s metadata', () => { + it('falls back to the current-run metadata for an unrecognised uuid', () => { TestMetadata.setCurrentTestRunUuid('uuid-1') TestMetadata.set({ identifier: 'run-1' }) + expect(TestMetadata.get('uuid-2')).toEqual({ identifier: 'run-1' }) + }) + + it('does not carry a previous run\'s metadata once reset() has run between tests', () => { + TestMetadata.setCurrentTestRunUuid('uuid-1') + TestMetadata.set({ identifier: 'run-1' }) + TestMetadata.reset() + TestMetadata.setCurrentTestRunUuid('uuid-2') expect(TestMetadata.get('uuid-2')).toEqual({}) })