percy / percy-agent

[Deprecated in favor of `@percy/cli`] An agent process for integrating with Percy.
https://percy.io
MIT License
22 stars 23 forks source link

Is there an official way to run PercyAgent via NPM module API? #488

Closed bahmutov closed 3 years ago

bahmutov commented 4 years ago

Hi,

We have made https://github.com/cypress-io/netlify-plugin-cypress for Netlify testing, and already would like to run our static site and get visual Percy screenshots too. BUT we cannot do percy exec -- because we are calling Cypress via its module API http://on.cypress.io/module-api

So I looked at Percy Agent source and hacked this little example https://github.com/bahmutov/run-percy-via-module-api which works https://percy.io/cypress-io/jsnation-example/builds/4732333

You can see the code in https://github.com/bahmutov/run-percy-via-module-api/blob/master/index.js but it looks like this right now

const cypress = require('cypress')
const percyAgent = require('@percy/agent/dist/services/agent-service')
const p = new percyAgent.AgentService()
// looking at default configuration
// https://github.com/percy/percy-agent/blob/6b36aabb02e951661778e1ef5e7f9bc0c342d6cc/src/configuration/configuration.ts#L15
const configuration = {
  agent: {
    port: 5338
  },
  snapshot: {
    'percy-css': ''
  }
}
p.start(configuration)
  .then(() => {
    console.log('running Cyrpess')
    return cypress.run({})
  })
  .then(() => {
    console.log('stopping Percy agent')
    return p.stop()
  })
  .catch((err) => {
    console.error(err)
    process.exit(1)
  })

But of course I would love to see an official documented way of using Percy from other Node modules

Robdel12 commented 4 years ago

Hey @bahmutov, this is an excellent question! The answer for now is no, there isn't a way to consume our SDKs via JS. You're free to do what you've posted above, but I fully expect all of the internals of our SDKs to change before 1.x. The only API we're currently committed to keeping the same is the CLI interface (and the Percy agent server).

1.x will absolutely make what you're wanting to do much easier. Right now @percy/agent is a bit of a confused package. It's mostly a node CLI package but its default export is a browser DOM lib, which is why you have to do a deep require (require('@percy/agent/dist/services/agent-service')). Soon this will be much better and everything will make a lot more sense (with docs too! 😃 )

bahmutov commented 4 years ago

Great, this is good plan, appreciate the response.

Robdel12 commented 3 years ago

Hey @bahmutov! I know it's been a while, but we've got a new package for you to accomplish this: https://github.com/percy/cli/tree/master/packages/core

Percy agent will soon be deprecated and we'll be moving to @percy/cli

bahmutov commented 3 years ago

Nice!

Sent from my iPhone

On Jan 13, 2021, at 11:27, Robert DeLuca notifications@github.com wrote:

 Hey @bahmutov! I know it's been a while, but we've got a new package for you to accomplish this: https://github.com/percy/cli/tree/master/packages/core

Percy agent will soon be deprecated and we'll be moving to @percy/cli

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.