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

How do I write unit tests or e2e tests? #68

Open
ralyodio opened this issue Apr 19, 2022 · 10 comments
Open

How do I write unit tests or e2e tests? #68

ralyodio opened this issue Apr 19, 2022 · 10 comments

Comments

@ralyodio
Copy link

The examples don’t show any kind of test code. Like “it”

@patrickhulce
Copy link
Collaborator

This library doesn't really interact with any specific test runner, so how you decide to chunk your tests is up to you.

Here's an example with describe/it wrapped around our example.

const puppeteer = require('puppeteer')
const {getDocument, queries, waitFor} = require('pptr-testing-library')

const {getByTestId, getByLabelText} = queries

describe('e2e tests', () => {
  let browser
  let page
  let $document

  beforeAll(async () => {
    browser = await puppeteer.launch()
    page = await browser.newPage()
  })

  it('loads the page', async () => {
    await page.goto('https://example.com')
    // Grab ElementHandle for document
    $document = await getDocument(page)
  })

  it('fills out the form', async () => {
    // Your favorite query methods are available
    const $form = await getByTestId($document, 'my-form')
    // returned elements are ElementHandles too!
    const $email = await getByLabelText($form, 'Email')
    // interact with puppeteer like usual
    await $email.type('pptr@example.com')
    // waiting works too!
    await waitFor(() => getByText($document, 'Loading...')) 
    const $submit = await getByLabelText(
  })

  it('submitted the data successfully', async () => {
    const $header = await getByTestId($document, 'header')
    expect(await $header.evaluate(el => el.textContent)).toEqual('Success!')
  })
})

@patrickhulce
Copy link
Collaborator

PR welcome for anyone that wants to contribute something more substantive :)

@ralyodio
Copy link
Author

How do I add the test methods?

@ralyodio
Copy link
Author

Is there a page that explains how to set up?

@patrickhulce
Copy link
Collaborator

Are you asking how to setup basic jest/mocha tests at all?

@ralyodio
Copy link
Author

ralyodio commented Apr 21, 2022

Yes I’m using jest for unit tests. Wondering how to get it to work with puppeteer too

@ralyodio
Copy link
Author

ralyodio commented Apr 21, 2022

I've got this now. but it won't run. It doesn't find "it()"

const puppeteer = require('puppeteer');
const { getDocument, queries, waitFor } = require('pptr-testing-library');

const { getByTestId, getByLabelText } = queries;

describe('Signup', () => {
	let browser;
	let page;
	let $document;

	beforeAll(async () => {
		browser = await puppeteer.launch();
		page = await browser.newPage();
	});

	it('loads the page', async () => {
		await page.goto('http://localhost:3000');
		// Grab ElementHandle for document
		$document = await getDocument(page);

		expect($docoument).toBeDefined();
	});
});

My package.json looks like this:

"e2e:pptr": "npm run dev & jest ./tests",

I get this error:

  Signup
    ✕ loads the page (5001 ms)

  ● Signup › loads the page

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      14 |      });
      15 |
    > 16 |      it('loads the page', async () => {
         |      ^
      17 |              await page.goto('http://localhost:3000');
      18 |              // Grab ElementHandle for document
      19 |              $document = await getDocument(page);

      at tests/homepage.test.js:16:2
      at Object.<anonymous> (tests/homepage.test.js:6:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        6.421 s

@ralyodio
Copy link
Author

Ok so actually it works. lol. but puppetter is not opening an actual browser. Is there a way to enable that?

@ralyodio
Copy link
Author

How can I do this:

expect(await $document.evaluate(el => el.title)).toEqual('Catalyze - Log In');

It throws an error.

@patrickhulce
Copy link
Collaborator

It throws an error.

What error?

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