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

chore: add browser example test #846

Merged
merged 2 commits into from
Jan 19, 2021
Merged

Conversation

vasco-santos
Copy link
Member

This PR adds a test for the libp2p in the browser example. It also includes the browser setup for testing examples based on ipfs/js-ipfs#2528

@vasco-santos vasco-santos force-pushed the chore/add-browser-example-test branch 4 times, most recently from 09a5f24 to 46c197d Compare December 28, 2020 13:25
@hugomrdias
Copy link
Member

'use strict'

const execa = require('execa')
const assert = require('assert')
const { chromium } = require('playwright');

async function run () {
  let url = ''
  const proc = execa('parcel', ['./index.html'], {
    preferLocal: true,
    localDir: __dirname,
    cwd: __dirname,
    all: true
  })
  
  proc.all.on('data', async (chunk) => {
    /**@type {string} */
    const out = chunk.toString()

    if(out.includes('Server running at')){
      url = out.replace('Server running at ', '')
    }

    if(out.includes('✨  Built in ')){
      try {
        const browser = await chromium.launch();
        const page = await browser.newPage();
        await page.goto(url);
        await page.waitForFunction(selector => document.querySelector(selector).innerText === 'libp2p started!', '#status')
        await page.waitForFunction(
          selector => { 
            const text = document.querySelector(selector).innerText 
            return text.includes('libp2p id is') &&
            text.includes('Found peer') &&
            text.includes('Connected to')
          }, 
          '#output',
          { timeout: 5000 }
        )
        await browser.close();
        
      } catch (err) {
        console.error(err)
        process.exit(1)
      } finally {
        proc.cancel()
      }
    }
  })

}

module.exports = run

With the above snippet in the test.js there no need for changes in the others files. What do you think ?

examples/libp2p-in-the-browser/package.json Outdated Show resolved Hide resolved
examples/nightwatch.conf.js Outdated Show resolved Hide resolved
examples/package.json Outdated Show resolved Hide resolved
examples/package.json Outdated Show resolved Hide resolved
examples/test.js Outdated Show resolved Hide resolved
@@ -8,6 +8,7 @@
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "parcel build index.html",
Copy link
Member

Choose a reason for hiding this comment

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

probably not needed with the new setup

Copy link
Member Author

Choose a reason for hiding this comment

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

Just tried this out, but we need it otherwise the tests do not run.

@vasco-santos
Copy link
Member Author

Thanks for the suggestions @hugomrdias . This should now be good. Just waiting on CI

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.

2 participants