Skip to content

QUESTION: using AroundNode to cleanup after tests #1589

@zhulik

Description

@zhulik

Hi, I've just discovered the new AroundNode feature and found it's absolutely awesome, thanks a lot! Yet it may be far more useful.

In according to the docs, an AroundNode when applied to a container, is being executed even for BeforeEach nodes, and this makes it unusable to cleanup after(or before) tests.

For instance, I create some database records in a BeforeEach node and use DeferCleanup to clean the database after:

// root Describe
BeforeEach(func(ctx SpecContext) {
	tenant = factory.CreateTenant(ctx)

	DeferCleanup(func(ctx SpecContext) {
		testhelpers.CleanDB(ctx, db)
	})
})

Since I have a lot of suites working with the database, I don't really want to repeat the same DeferCleanup over and over again and AroundNode would a good place to truncate the database if there was a way to distinguish what node it wraps.

Currently, if I define a decorator like

// Should be used as Describe("SomeDBService", Serial, testhelpers.WithDB(), func...
func WithDB() types.AroundNodeDecorator {
	return ginkgo.AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
		defer CleanDB(ctx, db)
		body(ctx)
	})
}

it will truncate the database after the BeforeEach node, so the test itself will see a clean database. The problem is that not all my tests interact with the database, so I don't need and want to truncate after every single test, that's why a decorator like I described above is useful, I can only apply it to specific containers.

I'm wondering if there is way to know what node an AroundNode wraps so I can only clean the database after actual tests and not after setup nodes.

Maybe there is a different way to achieve what I want?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions