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

add option to create tests with Playwright #4056

Merged
merged 7 commits into from Feb 23, 2022
Merged

add option to create tests with Playwright #4056

merged 7 commits into from Feb 23, 2022

Conversation

Rich-Harris
Copy link
Member

Thought I'd tackle one of the earliest issues — #19. This adds a 'Add Playwright for testing?' prompt when you create a new project:

image

After that, npm test runs test/test.js with Playwright.

Aside: we had to create our own test fixtures for Kit's own tests, notably clicknav, which allows us to wait for client-side routing to happen following a click. Am reluctantly coming round to the view that my position in #2917 (comment) and #3241 was misguided; in order to use things like page.click and waitForNavigation idiomatically, we need to wait until navigation has occurred before doing pushState. This isn't just the tail wagging the dog — premature pushState also causes a subtle bug where relative URLs are briefly incorrect.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Feb 22, 2022

🦋 Changeset detected

Latest commit: 9e3c8ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

just fyi, lint is failing

this seems like a good idea to me. the big questions I had about doing this were:

  • the navigation stuff (which you already mentioned. though I wonder if that should be done before this)
  • whether we'd also want to setup vitest or something similar for unit testing
  • whether it makes any sense to test components outside of a web browser in some scenarios (I legitimately don't know the answer to this as I've done limited frontend testing)

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
@Rich-Harris
Copy link
Member Author

the navigation stuff (which you already mentioned. though I wonder if that should be done before this)

probably. we can open a separate PR and leave this open till then

whether we'd also want to setup vitest or something similar for unit testing

will confess i have no idea what this looks like in practice. have people set this up in kit apps? any pitfalls? either way i tend to think unit tests are somewhat less important, so we can tackle that later

whether it makes any sense to test components outside of a web browser in some scenarios

FWIW it's fairly straightforward to test SSR and endpoints with Playwright. Though I wonder if we should have JS and JS-less projects in the playwright.config.js by default, the same as we do in Kit's own tests

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
@@ -0,0 +1,6 @@
import { expect, test } from '@playwright/test';

test('about page has expected h1', async ({ page }) => {
Copy link
Member

Choose a reason for hiding this comment

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

Does playwrigt support writing your code in TypeScript? If yes, should we add a variant for TS users?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, playwright does support typescript ootb, down to the playwright config too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't seem to get it to work. I keep seeing errors like this:

> my-new-app@0.0.1 test /Users/rich/Development/SVELTE/KIT/my-new-app
> playwright test

Error: playwright.config.ts: Cannot import a typescript file from an esmodule.

Am I doing something obviously wrong, or should we add TypeScript stuff in a follow-up PR?

@Rich-Harris
Copy link
Member Author

Let's do the TS stuff in a follow-up

@Rich-Harris Rich-Harris mentioned this pull request Feb 23, 2022
@randyridge
Copy link

randyridge commented Feb 24, 2022

Hi there, I just tried this out, with the build and preview command in playwright.config.js I found that my build runs longer than the default playwright test timeout, I ended up having to move the build portion to my package.json test script "pnpm build && playwright test".

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

Successfully merging this pull request may close these issues.

None yet

5 participants