Skip to content
Permalink
Browse files
feat(assertions): new assertions
affects: @serenity-js/assertions, @serenity-js/local-server
  • Loading branch information
jan-molak committed Nov 18, 2018
1 parent 0e9d15f commit bd6fc90
Show file tree
Hide file tree
Showing 23 changed files with 436 additions and 148 deletions.
@@ -10,67 +10,58 @@ describe('Ensure', () => {

const Enrique = Actor.named('Enrique');
const p = value => Promise.resolve(value);
const q = value => Question.about(`answer to some question`, actor => value);

describe('type safety', () => {

it('is compatible with all the different permutations of parameters', () => {

const LastResult = () => Question.about<number>(`the result of the calculation`, (actor: Actor) => {
return 42;
});

const LastPromisedResult = () => Question.about<Promise<number>>(`the result of the calculation`, (actor: Actor) => {
return Promise.resolve(42);
});

return Enrique.attemptsTo(
Ensure.that(42, equals(42)),
Ensure.that(Promise.resolve(42), equals(42)),
Ensure.that(LastResult(), equals(42)),
Ensure.that(LastPromisedResult(), equals(42)),
Ensure.that(42, equals(Promise.resolve(42))),
Ensure.that(Promise.resolve(42), equals(Promise.resolve(42))),
Ensure.that(LastResult(), equals(Promise.resolve(42))),
Ensure.that(LastPromisedResult(), equals(Promise.resolve(42))),
Ensure.that(42, equals(LastResult())),
Ensure.that(Promise.resolve(42), equals(LastResult())),
Ensure.that(LastResult(), equals(Promise.resolve(42))),
Ensure.that(LastPromisedResult(), equals(Promise.resolve(42))),
Ensure.that(42, equals(LastPromisedResult())),
Ensure.that(Promise.resolve(42), equals(LastPromisedResult())),
Ensure.that(LastResult(), equals(LastPromisedResult())),
Ensure.that(LastPromisedResult(), equals(LastPromisedResult())),

// todo Gabi's ideas:
// Ensure.that(WebElement.of('#id'), not(isVisible()))
// Ensure.that(Text.of('#id'), startsWith())
// Ensure.that(Attribute.of('#id').called('class'), startsWith())

);
const q = value => Question.about(`the answer to some question`, actor => value);

it('is compatible with all the different permutations of the `KnowableUnknown` parameters', () => {

const LastResult = () => Question.about<number>(`the result of the calculation`, (actor: Actor) => {
return 42;
});

const LastPromisedResult = () => Question.about<Promise<number>>(`the result of the calculation`, (actor: Actor) => {
return Promise.resolve(42);
});

return Enrique.attemptsTo(
Ensure.that(42, equals(42)),
Ensure.that(Promise.resolve(42), equals(42)),
Ensure.that(LastResult(), equals(42)),
Ensure.that(LastPromisedResult(), equals(42)),
Ensure.that(42, equals(Promise.resolve(42))),
Ensure.that(Promise.resolve(42), equals(Promise.resolve(42))),
Ensure.that(LastResult(), equals(Promise.resolve(42))),
Ensure.that(LastPromisedResult(), equals(Promise.resolve(42))),
Ensure.that(42, equals(LastResult())),
Ensure.that(Promise.resolve(42), equals(LastResult())),
Ensure.that(LastResult(), equals(Promise.resolve(42))),
Ensure.that(LastPromisedResult(), equals(Promise.resolve(42))),
Ensure.that(42, equals(LastPromisedResult())),
Ensure.that(Promise.resolve(42), equals(LastPromisedResult())),
Ensure.that(LastResult(), equals(LastPromisedResult())),
Ensure.that(LastPromisedResult(), equals(LastPromisedResult())),
);
});

given([
{ description: `#actor ensures that 'a value' is equal to 'a value'`, expected: 'a value', actual: 'a value' },
{ description: `#actor ensures that 'a value' is equal to the promised value`, expected: p('a value'), actual: 'a value' },
{ description: `#actor ensures that 'a value' is equal to answer to some question`, expected: q('a value'), actual: 'a value' },
{ description: `#actor ensures that 'a value' is equal to answer to some question`, expected: q(p('a value')), actual: 'a value' },

{ description: `#actor ensures that the promised value is equal to 'a value'`, expected: 'a value', actual: p('a value') },
{ description: `#actor ensures that the promised value is equal to the promised value`, expected: p('a value'), actual: p('a value') },
{ description: `#actor ensures that the promised value is equal to answer to some question`, expected: q('a value'), actual: p('a value') },
{ description: `#actor ensures that the promised value is equal to answer to some question`, expected: q(p('a value')), actual: p('a value') },

{ description: `#actor ensures that answer to some question is equal to 'a value'`, expected: 'a value', actual: q('a value') },
{ description: `#actor ensures that answer to some question is equal to the promised value`, expected: p('a value'), actual: q('a value') },
{ description: `#actor ensures that answer to some question is equal to answer to some question`, expected: q('a value'), actual: q('a value') },
{ description: `#actor ensures that answer to some question is equal to answer to some question`, expected: q(p('a value')), actual: q('a value') },

{ description: `#actor ensures that answer to some question is equal to 'a value'`, expected: 'a value', actual: q(p('a value')) },
{ description: `#actor ensures that answer to some question is equal to the promised value`, expected: p('a value'), actual: q(p('a value')) },
{ description: `#actor ensures that answer to some question is equal to answer to some question`, expected: q('a value'), actual: q(p('a value')) },
{ description: `#actor ensures that answer to some question is equal to answer to some question`, expected: q(p('a value')), actual: q(p('a value')) },
{ description: `#actor ensures that 'a value' does equal 'a value'`, expected: 'a value', actual: 'a value' },
{ description: `#actor ensures that 'a value' does equal the promised value`, expected: p('a value'), actual: 'a value' },
{ description: `#actor ensures that 'a value' does equal the answer to some question`, expected: q('a value'), actual: 'a value' },
{ description: `#actor ensures that 'a value' does equal the answer to some question`, expected: q(p('a value')), actual: 'a value' },

{ description: `#actor ensures that the promised value does equal 'a value'`, expected: 'a value', actual: p('a value') },
{ description: `#actor ensures that the promised value does equal the promised value`, expected: p('a value'), actual: p('a value') },
{ description: `#actor ensures that the promised value does equal the answer to some question`, expected: q('a value'), actual: p('a value') },
{ description: `#actor ensures that the promised value does equal the answer to some question`, expected: q(p('a value')), actual: p('a value') },

{ description: `#actor ensures that the answer to some question does equal 'a value'`, expected: 'a value', actual: q('a value') },
{ description: `#actor ensures that the answer to some question does equal the promised value`, expected: p('a value'), actual: q('a value') },
{ description: `#actor ensures that the answer to some question does equal the answer to some question`, expected: q('a value'), actual: q('a value') },
{ description: `#actor ensures that the answer to some question does equal the answer to some question`, expected: q(p('a value')), actual: q('a value') },

{ description: `#actor ensures that the answer to some question does equal 'a value'`, expected: 'a value', actual: q(p('a value')) },
{ description: `#actor ensures that the answer to some question does equal the promised value`, expected: p('a value'), actual: q(p('a value')) },
{ description: `#actor ensures that the answer to some question does equal the answer to some question`, expected: q('a value'), actual: q(p('a value')) },
{ description: `#actor ensures that the answer to some question does equal the answer to some question`, expected: q(p('a value')), actual: q(p('a value')) },
]).
it('provides a task description based on the assertion given', ({ description, actual, expected }) => {
const task = Ensure.that(actual, equals(expected));
@@ -106,25 +97,25 @@ describe('Ensure', () => {
);

given([
{ description: `'actual' should be equal to 'expected'`, actual: 'actual', expected: 'expected' },
{ description: `'actual' should be equal to 'expected'`, actual: 'actual', expected: p('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: 'actual', expected: q('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: 'actual', expected: q(p('expected')) },

{ description: `'actual' should be equal to 'expected'`, actual: p('actual'), expected: 'expected' },
{ description: `'actual' should be equal to 'expected'`, actual: p('actual'), expected: p('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: p('actual'), expected: q('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: p('actual'), expected: q(p('expected')) },

{ description: `'actual' should be equal to 'expected'`, actual: q('actual'), expected: 'expected' },
{ description: `'actual' should be equal to 'expected'`, actual: q('actual'), expected: p('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: q('actual'), expected: q('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: q('actual'), expected: q(p('expected')) },

{ description: `'actual' should be equal to 'expected'`, actual: q(p('actual')), expected: 'expected' },
{ description: `'actual' should be equal to 'expected'`, actual: q(p('actual')), expected: p('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: q(p('actual')), expected: q('expected') },
{ description: `'actual' should be equal to 'expected'`, actual: q(p('actual')), expected: q(p('expected')) },
{ description: `Expected 'actual' to equal 'expected'`, actual: 'actual', expected: 'expected' },
{ description: `Expected 'actual' to equal 'expected'`, actual: 'actual', expected: p('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: 'actual', expected: q('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: 'actual', expected: q(p('expected')) },

{ description: `Expected 'actual' to equal 'expected'`, actual: p('actual'), expected: 'expected' },
{ description: `Expected 'actual' to equal 'expected'`, actual: p('actual'), expected: p('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: p('actual'), expected: q('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: p('actual'), expected: q(p('expected')) },

{ description: `Expected 'actual' to equal 'expected'`, actual: q('actual'), expected: 'expected' },
{ description: `Expected 'actual' to equal 'expected'`, actual: q('actual'), expected: p('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: q('actual'), expected: q('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: q('actual'), expected: q(p('expected')) },

{ description: `Expected 'actual' to equal 'expected'`, actual: q(p('actual')), expected: 'expected' },
{ description: `Expected 'actual' to equal 'expected'`, actual: q(p('actual')), expected: p('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: q(p('actual')), expected: q('expected') },
{ description: `Expected 'actual' to equal 'expected'`, actual: q(p('actual')), expected: q(p('expected')) },
]).
it('rejects a promise when the assertion fails', ({ description, actual, expected }) =>
expect(Enrique.attemptsTo(
@@ -136,4 +127,6 @@ describe('Ensure', () => {
expect(error.actual).to.equal(actual);
}),
);

// todo add a stringification test [ Array(2) ]
});
@@ -0,0 +1,30 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Actor, AssertionError } from '@serenity-js/core';

import { containsItem, Ensure } from '../../src';

describe('containsItem', () => {

const Enrique = Actor.named('Enrique');

it('checks if a list of items contains the expected value', () =>
expect(Enrique.attemptsTo(
Ensure.that([1, 2, 3], containsItem(2)),
)).to.be.fulfilled);

describe('when used with Ensure', () => {

it('throws an error when the assertion is not met', () => {
return expect(Enrique.attemptsTo(
Ensure.that([1, 2, 3], containsItem(5)),
)).to.be.rejectedWith(AssertionError, `Expected [ 1, 2, 3 ] to contain 5`);
});

it('contributes to the task description', () => {
expect(Ensure.that([1, 2, 3], containsItem(2)).toString())
.to.equal(`#actor ensures that [ 1, 2, 3 ] does contain 2`);
});
});
});
@@ -0,0 +1,30 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Actor, AssertionError } from '@serenity-js/core';

import { containsText, Ensure } from '../../src';

describe('containsText', () => {

const Enrique = Actor.named('Enrique');

it('checks if the actual value contains the expected value', () =>
expect(Enrique.attemptsTo(
Ensure.that('Hello World!', containsText('World')),
)).to.be.fulfilled);

describe('when used with Ensure', () => {

it('throws an error when the assertion is not met', () => {
return expect(Enrique.attemptsTo(
Ensure.that('Hello World!', containsText('Mundo'))),
).to.be.rejectedWith(AssertionError, `Expected 'Hello World!' to contain 'Mundo'`);
});

it('contributes to the task description', () => {
expect(Ensure.that('Hello World!', containsText('World')).toString())
.to.equal(`#actor ensures that 'Hello World!' does contain 'World'`);
});
});
});
@@ -0,0 +1,30 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Actor, AssertionError } from '@serenity-js/core';

import { endsWith, Ensure } from '../../src';

describe('endsWith', () => {

const Enrique = Actor.named('Enrique');

it('checks if the actual value ends with the expected value', () =>
expect(Enrique.attemptsTo(
Ensure.that('Hello World!', endsWith('World!')),
)).to.be.fulfilled);

describe('when used with Ensure', () => {

it('throws an error when the assertion is not met', () => {
return expect(Enrique.attemptsTo(
Ensure.that('Hello World!', endsWith('Mundo!'))),
).to.be.rejectedWith(AssertionError, `Expected 'Hello World!' to end with 'Mundo!'`);
});

it('contributes to the task description', () => {
expect(Ensure.that('Hello World!', endsWith('World!')).toString())
.to.equal(`#actor ensures that 'Hello World!' does end with 'World!'`);
});
});
});
@@ -32,16 +32,16 @@ describe('equals', () => {
Ensure.that(actual, equals(expected)),
)).to.be.fulfilled);

// todo add a stringification test [ Array(2) ]

it('throws an error when the assertion is not met', () => {
return expect(Enrique.attemptsTo(
Ensure.that(true, equals(false)),
)).to.be.rejectedWith(AssertionError, 'true should be equal to false');
});

it('contributes to the task description', () => {
expect(Ensure.that(true, equals(true)).toString())
.to.equal(`#actor ensures that true is equal to true`);
describe('when used with Ensure', () => {
it('throws an error when the assertion is not met', () => {
return expect(Enrique.attemptsTo(
Ensure.that(true, equals(false)),
)).to.be.rejectedWith(AssertionError, 'Expected true to equal false');
});

it('contributes to the task description', () => {
expect(Ensure.that(true, equals(true)).toString())
.to.equal(`#actor ensures that true does equal true`);
});
});
});
@@ -0,0 +1,30 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Actor, AssertionError } from '@serenity-js/core';

import { Ensure, isGreaterThan } from '../../src';

describe('isGreaterThan', () => {

const Enrique = Actor.named('Enrique');

it('checks if the actual value is greater than the expected value', () =>
expect(Enrique.attemptsTo(
Ensure.that(3, isGreaterThan(2)),
)).to.be.fulfilled);

describe('when used with Ensure', () => {

it('throws an error when the assertion is not met', () => {
return expect(Enrique.attemptsTo(
Ensure.that(2, isGreaterThan(3)),
)).to.be.rejectedWith(AssertionError, `Expected 2 to have value greater than 3`);
});

it('contributes to the task description', () => {
expect(Ensure.that(3, isGreaterThan(2)).toString())
.to.equal(`#actor ensures that 3 does have value greater than 2`);
});
});
});
@@ -0,0 +1,30 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Actor, AssertionError } from '@serenity-js/core';

import { Ensure, isLessThan } from '../../src';

describe('isLessThan', () => {

const Enrique = Actor.named('Enrique');

it('checks if the actual value is less than the expected value', () =>
expect(Enrique.attemptsTo(
Ensure.that(2, isLessThan(3)),
)).to.be.fulfilled);

describe('when used with Ensure', () => {

it('throws an error when the assertion is not met', () => {
return expect(Enrique.attemptsTo(
Ensure.that(2, isLessThan(1)),
)).to.be.rejectedWith(AssertionError, `Expected 2 to have value less than 1`);
});

it('contributes to the task description', () => {
expect(Ensure.that(2, isLessThan(3)).toString())
.to.equal(`#actor ensures that 2 does have value less than 3`);
});
});
});

0 comments on commit bd6fc90

Please sign in to comment.