Code Monkey home page Code Monkey logo

cypress-log-to-term's Introduction

cypress-log-to-term cypress version ci

Overwrite the cy.log command to print to both the Command Log and to the terminal

Logs to both places

Explanation

Read the blog post Two Simple Tricks To Make Your Cypress Tests Better. This plugin is covered in my Cypress Plugins course πŸŽ“:

Install

Install this plugin from the NPM registry using NPM or Yarn or your favorite Node package manager.

# Install using NPM
$ npm i -D cypress-log-to-term
# Install using Yarn
$ yarn add -D cypress-log-to-term

Include the plugin from your support or spec file

// cypress/e2e/spec.cy.js
// https://github.com/bahmutov/cypress-log-to-term
import 'cypress-log-to-term/commands'

Include the plugin from your cypress.config.js file's setupNodeEvents callback

// cypress.config.js
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    // baseUrl, etc
    supportFile: false,
    fixturesFolder: false,
    setupNodeEvents(on, config) {
      // register the "cypress-log-to-term" plugin
      // https://github.com/bahmutov/cypress-log-to-term
      // IMPORTANT: pass the "on" callback argument
      require('cypress-log-to-term')(on)
    },
  },
})

See cypress/e2e/spec.cy.js

Markdown

Markdown bold characters ** are automatically removed before passing them to the task to be printed.

Yields the current subject

The cy.log yields the current subject to the next command, because it should.

cy.wrap(42).log().should('equal', 42)

Format string

This plugins adds format string and format callback function feature to the cy.log command. For example, the "standard" cy.log command could only print a string argument

cy.wrap({ name: 'Joe' }).log('the name')
// prints "the name"

This module allows you to print the current subject

cy.wrap({ name: 'Joe' }).log()
// prints '{"name":"Joe"}'

Complex objects are supported

cy.wrap([1, 'hello', { name: 'Joe' }]).log()
// prints '[1,"hello",{"name":"Joe"}]'

You can add a string argument and insert the formatted subject into it

cy.wrap({ name: 'Joe' }).log('person is %o')
// prints 'person is {"name":"Joe"}'

This is equivalent to {0} notation

cy.wrap({ name: 'Joe' }).log('person is {0}')
// prints 'person is {"name":"Joe"}'

You can even access the properties of the subject

cy.wrap({ name: 'Joe' }).log('my name is {0.name}')
// prints 'my name is Joe'

Deep properties are allowed

cy.wrap([{ name: 'Joe' }, { name: { first: 'Anna' } }])
  // from the subject (accessed via {0})
  // grab the property "1" (which is the second item in our array)
  // then grab the path "name.first"
  .log('her name is {0.1.name.first}')
// prints 'her name is Anna'

Format callback

You can pass a callback function to the overwritten cy.log command. This way you can return a formatted string given the subject value.

const person = { name: 'Joe' }
cy.wrap(person).log((p) => `name is ${p.name}`)
// prints 'name is Joe'
cy.wrap([1, 2, 3]).log((list) => list[1])
// prints "2"

If you return non-string result, cy.log will try its best to serialize it

cy.wrap({ name: 'Me' }).log((x) => x) // {"name":"Me"}

Circular objects

If the subject to be serialized has circular references, they are safely converted to string

const obj = {
  name: 'object',
}
obj.next = obj
// try to log a circular object
cy.wrap(obj, { log: false }).log()
// {"name":"object","next":"[Circular]"}

See circular.cy.js

jQuery / DOM elements

Are serialized with main properties (id, class, attributes) and the number of matched elements

<h1 id="title">Hello</h1>
cy.get('h1').log()
// $ of 1 <div#obj.my-object/>

If there are a lot of yielded elements, only the first and the last one are logged

<ol id="people">
  <li class="first">one</li>
  <li>two</li>
  <li>three</li>
  <li>four</li>
  <li class="last">five</li>
</ol>
cy.get('#people li').log()
// $ of 5 [<li.first/>...<li.last/>]

Note: when serializing, the log respects the chai.config.truncateThreshold setting. To log more info, increase it in your spec or support file:

<p class="some-class" data-cy="para" data-test="my-p">Test page</p>
cy.get('#people li').log()
// default
// $ of 1 <p.some-class data-cy=para data-t...
// with chai.config.truncateThreshold = 200
// $ of 1 <p.some-class data-cy=para data-test=my-p/>

Tip: use the format string to better explain what the elements are

cy.get('#people li').log('list of people %o')
// list of people $ of 5 [<li.first/>...<li.last/>]

See dom.cy.js

Types

This package includes TypeScript command definitions for its custom commands in the file src/index.d.ts. To use it from your JavaScript specs:

/// <reference types="cypress-log-to-term" />

If you are using TypeScript, include this module in your types list

{
  "compilerOptions": {
    "types": ["cypress", "cypress-log-to-term"]
  }
}

Small print

Author: Gleb Bahmutov <[email protected]> Β© 2022

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2022 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cypress-log-to-term's People

Contributors

bahmutov avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cypress-log-to-term's Issues

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency cypress to v12.17.4
  • chore(deps): update actions/checkout action to v4
  • chore(deps): update dependency cypress to v13
  • chore(deps): update dependency ubuntu to v22
  • chore(deps): update stefanzweifel/git-auto-commit-action action to v5
  • πŸ” Create all rate-limited PRs at once πŸ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/badges.yml
  • actions/checkout v3
  • stefanzweifel/git-auto-commit-action v4
  • ubuntu 20.04
.github/workflows/ci.yml
  • actions/checkout v3
  • cypress-io/github-action v5
  • cycjimmy/semantic-release-action v3
  • ubuntu 20.04
npm
package.json
  • safe-stable-stringify ^2.4.2
  • string-format ^2.0.0
  • cypress ^12.5.1
  • prettier ^2.8.3
  • semantic-release ^20.0.0
  • stop-only ^3.3.1
  • typescript ^4.9.5

  • Check this box to trigger a request for Renovate to run again on this repository

Set the message property

When overwriting the log command, use Cypress.log static method to put the full text into the terminal devtools console

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.