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

[Testing] Configuration for e2e testing in jest #731

Closed
wants to merge 7 commits into from

Conversation

RobertBroersma
Copy link
Contributor

This PR enables E2E-like testing on the web side like so: https://github.com/RobertBroersma/redwood-testing/blob/master/web/src/pages/BlogPostPage/BlogPostPage.test.js without having to spin up any HTTP servers!

It still allows the user to override calls with MSW.

There are some weird things in this PR that probably have a better way to do it, which is why I opened it as a draft!

For it to work and not destroy your dev database all the time this PR requires #691 to be reach consensus and be merged first.

On a side note I would like to say I've never written my tests in this way before, but I'm quite excited about the possibility! So I'm curious to hear what others think about this method. Might it be redundant to do this if we can just spin up the server and run Cypress?

@@ -2,11 +2,24 @@ require('@testing-library/jest-dom')
require('whatwg-fetch')

const { server } = require('@redwoodjs/testing')
const camelCase = require('lodash/camelCase')
const { PrismaClient } = require('@prisma/client')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to import the db.js from the API side of the app, but Jest won't transpile it because it's outside of the rooDir (I think).

Copy link
Contributor

Choose a reason for hiding this comment

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

I can take a look at this, which steps should I take to reproduce this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. Checkout https://github.com/RobertBroersma/redwood-testing
  2. Checkout this PR
  3. Replace in this file the direct PrismaClient import with the db.js import.
  4. Run yarn rw test web in the redwood-testing repo

Thanks!

const redwoodPaths = getPaths()

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { PrismaClient } = require('@prisma/client')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here. Jest won't let me import the db from the src of the api side.

@peterp
Copy link
Contributor

peterp commented Jun 24, 2020

So I'm curious to hear what others think about this method. Might it be redundant to do this if we can just spin up the server and run Cypress?

I can totally see where you're going with this, but I'm a bit worried that it's too much of an abstraction of the GraphQL Serverless Function, and it might not match a user's expectations... it's super close, but doesn't take into account the context or the current user, and I don't know if it's entirely worth it.

@RobertBroersma
Copy link
Contributor Author

So I'm curious to hear what others think about this method. Might it be redundant to do this if we can just spin up the server and run Cypress?

I can totally see where you're going with this, but I'm a bit worried that it's too much of an abstraction of the GraphQL Serverless Function, and it might not match a user's expectations... it's super close, but doesn't take into account the context or the current user, and I don't know if it's entirely worth it.

I've had some time to let it simmer in my head as well, and I think for now we can write it off as "Too soon" or "Too weird" or "Probably easier using Cypress"

I might get back to it later because I do like the idea, and I think the context and current user problems are solvable!

@peterp
Copy link
Contributor

peterp commented Jun 24, 2020

I like it too! It does feel too soon.. like my brain wants to wrap around the idea but it's struggling!

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

Successfully merging this pull request may close these issues.

2 participants