GithubHelp home page GithubHelp logo

testing-library / nightwatch-testing-library Goto Github PK

View Code? Open in Web Editor NEW
31.0 31.0 11.0 869 KB

๐Ÿฆ‡Simple and complete custom queries for Nightwatch that encourage good testing practices.

JavaScript 76.42% HTML 23.58%
testing

nightwatch-testing-library's Introduction

@testing-library/angular

Octopus with the Angular logo

Simple and complete Angular testing utilities that encourage good testing practices.


Read The Docs | Edit the docs



Build Status version downloads MIT License

All Contributors PRs Welcome Code of Conduct Discord

Watch on GitHub Star on GitHub Tweet

Open in GitHub Codespaces

Table of Contents

The problem

You want to write maintainable tests for your Angular components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your testbase to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.

This solution

The @testing-library/angular is a very lightweight solution for testing Angular components. It provides light utility functions on top of Angular and @testing-library/dom, in a way that encourages better testing practices. Its primary guiding principle is:

The more your tests resemble the way your software is used, the more confidence they can give you.

Example

counter.component.ts

@Component({
  selector: 'app-counter',
  template: `
    <button (click)="decrement()">-</button>
    <span>Current Count: {{ counter }}</span>
    <button (click)="increment()">+</button>
  `,
})
export class CounterComponent {
  @Input() counter = 0;

  increment() {
    this.counter += 1;
  }

  decrement() {
    this.counter -= 1;
  }
}

counter.component.spec.ts

import { render, screen, fireEvent } from '@testing-library/angular';
import { CounterComponent } from './counter.component';

describe('Counter', () => {
  test('should render counter', async () => {
    await render(CounterComponent, { componentProperties: { counter: 5 } });

    expect(screen.getByText('Current Count: 5'));
  });

  test('should increment the counter on click', async () => {
    await render(CounterComponent, { componentProperties: { counter: 5 } });

    const incrementButton = screen.getByRole('button', { name: '+' });
    fireEvent.click(incrementButton);

    expect(screen.getByText('Current Count: 6'));
  });
});

See more examples

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install @testing-library/angular --save-dev

You may also be interested in installing jest-dom so you can use the custom jest matchers.

Docs

Version compatibility

Angular Angular Testing Library
18.x 16.x, 15.x, 14.x, 13.x
17.x 16.x, 15.x, 14.x, 13.x
16.x 14.x, 13.x
>= 15.1 14.x, 13.x
< 15.1 12.x, 11.x
14.x 12.x, 11.x

Guiding Principles

The more your tests resemble the way your software is used, the more confidence they can give you.

We try to only expose methods and utilities that encourage you to write tests that closely resemble how your Angular components are used.

Utilities are included in this project based on the following guiding principles:

  1. If it relates to rendering components, it deals with DOM nodes rather than component instances, nor should it encourage dealing with component instances.
  2. It should be generally useful for testing individual Angular components or full Angular applications.
  3. Utility implementations and APIs should be simple and flexible.

At the end of the day, what we want is for this library to be pretty light-weight, simple, and understandable.

Contributors

Thanks goes to these people (emoji key):

Tim Deschryver
Tim Deschryver

๐Ÿ’ป ๐Ÿ“– ๐Ÿš‡ โš ๏ธ
Michaรซl De Boey
Michaรซl De Boey

๐Ÿ“–
Ignacio Le Fluk
Ignacio Le Fluk

๐Ÿ’ป โš ๏ธ
Tamรกs Szabรณ
Tamรกs Szabรณ

๐Ÿ’ป
Gregor Woiwode
Gregor Woiwode

๐Ÿ’ป
Toni Villena
Toni Villena

๐Ÿ› ๐Ÿ’ป ๐Ÿ“– โš ๏ธ
ShPelles
ShPelles

๐Ÿ“–
Miluoshi
Miluoshi

๐Ÿ’ป โš ๏ธ
Nick McCurdy
Nick McCurdy

๐Ÿ“–
Srinivasan Sekar
Srinivasan Sekar

๐Ÿ“–
Bitcollage
Bitcollage

๐Ÿ“–
Emil Sundin
Emil Sundin

๐Ÿ’ป
Ombrax
Ombrax

๐Ÿ’ป
Rafael Santana
Rafael Santana

๐Ÿ’ป โš ๏ธ ๐Ÿ›
Benjamin Blackwood
Benjamin Blackwood

๐Ÿ“– โš ๏ธ
Gustavo Porto
Gustavo Porto

๐Ÿ“–
Bo Vandersteene
Bo Vandersteene

๐Ÿ’ป
Janek
Janek

๐Ÿ’ป โš ๏ธ
Gleb Irovich
Gleb Irovich

๐Ÿ’ป โš ๏ธ
Arjen
Arjen

๐Ÿ’ป ๐Ÿšง
Suguru Inatomi
Suguru Inatomi

๐Ÿ’ป ๐Ÿค”
Amit Miran
Amit Miran

๐Ÿš‡
Jan-Willem Willebrands
Jan-Willem Willebrands

๐Ÿ’ป
Sandro
Sandro

๐Ÿ’ป ๐Ÿ›
Michael Westphal
Michael Westphal

๐Ÿ’ป โš ๏ธ
Lukas
Lukas

๐Ÿ’ป
Matan Borenkraout
Matan Borenkraout

๐Ÿšง
mleimer
mleimer

๐Ÿ“– โš ๏ธ
MeIr
MeIr

๐Ÿ› โš ๏ธ
John Dengis
John Dengis

๐Ÿ’ป โš ๏ธ
Rokas Brazdลพionis
Rokas Brazdลพionis

๐Ÿ’ป
Mateus Duraes
Mateus Duraes

๐Ÿ’ป
Josh Joseph
Josh Joseph

๐Ÿ’ป โš ๏ธ
Torsten Knauf
Torsten Knauf

๐Ÿšง
antischematic
antischematic

๐Ÿ› ๐Ÿค”
Florian Pabst
Florian Pabst

๐Ÿ’ป
Mark Goho
Mark Goho

๐Ÿšง ๐Ÿ“–
Jan-Willem Baart
Jan-Willem Baart

๐Ÿ’ป โš ๏ธ

This project follows the all-contributors specification. Contributions of any kind welcome!

Docs

Read The Docs | Edit the docs

FAQ

I am using Reactive Forms and the jest-dom matcher toHaveFormValues always returns an empty object or there are missing fields. Why?

Only form elements with a name attribute will have their values passed to toHaveFormsValues.

Issues

Looking to contribute? Look for the Good First Issue label.

๐Ÿ› Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior.

See Bugs

๐Ÿ’ก Feature Requests

Please file an issue to suggest new features. Vote on feature requests by adding a ๐Ÿ‘. This helps maintainers prioritize what to work on.

See Feature Requests

โ“ Questions

For questions related to using the library, please visit a support community instead of filing an issue on GitHub.

Getting started with GitHub Codespaces

To get started, create a codespace for this repository by clicking this ๐Ÿ‘‡

Open in GitHub Codespaces

A codespace will open in a web-based version of Visual Studio Code. The dev container is fully configured with software needed for this project.

Note: Dev containers is an open spec which is supported by GitHub Codespaces and other tools.

LICENSE

MIT

nightwatch-testing-library's People

Contributors

allcontributors[bot] avatar benmonro avatar dependabot-preview[bot] avatar imgbotapp avatar michaeldeboey avatar nickmccurdy 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  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nightwatch-testing-library's Issues

Call for maintainers

I created this when my company was using night watch but we have since moved on to testcafe. If you are interested in taking over this project let me know and I will make you admin

window.TestingLibraryDom is undefined

Environment:
node v12.10.0
Package.json:

"react": "^16.8.6",
"react-dom": "^16.8.6",
"@testing-library/jest-dom": "^4.2.3",
"@testing-library/nightwatch": "^1.1.0",
"geckodriver": "^1.19.1",
"nightwatch": "^1.2.4"

Error:

[E2e/Auth/Login Test] Test Suite ================================ Running: loginButtonIsVisible
 window.TestingLibraryDom is undefined
 Error: 
{"selector":"getByText","value":["Log In"],"locatorStrategy":"getByText","name":""
at processTicksAndRejections (internal/process/task_queues.js:93:5)  FAILED: 1 errors (5.117s)

Using chrome is fine but in firefox I got this error above.

Test code:

const { getQueriesFrom } = require('@testing-library/nightwatch');

module.exports = {
  beforeEach(browser, done) {
    browser.url('http://localhost:3000');
    done();
  },

  // Smoke test
  async loginButtonIsVisible(browser) {
    const { getByText } = getQueriesFrom(browser);
    browser.pause(5000);
    const input = await getByText('Log In');
    browser.waitForElementVisible(input, 5000);
    browser.assert.visible(input);
  },
};

Error: [object Object]

Hello, I'm trying to set up a project with nightwatch & nightwatch-testing-library but I can't get it to work and just returns object-object for any tests.

I've followed this stackover flow article but it still results in the same issue.

Even though there is a example of a test this is against a localhost page which we can't inspect it would be good if this example was against an actual site such as Ecosia in the nightwatch example.

Could I get some help and apologies if I've posted in the wrong place.

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 to 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.


Cannot push to the Git repository.

semantic-release cannot push the version tag to the branch master on the remote Git repository with URL https://[secure]@github.com/testing-library/nightwatch-testing-library.git.

This can be caused by:


Good luck with your project โœจ

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€

UserEvents and the nightwatch-testing-library

Is nightwatch-testing-library intended to work with the testing-library's userEvent system?

I have many unit tests written with the userEvent system and I'd like to continue using the same system with nightwatch to eliminate code duplication, along with the other benefits of user-event testing.

To be more clear with code, I would like to do something like:

const input = await getByTestId('select-state')
userEvent.type(input, 'C{arrowdown}{enter}')

But type requires an Element and input above is the following object:

{
  selector: "[id='downshift-0-input']",
  nth: [Function: nth],
  browser: [Function: browser]
}

findBy* resolving immediately

I'm trying to get it running with nightwatch and followed the examples, though I cannot get findByLabelText to return a promise and wait for the element.

I tried it like this:

const { getQueriesFrom } = require("@testing-library/nightwatch")

describe("test", function() {
  test("other", async function(browser) {
    const { findByLabelText } = getQueriesFrom(browser)

    browser.url("http://localhost:8080")

    const input = await findByLabelText("Not Existent", { exact: false })
  
    console.log(input)
    // =>  
    // {
    //   selector: '.NWTL_findByLabelText-Not-Existent',
    //   nth: [Function: nth],
    //   browser: [Function: browser]
    // }

    browser.setValue(input, "[email protected]")
    browser.expect.element(input).value.to.equal("[email protected]")
  })
})

The problem is that awaiting findByLabelText returns immediately and is an object with those keys selector, nth and browser. It does not matter if I try with a not existent label or a label that should exist.

Then the next lines of course do not work and I get this error:

Expected element <.NWTL_findByLabelText-Not-Existent> to have value equal: "[email protected]" - element was not found - expected "equal '[email protected]'" but got: "not present" (5073ms)

and

NoSuchElementError: An error occurred while running .setValue() command on <.NWTL_findByLabelText-Not-Existent>: undefined; undefined

Shouldn't findByLabelText wait until it either finds an input or raise an error if it cannot?

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 to 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 with URL https://[secure]@github.com/testing-library/nightwatch-testing-library.git.

Please refer to the authentication configuration documentation to configure the Git credentials on your CI environment and make sure the repositoryUrl is configured with a valid Git URL.


Good luck with your project โœจ

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€

Can't fire a submit event using Nightwatch.js + Testing Library

"@testing-library/dom": "^7.26.7",
"@testing-library/nightwatch": "^2.0.0",
"@testing-library/user-event": "^12.2.2",
"chromedriver": "^87.0.0",
"nightwatch": "^1.5.1"

Node 12.19.0

Relevant code or config

const { getQueriesFrom } = require('@testing-library/nightwatch')
const { fireEvent } = require('@testing-library/dom')

module.exports = {

  async 'Demo test ecosia.org'(browser) {
    await browser.url('https://www.ecosia.org/')

    const { getByPlaceholderText } = getQueriesFrom(browser)

    const searchBox = await getByPlaceholderText(/search the web to plant trees.../i)

    await browser.setValue(searchBox, 'nightwatch')
    await fireEvent.submit(searchBox);

    await browser.end()
  }
}

What you did:
npm t -- tests/ecosiaSearchWithTL.js
What happened:

[Ecosia Search With TL] Test Suite
==================================
โ„น Connected to localhost on port 9515 (1061ms).
  Using: chrome (87.0.4280.66) on Mac OS X platform.

Running:  Demo test ecosia.org


FAILED: 1 errors (913ms)
   Error: Unable to find the "window" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new
       at Object.Demo test ecosia.org (/Users/lsr7759/projects/e2e/tests/ecosiaSearchWithTL.js:14:21)
       at processTicksAndRejections (internal/process/task_queues.js:97:5)
_________________________________________________

TEST FAILURE: 1 error during execution; 0 tests failed, 0 passed (2.642s)

Repo:
https://github.com/lsoares/e2e-nightwatchjs-testinglibrary

Above, I tried firing an event to submit the form. It failed... but I also tried using getByRole but it also failed for other reason. I think getByRole would be better though.

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.