npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-cli-yadda

v0.7.0

Published

Ember-cli yadda addon for running Gherkin acceptance tests

Downloads

5,165

Readme

CI Ember Observer Score

Ember-cli-yadda

This Ember CLI addon facilitates writing BDD tests in the Gherkin language and executing them against your Ember app.

@mschinis (Micheal Schinis) Did a great talk at @emberlondon BDD approach with ember using ember-cli-yadda.

It uses the yadda library to parse and run your feature files, and integrates into your Ember test setup using either ember-qunit or ember-mocha.

The following describes the use of ember-cli-yadda >= v0.4.0 which works only with the latest modern Ember testing APIs, as laid out in the RFCs 232 and 268.

For the older APIs use v0.3.x and have a look at our Legacy Guide.

Compatibility

  • Ember.js v3.20 or above
  • Ember CLI v3.20 or above
  • Node.js v12 or above
  • ember-auto-import v2 or Embroider

Due to the way yadda is written, you need to add some custom webpack config options when using Embroider!

Installation

Installing ember-cli-yadda is a breeze. All you need to do is run the following command in your project directory.

ember install ember-cli-yadda

This adds the following files:

/tests/acceptance/steps/steps.js
/tests/integration/steps/steps.js
/tests/unit/steps/steps.js
/tests/helpers/yadda-annotations.js

You may specify the version of yadda by adding it in package.json and running npm install.

Upgrading

See the Release Notes.

Usage

The following describes the specific features and Ember integration points of ember-cli-yadda. For general documentation on how to write yadda-based tests please consult the Yadda User Guide.

Creating feature files

This ember-cli addon provides you with a blueprint with which you can create feature files:

ember g feature [feature title] --type=[acceptance|integration|unit]

Acceptance tests

For acceptance tests you can omit the --type option. So you can use ember g feature [feature title] which generates a feature file for your acceptance tests and a step definition.

For example:

ember g feature make-a-feature

This will generate the following files in your project directory:

/tests/acceptance/steps/make-a-feature-steps.js
/tests/acceptance/make-a-feature.feature

Integration or unit tests

To create an integration or unit test, you can use ember g feature [feature title] --type=integration for an integration test, or --type=unit for a unit test. This generates a feature and step definition file where you can write your tests.

For example:

ember g feature make-a-feature --type=unit

This will generate the following files in your project directory:

/tests/unit/steps/make-a-feature-steps.js
/tests/unit/make-a-feature.feature

Writing tests

Let's take this example of an acceptance test feature:

@setupApplicationTest
Feature: bananas rot

  Scenario: bananas rot faster when next to apples
    Given I have a bananas
    And it's next to an apples
    When left together for a while
    Then the banana rots

The @setupApplicationTest annotation will setup all scenarios of this feature as application tests, using the setupApplicationTest() function provided by either ember-qunit or ember-mocha. See the Annotations section below for more information on how to setup your tests.

Because we probably have more features about bananas, we add the Given I have bananas to the global steps file: /tests/acceptance/steps.js

import yadda from 'yadda';
import { visit } from '@ember/test-helpers';

export default function(assert) {
  return yadda.localisation.default.library()
    .given("I have bananas", async function() {
      await visit("/bananas");
    });
}

Notice that the preferable way to handle asynchronous steps like the one above is to use async/ await. But you can also explicitly return a promise or use a next() callback.

The fact that "it's next to apples" is probably unique to this Feature so we'll add it to the feature specific step definitions in /tests/acceptance/steps/bananas-rot-feature-steps.js. That will look like this:

import steps from './steps';

// step definitions that are shared between features should be moved to the
// tests/accptance/steps/steps.js file

export default function(assert) {
  return steps(assert)
    .given('it\'s next to apples', function() {
      let apples = this.element.querySelectorAll('.apple');
      assert.ok(apples.length > 0)
    })
    .when('left together for a while', function(next) {
      // bananas rot really quickly next to apples.
      setTimeout(next, 1000);
    })
    .then('the banana rots', function () {
      let banana = this.element.querySelector('.banana');
      assert.ok(banana.classList.contains('rotten'));
    });
}

Important information

Scope and helpers

