Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs]: How to write isolated playwright tests against a real database #33699

Open
nodefir opened this issue Nov 20, 2024 · 2 comments
Open

[Docs]: How to write isolated playwright tests against a real database #33699

nodefir opened this issue Nov 20, 2024 · 2 comments

Comments

@nodefir
Copy link

nodefir commented Nov 20, 2024

Page(s)

https://playwright.dev/docs/next/browser-contexts

Description

When writing playwright tests against a local database (i.e. a postgres server with a database named your_app_test), there does not seem to be anywhere online that walks one through writing transactional tests that will not leak data during parallelization. All I have been able to come across is information on fixture data, but what about data created by the test itself?

I have gone down the rabbit hole of trying to recreate the seamless system testing (end-to-end testing) provided by Rails, which is able to spin up a test server and run tests in parallel without having to worry about data collisions.

Approaches I have tried thus far:

  • data truncation in before/after hooks
  • data truncation around await use() in fixtures
  • beginning and rolling back transactions around test (using beforeEach, afterEach, and then a fixture)
  • creating isolated databases using something like execSync(psql -c "CREATE DATABASE your_app_test_${env.process.TEST_WORKER_INDEX}; (doesn't work because env.process.TEST_WORKER_INDEX isn't available to the web server).
  • data truncation using a single worker (too slow to realistically be an option - but works)

How can I achieve a similar workflow to Rails system tests, where I can create data (directly in the test or by clicking around in my app) in a test database without worrying about other tests creating similar data? Is this out of scope for playwright?

For what it's worth, I am using prisma and Next.js.

@dgozman
Copy link
Contributor

dgozman commented Nov 22, 2024

@nodefir Thank you for the issue! This is what we'd recommend for you:

  • loosely follow this guide;
  • instead of running your Next.js server as a webServer, start it in the worker fixture from the guide above;
  • make sure your Next.js server creates a unique database in this setup;
  • you might want to override baseURL fixture to use the freshly launched server's address;
  • teardown your Next.js server in the fixture cleanup.

This will ensure that tests running in one worker will talk to its own server instance that will connect to its own database. Let me know what you think.

@nodefir
Copy link
Author

nodefir commented Nov 22, 2024

Thanks @dgozman! I'll take a look this evening. If I can get this working I'll write up a blog post and also post the solution here as well.

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

No branches or pull requests

2 participants