Skip to content

Conversation

rebasecase
Copy link

@rebasecase rebasecase commented Aug 7, 2025

Description

Related issue: #8404

This PR allows assiging data to the meta context property via TestOptions. Properties assigned here will be available in beforeEach. Properties defined in the test body will override these, maintaining current behaviour

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@rebasecase rebasecase changed the title feat 8404 Add metadata to feat 8404 Add metadata to TestOptions Aug 7, 2025
Copy link

netlify bot commented Aug 7, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 4d0b2bd
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/689f770d9f1ef20008c5d52c
😎 Deploy Preview https://deploy-preview-8405--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@rebasecase rebasecase changed the title feat 8404 Add metadata to TestOptions feat: Addmeta to TestOptions Aug 7, 2025
@rebasecase rebasecase changed the title feat: Addmeta to TestOptions feat: Add meta to TestOptions Aug 7, 2025
@rebasecase
Copy link
Author

rebasecase commented Aug 7, 2025

  • Need to consider assertion of ctx.task.suite.meta !== ctx.task.meta
    Now can distinguish between suite and task meta

@rebasecase
Copy link
Author

rebasecase commented Aug 7, 2025

  • Need to consider nested describes, how would that work? How does the meta cascade, if at all?

Now cascades from grandparents

/**
* Custom metadata for the task. This will be merged with any meta property defined in the test.
*/
meta?: Record<string, unknown>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be Partial<TaskMeta>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some places it is Record<string, unknown> should these all be Partial<TaskMeta>?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

meta?: Record<string, any> | null

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what to do about this. Setting Partial<TaskMeta> cause a lot of errors now 😢 Does TaskMeta want to be extends Record<string, any> ?

I don't understand complex typing much yet.


test('should allow adding meta at runtime', { meta: { testLevel: 'runtime-test' } }, ({ task }) => {
// Add meta at runtime
(task.meta as any).runtimeAdded = 'added-during-test'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a local type instead of assigning any

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With meta now being Partial<TaskMeta> we can now assign the property directly without any types? is that okay?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong 😿

@rebasecase
Copy link
Author

@sheremet-va Thank you for your review!

The comments on Object.create(null), do not make much sense to me yet - I am rather new to TS. I kept it there because it was there before.

Object.create(null) sets a prototype to an object which "is" null, but has all the available interfaces to an "Object"? The spread operator here does nothing since this "null object" has no properties to spread?

So in this instance it can be left out?

I shall fix this 😸

@rebasecase rebasecase requested a review from sheremet-va August 15, 2025 18:17
/**
* Custom metadata for the task that will be assigned to `task.meta`.
*/
meta?: Record<string, unknown>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sheremet-va This is what I meant here

? 'todo'
: 'run',
meta: options.meta ?? Object.create(null),
meta: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the meta with a null prototype. Just assign options.meta to meta collected with collectAncestorMeta:

const testMeta = collectAncestorMeta(collectorContext.currentSuite?.suite)
if(options.meta) {
  Object.assign(testMeta, options.meta)
}

{
  // ...
  meta: testMeta,
}

shuffle: suiteOptions?.shuffle,
tasks: [],
meta: Object.create(null),
meta: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants