Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@markkaylor
Copy link
Contributor

@markkaylor markkaylor commented Nov 6, 2025

What does it do?

Add setupDatabaseReset utility. Accomplishes the same goal as testInTransaction by resetting the database after each test has run. It's slower but more reliable and is not vulnerable to test failures caused by function calls outside the test's transaction context when using testInTransaction.

Updates the docker file to spin up test databases since it was throwing an error when starting

Why is it needed?

testInTransaction often causes API tests to fail since some async code is intentionally fired without await and then tries to execute after the test's transaction context has closed

For example see: https://github.com/strapi/strapi/actions/runs/19104795038/job/54586327649?pr=24648

How to test it?

The API tests should pass on this PR

Test it locally by running the docker test file docker-compose -f docker-compose.test.yml up

Now you can test all database dialects with yarn test:api --db=<sqlite | postgres | mysql>

Related issue(s)/PR(s)

I've tested (in the CI) this will resolves the transaction error causing tests to fail on:

#24648

@vercel
Copy link

vercel bot commented Nov 6, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
contributor-docs Ready Ready Preview Comment Nov 7, 2025 8:23am

@markkaylor markkaylor added pr: chore This PR contains chore tasks (cleanups, configs, tooling...) source: tooling Source is GitHub tooling/tests/ect flag: don't merge This PR should not be merged at the moment labels Nov 6, 2025
@markkaylor markkaylor self-assigned this Nov 6, 2025
@markkaylor markkaylor changed the title [wip] chore: replace testInTransaction with table truncate chore: replace testInTransaction with table truncate Nov 6, 2025
@markkaylor markkaylor changed the title chore: replace testInTransaction with table truncate chore(tests): replace testInTransaction with table truncate Nov 6, 2025
@markkaylor markkaylor marked this pull request as ready for review November 6, 2025 13:11
@innerdvations
Copy link
Contributor

Have you done tests on speed? I think this was originally added during the creation of the document service when we discovered the tests ran about 90% faster with this method.

As long as this is not a major increase in runtime I'm all for it though

@markkaylor
Copy link
Contributor Author

markkaylor commented Nov 6, 2025

Have you done tests on speed? I think this was originally added during the creation of the document service when we discovered the tests ran about 90% faster with this method.

As long as this is not a major increase in runtime I'm all for it though

I just did some and indeed it appears quite a bit slower, wdyt of keeping both? Favor the transaction util but use table truncate reset where needed?

As an example adonis exposes utils like that for similar reasons (ie nested transaction) https://docs.adonisjs.com/guides/testing/database

@markkaylor markkaylor force-pushed the chore/api-test-db-reset branch from 7ca70ae to ba53c3f Compare November 6, 2025 15:00
@markkaylor markkaylor changed the title chore(tests): replace testInTransaction with table truncate chore(tests): add setupDatabaseReset utility Nov 6, 2025
@markkaylor markkaylor added this to the 5.30.2 milestone Nov 7, 2025
@markkaylor markkaylor removed the flag: don't merge This PR should not be merged at the moment label Nov 7, 2025
@markkaylor markkaylor marked this pull request as ready for review November 7, 2025 08:17
@innerdvations
Copy link
Contributor

Have you done tests on speed? I think this was originally added during the creation of the document service when we discovered the tests ran about 90% faster with this method.
As long as this is not a major increase in runtime I'm all for it though

I just did some and indeed it appears quite a bit slower, wdyt of keeping both? Favor the transaction util but use a true database reset where needed?

As an example Adonis does something like that docs.adonisjs.com/guides/testing/database

I'm fine with having this as an option too, it's better than our current method of requiring every test to clean up after itself and then causing unrelated tests down the line to fail when they don't. The ideal solution would be a db reset that is fast enough to run before every test suite, so maybe this would be a first step towards that.

Copy link
Contributor

@mathildeleg mathildeleg left a comment

Choose a reason for hiding this comment

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

LGTM (but I don't think I know enough on this subject to give my full approval 🙈)

@markkaylor
Copy link
Contributor Author

@innerdvations If you are ok with the changes could you give me an approval? As you mentioned we can always improve it or even remove it and find a better solution if it isn't suiting our needs.

Copy link
Contributor

@araksyagevorgyan araksyagevorgyan left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@markkaylor markkaylor merged commit 366cfb5 into develop Nov 7, 2025
78 of 79 checks passed
@markkaylor markkaylor deleted the chore/api-test-db-reset branch November 7, 2025 10:19
chaitya-titan pushed a commit to chaitya-titan/strapi that referenced this pull request Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: chore This PR contains chore tasks (cleanups, configs, tooling...) source: tooling Source is GitHub tooling/tests/ect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants