@percy/sdk-utils
Common JavaScript SDK utils
Usage
logger([debug])
This function is a direct export of @percy/logger
.
percy
This object contains information about the local Percy environment and is updated when
isPercyEnabled
is called for the first time.
import { percy } from '@percy/sdk-utils'
percy.address === 'http://localhost:5338'
percy.enabled === true|false
percy.version.major === 1
percy.version.minor === 2
percy.version.patch === 3
percy.version.toString() === '1.2.3'
percy.config === {}
percy.domScript === fs.readFile(require.resolve('@percy/dom'))
isPercyEnabled()
Returns true
or false
if the Percy CLI API server is running. Calls the server's /healthcheck
endpoint and populates information for the percy
property. The result of this function
is cached and subsequent calls will return the first cached result. If the healthcheck fails, will
log a message unless the CLI loglevel is quiet
or silent
.
import { isPercyEnabled } from '@percy/sdk-utils'
await isPercyEnabled() === false
await isPercyEnabled() === true
fetchPercyDOM()
Fetches and returns the @percy/dom
serialization script hosted by the local Percy API server. The
resulting string can be evaulated within a browser context to add the PercyDOM.serialize
function
to the global scope. Subsequent calls return the first cached result.
import { fetchPercyDOM } from '@percy/sdk-utils'
let script = await fetchPercyDOM()
driver.executeScript(script)
browser.execute(script)
page.evaluate(script)
browser.executeScript(script)
postSnapshot(options)
Posts snapshot options to the local Percy API server.
import { postSnapshot } from '@percy/sdk-utils'
await postSnapshot({
name: 'Snapshot Name',
url: 'http://localhost:8000/',
domSnapshot: 'result from PercyDOM.serialize()'
environmentInfo: ['<lib>/<version>', '<lang>/<version>'],
clientInfo: '<sdk>/<version>',
widths: [475, 1280],
minHeight: 1024,
enableJavaScript: false,
requestHeaders: {}
})
request(url[, options])
Sends a request to the local Percy API server. Used internally by the other SDK utils.
import { request } from '@percy/sdk-utils'
await request('/percy/idle')
await request('/percy/stop')
request.fetch(url, options)
The underlying implementation of the request()
util. For Node environments, http.request
is
used; for browser environments, window.fetch
is used. Can be overridden by the SDK's framework to
work around CORS/CSP issues.
The returned object must contain the following normalized properties from the request response:
status
, statusText
, headers
, body
import { request } from '@percy/sdk-utils'
request.fetch = async function fetch(url, options) {
options = { url, retryOnNetworkFailure: false, ...options }
return Cypress.backend('http:request', options)
}