Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ function createSuiteCollector(
: options.todo
? 'todo'
: 'run',
meta: options.meta ?? Object.create(null),
meta: {
Copy link
Copy Markdown
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,
}

...Object.create(null),
Comment thread
rebasecase marked this conversation as resolved.
Outdated
...(suiteOptions?.meta || {}),
...(options.meta || {}),
},
annotations: [],
}
const handler = options.handler
Expand Down
4 changes: 4 additions & 0 deletions packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ export interface TestOptions {
* Whether the test is expected to fail. If it does, the test will pass, otherwise it will fail.
*/
fails?: boolean
/**
* Custom metadata for the task. This will be merged with any meta property defined in the test.
*/
meta?: Record<string, unknown>
Comment thread
rebasecase marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

}

interface ExtendedAPI<ExtraContext> {
Expand Down
69 changes: 69 additions & 0 deletions test/core/test/test-meta-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { beforeEach, describe, expect, test } from 'vitest'

describe('TestOptions meta property functionality', { meta: { suiteLevel: 'test-suite', priority: 'medium' } }, () => {
let beforeEachMeta: Record<string, unknown>

beforeEach(({ task }) => {
beforeEachMeta = { ...task.meta }
})

test('should merge suite and test meta properties', { meta: { testLevel: 'individual-test', priority: 'high' } }, ({ task }) => {
// Test should have both suite and test meta
expect(task.meta).toMatchObject({
suiteLevel: 'test-suite',
testLevel: 'individual-test',
priority: 'high', // test meta should override suite meta
})

// beforeEach should have access to merged meta
expect(beforeEachMeta).toMatchObject({
suiteLevel: 'test-suite',
testLevel: 'individual-test',
priority: 'high',
})
})

test('should inherit suite meta when no test meta provided', ({ task }) => {
// Test should only have suite meta
expect(task.meta).toMatchObject({
suiteLevel: 'test-suite',
priority: 'medium',
})

// beforeEach should have access to suite meta
expect(beforeEachMeta).toMatchObject({
suiteLevel: 'test-suite',
priority: 'medium',
})
})

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Author

Choose a reason for hiding this comment

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

I was wrong 😿


expect(task.meta).toMatchObject({
suiteLevel: 'test-suite',
testLevel: 'runtime-test',
priority: 'medium',
runtimeAdded: 'added-during-test',
})
})
})

describe('Suite without meta', () => {
let beforeEachMeta: Record<string, unknown>

beforeEach(({ task }) => {
beforeEachMeta = { ...task.meta }
})

test('should only have test meta when suite has no meta', { meta: { testOnly: 'test-meta' } }, ({ task }) => {
expect(task.meta).toMatchObject({
testOnly: 'test-meta',
})

expect(beforeEachMeta).toMatchObject({
testOnly: 'test-meta',
})
})
})
Loading