ember-cli-yadda passes the original scope down to each step definition. This means that you have access to the same context (like this.element or this.owner) and helpers from @ember/test-helpers (like click()), as you did when writing a normal test in QUnit/Mocha.

Sharing variables between steps

You can easily share variables between your steps, by either creating a new variable outside your step chain, or by storing the values in this.ctx in each step.

For Example:

  import steps from './steps';

  // Variable outside step chain
  let something = '';

  export default function(assert) {
    return steps(assert)
      .given('I add something to the context', function() {
        // Assign 'hello' to the variable outside the step chain
        something = 'hello';
        // Assign 'there' to a new variable in `this.ctx`
        this.ctx.something = 'there';
        assert.ok(true, this.step);
      })
      .then('it should be there in the next step', function() {
        // Do an assertion to check that 'there' has been passed correctly
        // to the next step
        assert.equal(this.ctx.something, 'there', this.step);
      })
      .then('external variable should be there in the next step', function(){
        // Assert that the external variable still holds the information
        // we set in the first step
        assert.equal(something,'hello',this.step);
      });
  }

Annotations

You already saw the use of the @setupApplicationTest annotation in the example feature file above. Yadda's support for annotations can be used to customize the way tests are run.

The implementation for the way certain annotations affect your tests lives in the tests/yadda-annotations.js file. The addon installs this file with a default implementation as described below, but you can freely customize it at your will.

Skipping tests

See the Contributing guide for details. You can skip tests by adding the @ignore annotation above the Scenario or Feature.

Test suites

You can set ENV.annotations to an array of annotations (either statically or e.g. by assigning them from an environment variable like process.env.ANNOTATIONS). This will then run only those Features or Scenarios that have one of these annotations assigned.

Setup tests

For each of the setup functions already known from ember-qunit or ember-mocha, there exists a corresponding annotation to setup your Feature/Scenario accordingly:

  • @setupTest for (unit) tests requiring the DI container of Ember to be set up
  • @setupRenderingTest for (integration) tests allowing you to call render, e.g. for component tests
  • @setupApplicationTest for (acceptance) tests requiring the whole application to be booted

Customization

You can customize how annotations are handled in your app's tests/yadda-annotations.js file, e.g. to add support for additional annotations, or extend the existing ones. This module has to export these hooks, that are called by this addon's test runner:

  • runFeature: called for each feature. If you return a function, this will be called to run the feature, instead of the default implementation.
  • runScenario: similar to runFeature, but called for each scenario.
  • setupFeature: called for each feature to setup the test environment. You can call QUnit's or Mocha's beforeEach and afterEach functions here to add custom setup/teardown work.
  • setupScenario: similar to setupFeature, but called for each scenario.

Have a look at the existing implementation and the comments present in your tests/yadda-annotations.js file!

Here is an example to extend the defaul implementation of the @setupApplicationTest annotation to also call the setupMirage() function provided by ember-cli-mirage to setup the Mirage server:

import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

// your existing tests/yadda-annotations.js file...

function setupYaddaTest(annotations) {
  if (annotations.setupapplicationtest) { // lower case!
    return function(hooks) {
      setupApplicationTest(hooks);
      setupMirage(hooks);
    }
  }
  // ...
}

Yadda Configuration

If you need to set Yadda configuration, add the following to ember-cli-build.js:

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {

    'ember-cli-yadda': {
      yaddaOptions: { // passed through to yadda parseFeature()
        language: 'Polish', // converted to Yadda.localisation.Polish
        leftPlaceholderChar: '<',
        rightPlaceholderChar: '>'
      }

    }
  });

See yadda FeatureParser for yadda options.

Inner workings

This ember addon registers a preprocessor that parses .feature / .spec / .specification files using yadda and generates a -test.js file in the apropriate test folder. It also adds a little loader helper /tests/helpers/yadda.js because yadda does not define an amd module.

The addon also adds ES6 modules /tests/[type]/steps/steps you can extend in feature specific step definitions. Any shared step definitions should be moved to these file or included there, depending on the type of test you are running. Feature specific step definitions reside in /tests/[type]/steps/. The generated feature test js files import a /tests/[type]/steps/[feature title]-steps module, where type can either be acceptance, integration or unit.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.