Coder Social home page Coder Social logo

cypress-hyperapp-unit-test's Introduction

cypress-hyperapp-unit-test

Unit test Hyperapp components using Cypress

NPM

Build status semantic-release js-standard-style Cypress.io dashboard renovate-app badge

TLDR

  • What is this? This package allows you to use Cypress test runner to unit test your Hyperapp components with zero effort. The component runs in the real browser with full power of Cypress E2E test runner: live GUI, powerful API, screen recording, historical DOM snapshots, CI support, cross-platform.

  • The line between unit testing a component that renders into a DOM, makes HTTP requests, uses browser API and an end-to-end test for a complete web application is becoming very blurry in my opinion. Hope this little bridge between Hyperapp and Cypress test runner proves it. See examples below - some of them are testing individual components, some full apps. But the unit and end-to-end tests look and run very much alike.

Install

Requires Node version 6 or above.

npm install --save-dev cypress-hyperapp-unit-test

also requires peer dependencies in your project

npm install cypress hyperapp

API

You can import this module from your own tests

import { mount } from 'cypress-hyperapp-unit-test'
// import or code state, action and view
beforeEach(() => {
  mount(state, actions, view)
})
// you get fresh mini-app running in each test

Use

In your Cypress spec files (the example below is from file cypress/integration/hello-world-spec.js) mount the application, just like you would "normally".

import { mount } from 'cypress-hyperapp-unit-test'
import { h } from 'hyperapp'
// view function we are testing
const view = (state, actions) => h('div', { class: 'greeting' }, 'Hello, World')
describe('Hello World', () => {
  beforeEach(() => {
    const state = {}
    const actions = {}
    // no state or actions for this simple example
    mount(state, actions, view)
  })
  it('shows greeting', () => {
    // use any Cypress command - we have
    // real Hyperapp application for testing
    cy.contains('.greeting', 'Hello, World')
  })
})

Start Cypress using $(npm bin)/cypress open and execute the spec. You have full end-to-end test run but with your component! Why waste time on unit testing inside synthetic DOM's blackbox if you could see the result, inspect the DOM, investigate how it works using time-travelling debugger?

Hello World shows greeting

Examples

Unit tests and E2E tests start looking very much alike. Compare TodoList unit test and TodoMVC end-to-end test.

Repo organization

  • src/index.js the main file implementing mount
  • components different Hyper components for testing
  • actions pure actions functions used from components and tests
  • apps one or more complete bundled applications (build them using npm run build)
  • cypress/integration example spec files showing various test situations

See video of tests running on CI on the project's Cypress Dashboard

API Extras

  • Mounted component's actions object is attached to the global Cypress.main variable. The name main was picked because that's what Hyperapp uses in its docs const main = app(state, ...)
  • The mount function adds an action _getState to the actions object, if there is not one already present. This allows you to get the current state of the component for inspection.
Cypress.main.setName('Joe')
Cypress.main
  ._getState()
  .its('name')
  .should('equal', 'Joe')
Cypress.main.setAge(37)
Cypress.main._getState().should('deep.equal', {
  name: 'Joe',
  age: 37
})

Note: the Cypress.main wraps returned Hyperapp actions with cy.then to queue the calls through the Cypress command queue. Thus the above code looks synchronous, but in reality there could be DOM updates, network calls, etc, and it still works.

Package scripts

  • npm run build bundles complete applications if you want to run tests against full applications
  • npm run cy:open starts Cypress GUI, which is great for TDD mode
  • npm run cy:run runs Cypress headlessly, testing all specs. Same command runs on CI with additional --record argument to record the run and send to the Cypress Dashboard

Similar adaptors

Small print

Author: Gleb Bahmutov <[email protected]> © 2017

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) 2017 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-hyperapp-unit-test's People

Contributors

bahmutov avatar greenkeeper[bot] avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mateatslc

cypress-hyperapp-unit-test's Issues

Publish new version

Currently it somehow thinks 0.0.0-development is the latest version

$ vers cypress-hyperapp-unit-test
cypress-hyperapp-unit-test from git+https://github.com/bahmutov/cypress-hyperapp-unit-test.git
----------------------------------------------------------------------------------------------
version            age       dist-tag  release                         
-----------------  --------  --------  --------------------------------
1.0.0              3 months            # 1.0.0 (2017-12-29)            
1.1.0              3 months            # 1.1.0 (2017-12-29)            
1.1.1              3 months            # 1.1.1 (2017-12-29)            
1.1.2              3 months            # 1.1.2 (2017-12-29)            
1.2.0              3 months            # 1.2.0 (2017-12-29)            
1.2.1              3 months            # 1.2.1 (2017-12-30)            
1.2.2              3 months            # 1.2.2 (2017-12-30)            
1.3.0              2 months            # 1.3.0 (2018-01-19)            
0.0.0-development  a month   latest    # 0.0.0-development (2018-02-16)

Probably just switch to Renovate app

switch build to Parcel

getting an error with microbundle now

$ npm run build:todo-app

> [email protected] build:todo-app /Users/gleb/git/cypress-hyperapp-unit-test
> microbundle -i apps/todo-app.js -o apps/todo-app-bundle.js --format umd --compress false

