Skip to content

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 ?

@vasco-santos vasco-santos force-pushed the chore/add-browser-example-test branch from 46c197d to 2b6fece Compare January 18, 2021 16:56
],
"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 vasco-santos force-pushed the chore/add-browser-example-test branch from 2b6fece to bdf7f4d Compare January 18, 2021 17:05
@vasco-santos vasco-santos force-pushed the chore/add-browser-example-test branch from bdf7f4d to 03173f3 Compare January 18, 2021 17:05
@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