Giter Site home page Giter Site logo

ember-cli-custom-assertions's Introduction

ember-cli-custom-assertions # Build Status

ember-cli-custom-assertions is built and maintained by DockYard, contact us for expert Ember.js consulting.

About

Add custom assertions to your Ember test suite.

Installing

ember install ember-cli-custom-assertions

Looking for help?

If it is a bug please open an issue on GitHub.

Usage

Add new assertions to test/assertions. Then use on the assert object in your test suite.

For example:

// tests/assertions/contains.js
export default function(context, element, text, message) {
  message = message || `${element} should contain "${text}"`;
  let actual = context.$(element).text();
  let expected = text;
  let result = !!actual.match(new RegExp(expected));

  this.pushResult({ result, actual, expected, message });
}

// tests/acceptance/foo-test.js
test('foo is bar', function(assert) {
  visit('/');

  andThen(function() {
    assert.contains('.foo', 'Foo Bar');
  });
});

Note: hyphenated file names like tests/assertions/double-trouble.js will be camelized: assert.doubleTrouble

Blueprint

You can generate a new assertion by using the assertion blueprint:

ember g assertion double-trouble

Assertion

A context is always injected as the first argument. You don't need to pass a context when calling the assertion.

// good
assert.contains('.foo', 'Foo bar');

// bad
assert.contains(app, '.foo', 'Foo bar');

Setup

You must inject the assertions to use them in tests.

New testing API

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import setupCustomAssertions from 'ember-cli-custom-assertions/test-support';

module('default setup', function(hooks) {
  setupTest(hooks);
  setupCustomAssertions(hooks);

  test('can inject custom assertions', function(assert) {
    assert.contains('.foo', 'Foo bar');
  });
});

Old testing API

// ...
import { assertionInjector, assertionCleanup } from 'ember-cli-custom-assertions/test-support';

module('Acceptance | foo', {
  beforeEach: function() {
    var application = startApp();
    assertionInjector(application);
  },

  afterEach: function() {
    Ember.run(application, 'destroy');
    assertionCleanup(application);
  }
});

Authors

We are very thankful for the many contributors

Versioning

This library follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this library. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard, Inc © 2015

@dockyard

Licensed under the MIT license

ember-cli-custom-assertions's People

Contributors

andrewhavens avatar bcardarella avatar cibernox avatar daniel-xu avatar ember-tomster avatar jelhan avatar jleja avatar lolmaus avatar maabernethy avatar miguelcobain avatar ryanto avatar serabe avatar snewcomer avatar turbo87 avatar xiphiasuvella 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ember-cli-custom-assertions's Issues

In util tests, custom asserters are not curried with context.

So I've got a custom assertion. In an integration test I use it like this:

assert.foo('bar', 'baz');

But in a util test, I have to invoke my custom assertion like this:

assert.foo(null, 'bar', 'baz');

I assume that in util tests the custom assertion function is not being curried with context.

auto-inject assertions

We'd like to avoid having to setup and tear down the assertions manually. auto-injecting the assertions would be great. Ensuring the context (app, component, etc...) is passed in is important as well.

Doesn't work from within addons

Version

ember-cli: 2.9.0-beta.2
node: 6.6.0
os: darwin x64
ember-cli-custom-assertions: 0.0.6

Test Case

Not really a test case.
https://github.com/DockYard/ember-cli-custom-assertions/issues/new

Steps to reproduce

Install it in an empty addon and try to run ember g assertion is-cool.

Expected Behavior

An assertion is generated

Actual Behavior

Error: Cannot find module 'ember-cli-string-utils'

Given that I use this addon in apps without problem, seems the issue arises when used from within another addon.

Incompatibility with Embroider

I've been checking compatibility with Embroider and various Addons in the community and I came across that this addon is incompatible.

Specifically I was trying to add ember-try Embroider configuration to Ember Power Calendar, which uses this addon, but the tests fail on its custom assertion usage. The problem is that the custom assertions that the Power Calendar tests add aren't added to the global QUnit object.

Uncaught TypeError: assert.isDay is not a function

Doing a bit of digging I came across this issue (and PR), on the qunit-dom repo which similarly tries to add custom assertions to the global QUnit object. It appears this is no longer possible under ember-qunit v5 (migration guide) which is required to support Embroider.

tooling can no longer depend on the QUnit global to be defined

From the looks of it there appear to be a few changes that need to be made for it to work to support ember-qunit v5 (and therefore Embroider).

For example in qunit-dom, users now bootstrap the assertions in test-helper.js and pass in QUnit.assert.

// tests/test-helper.js
import * as QUnit from 'qunit';
import { setup } from 'qunit-dom';
//...snip...
setup(QUnit.assert);

Trying to run the tests I put together a branch which has Embroider + ember-try setup but I think to start, the repo will need be updated to use a newer ember-cli for it to work.

Upgrade to ember-cli-babel@^6.0.0

This addon is outdated with the recent Ember versions

[email protected]:
  version "0.0.4"
  resolved "https://registry.yarnpkg.com/ember-cli-custom-assertions/-/ember-cli-custom-assertions-0.0.4.tgz#e4cf1db3b575dade9fdd1a9080f6a7e975aa6f9e"
  dependencies:
    ember-cli-babel "^5.0.0"
$> ember -v

ember-cli: 2.14.1
node: 8.2.1
os: darwin x64

Failing test scenarios

3 of the ember-try scenarios are failing after #55 merge (looks like tests didn't run there). Opening an issue to track:

ember-default-with-jquery
embroider-safe
embroider-optimized

ember-defeault-with-jquery

Seems to be failing with:

Setting the `jquery-integration` optional feature flag to `true` was deprecated in Ember 3.x and removed in Ember 4.0.0. You must add the `@ember/optional-features` addon and set this feature to `false`.

For more information, see the deprecation guide: https://deprecations.emberjs.com/v3.x/#toc_optional-feature-jquery-integration

I tried adding this dependency into the ember try scenario, but it didn't seem to work (package was installed, but same failure message). It might make most sense to just drop this scenario.

embroider scenarios

Not sure what to do about these!

Inject assertion on Unit tests

I saw in the documentation that we need to pass the Application as context to inject the assertions. What about with unit tests, how should this be done?

Generator missing dependency

Just tried to run ember g assertion contains and I get the following error:

Cannot find module 'ember-cli/lib/utilities/string'
Error: Cannot find module 'ember-cli/lib/utilities/string'
...

I notice in blueprints/assertion/index.js the first line requires that module

var stringUtil = require('ember-cli/lib/utilities/string');

but digging through node_modules, I don't actually see that file. Could this be a version mismatch issue? Here's the output of ember v:

ember-cli: 2.4.3
node: 5.3.0
os: darwin x64

PhantomJS does not recognize assertion

Added a custom assertion and it runs fine in Chrome but fails in PhantomJS.

Expected at least one assertion, but none were run - call expect(0) to accept zero assertions

  • ember-cli: 2.4.2
  • node: 4.3.2
  • PhantomJS: 2.0

My custom assertion is pretty long where attempting to do a pushResult on a bunch of expected behavior that originally was multiple assertions.

export default function userMessage(context, message, messageText = '', numMessages = 1, type = 'error') {
  const userMessageEl = context.$('[data-test-userMessages]');
  if (!userMessageEl) {
    return this.pushResult({
      result: false,
      actual: 0,
      expected: numMessages,
      message: `${message} - User Message Did Not Appear`
    });
  }

  const actualNumMessages = userMessageEl.length;
  if (actualNumMessages !== numMessages) {
    return this.pushResult({
      result: false,
      actual: actualNumMessages,
      expected: numMessages,
      message: `${message} - Unexpected User Messages`
    });
  }

  const actualMessage = context.$('[data-test-userMessageDetail').text().trim();
  if (messageText && (messageText !== actualMessage)) {
    return this.pushResult({
      result: false,
      actual: actualMessage,
      expected: messageText,
      message: `${message} - Incorrect Message Text`
    });
  }

  const expectedTypeClass = `user-messages__message--${type}`;
  if (!userMessageEl.hasClass(expectedTypeClass)) {
    const actualClasses = userMessageEl.attr('class');
    return this.pushResult({
      result: false,
      actual: actualClasses,
      expected: expectedTypeClass,
      message: `${message} - Incorrect Type Class`
    });
  }

  const expected = `${numMessages} - ${messageText}`;
  const actual = expected;
  return this.pushResult({
    result: true,
    actual,
    expected,
    message
  });
}

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.