'http' is imported by node_modules/axios/lib/adapters/http.js, but could not be resolved – treating it as an external dependency
'https' is imported by node_modules/axios/lib/adapters/http.js, but could not be resolved – treating it as an external dependency
'http' is imported by node_modules/follow-redirects/index.js, but could not be resolved – treating it as an external dependency
'https' is imported by node_modules/follow-redirects/index.js, but could not be resolved – treating it as an external dependency
'assert' is imported by node_modules/follow-redirects/index.js, but could not be resolved – treating it as an external dependency
'stream' is imported by node_modules/follow-redirects/index.js, but could not be resolved – treating it as an external dependency
'tty' is imported by node_modules/debug/src/node.js, but could not be resolved – treating it as an external dependency
'util' is imported by node_modules/debug/src/node.js, but could not be resolved – treating it as an external dependency
'os' is imported by node_modules/supports-color/index.js, but could not be resolved – treating it as an external dependency
'os' is imported by commonjs-external:os, but could not be resolved – treating it as an external dependency
'tty' is imported by commonjs-external:tty, but could not be resolved – treating it as an external dependency
'util' is imported by commonjs-external:util, but could not be resolved – treating it as an external dependency
'http' is imported by commonjs-external:http, but could not be resolved – treating it as an external dependency
'https' is imported by commonjs-external:https, but could not be resolved – treating it as an external dependency
'assert' is imported by commonjs-external:assert, but could not be resolved – treating it as an external dependency
'stream' is imported by commonjs-external:stream, but could not be resolved – treating it as an external dependency
'zlib' is imported by node_modules/axios/lib/adapters/http.js, but could not be resolved – treating it as an external dependency
SyntaxError: Unexpected token, expected ; (2:9)

Cannot publish to NPM

seems I am getting this error: npm/npm#19813

https://travis-ci.org/bahmutov/cypress-hyperapp-unit-test/builds/355383802

  stdout: '',
  stderr: 'npm ERR! publish Failed PUT 400\nnpm ERR! code E400\nnpm ERR! This package contains unacceptable content. Contact [email protected]. : cypress-hyperapp-unit-test\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     /home/travis/.npm/_logs/2018-03-19T14_25_32_266Z-debug.log\n',
  failed: true,
  signal: null,
  cmd: 'npm publish ./. --registry http://registry.npmjs.org/',
  timedOut: [secure],
  killed: [secure],
  pluginName: '@semantic-release/npm' }
{ Error: Command failed: npm publish ./. --registry http://registry.npmjs.org/
npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! This package contains unacceptable content. Contact [email protected]. : cypress-hyperapp-unit-test

Dependency deprecation warning: nsp (npm)

On registry https://registry.npmjs.org, the "latest" version (v3.2.1) of dependency nsp has the following deprecation notice:

The Node Security Platform service is shutting down 9/30 - https://blog.npmjs.org/post/175511531085/the-node-security-platform-service-is-shutting

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Affected package file(s): package.json

If you don't care about this, you can close this issue and not be warned about nsp's deprecation again. If you would like to completely disable all future deprecation warnings then add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

Move hyperapp from dev to peer dependency

Currently, the version of Hyperapp to load during tests is hardcoded without version

<script defer src="https://unpkg.com/hyperapp"></script>

Instead should load whatever is specified in the test project (how to load package.json from the test package?)

Should actions triggered through `Cypress.main` automatically queue up?

in this test

it('changes state by invoking action', () => {
    // the component's actions are referenced in Cypress.main
    Cypress.main.toggle()
    cy.get('.toggle').should('be.checked')
    // the action happens synchronously
    // but Cypress commands are queued
    // thus toggle again only after previous cy.get... has passed
    cy.then(() => {
      Cypress.main.toggle()
    })
    cy.get('.toggle').should('not.be.checked')
  })

Cypress.main.toggle() is called synchronously, but previous cypress command has not finished yet - it is queued up. Thus the second toggle has to be placed into the cy.then to make sure previous check button has finished. Maybe we can wrap actions attached to Cypress.main with cy.then inside mount function?

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 axios to v0.28.1
  • chore(deps): update dependency @types/node to v20
  • chore(deps): update dependency axios to v1
  • chore(deps): update dependency cypress to v13
  • chore(deps): update dependency mocha to v10
  • chore(deps): update dependency standard to v17
  • chore(deps): update dependency typescript to v5
  • 🔐 Create all rate-limited PRs at once 🔐

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

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

Detected dependencies

npm
package.json
  • @types/node 9.6.61
  • axios 0.21.1
  • ban-sensitive-files 1.9.15
  • common-tags 1.8.0
  • cypress 3.8.3
  • dependency-check 3.4.1
  • deps-ok 1.4.1
  • git-issues 1.3.1
  • hyperapp 1.2.10
  • license-checker 20.2.0
  • mocha 5.2.0
  • nsp 3.2.1
  • parcel-bundler 1.12.4
  • picostyle 2.2.0
  • pre-git 3.17.1
  • prettier-standard 8.0.1
  • simple-commit-message 4.1.2
  • standard 12.0.1
  • typescript 3.9.9
  • semantic-release 15.14.0
  • node >=6
travis
.travis.yml
  • node 8

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

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset name not found within published preset config (monorepo:babel6). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

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

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 resolve 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 the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is 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.


The push permission to the Git repository is required.

semantic-release cannot push the version tag to the branch master on remote Git repository.

Please refer to the authentication configuration documentation to configure the Git credentials on your CI environment.


Good luck with your project ✨

Your semantic-release bot 📦🚀

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.