当前位置: 首页 > 软件库 > 程序开发 > >

ember-cli-yadda

Write cucumber specs for ember-cli applications
授权协议 MIT License
开发语言 JavaScript
所属分类 程序开发
软件类型 开源软件
地区 不详
投 递 者 张坚白
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

CI

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 intoyour Ember test setup using either ember-qunit orember-mocha.

The following describes the use of ember-cli-yadda >= v0.4.0 which works only with the latest modernEmber testing APIs, as laid out in the RFCs232and268.

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 documentationon 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 generatesa 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 anintegration test, or --type=unit for a unit test. This generates a feature and step definition file where you canwrite 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 thesetupApplicationTest() function provided by either ember-qunit or ember-mocha. See the Annotationssection 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 canalso 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 samecontext (like this.element or this.owner) and helpers from @ember/test-helpers (like click()), as you did whenwriting 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 canbe 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 yourwill.

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 anenvironment variable like process.env.ANNOTATIONS). This will then run only those Features or Scenarios that have oneof these annotations assigned.

Setup tests

For each of the setup functions already known from ember-qunit or ember-mocha, there exists a correspondingannotation 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 foradditional annotations, or extend the existing ones. This module has to export these hooks, that are called by thisaddon's test runner:

  • runFeature: called for each feature. If you return a function, this will be called to run the feature, instead ofthe 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 beforeEachand 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 thesetupMirage() 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.

 相关资料
  • Ember CLI 是一个 Ember.js 命令行工具,提供了由 broccoli 提供的快速的资源管道和项目结构。 Ember CLI 基于 Ember App Kit Project 目前已经废弃。 Assets Compilation Ember CLI asset compilation is based on broccoli. Broccoli has support for: Ha

  • This repository is no longer maintained. As a replacement check out: https://github.com/sir-dunxalot/ember-tooltips Ember CLI Tooltipster An Ember CLI add-on that wraps Tooltipster into an ember compo

  • ember-cli-updater This ember-cli addon helps you update your ember-cli application or addon. The idea of this addon is to automate some parts of the upgrade process so it's simplified. Not every chang

  • Ember-cli-simditor Ember component wrapper for simditor. Changes 0.0.7 Different from previous version, you must wrap content in object. See issue 6 for why. Getting Started Installation In your ember

  • ember-cli-chai Chai assertions for Ember.js. Deprecated This package is deprecated. Please use ember-auto-import to use chai and chai plugins directly. If you'd like to use chai, or were previously us

  • ember-cli-storybook �� Ember storybook adapter Compatibility Ember.js v3.16 or above Ember CLI v2.13 or above Node.js v10 or above Installation ember install @storybook/ember-cli-storybook Usage This