Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feat(assertions): new assertions library
affects: @serenity-js/assertions, @serenity-js/core
- Loading branch information
Showing
with
842 additions
and 554 deletions.
- +36 −117 packages/assertions/spec/Ensure.spec.ts
- +54 −0 packages/assertions/spec/assertions/Assertion.spec.ts
- +40 −0 packages/assertions/spec/assertions/and.spec.ts
- +30 −0 packages/assertions/spec/assertions/contains.spec.ts
- +0 −30 packages/assertions/spec/assertions/containsItem.spec.ts
- +0 −30 packages/assertions/spec/assertions/containsText.spec.ts
- +16 −16 packages/assertions/spec/assertions/endsWith.spec.ts
- +23 −28 packages/assertions/spec/assertions/equals.spec.ts
- +30 −0 packages/assertions/spec/assertions/includes.spec.ts
- +17 −17 packages/assertions/spec/assertions/isGreaterThan.spec.ts
- +16 −16 packages/assertions/spec/assertions/isLessThan.spec.ts
- +30 −0 packages/assertions/spec/assertions/matches.spec.ts
- +0 −30 packages/assertions/spec/assertions/matchesRegex.spec.ts
- +200 −15 packages/assertions/spec/assertions/not.spec.ts
- +40 −0 packages/assertions/spec/assertions/or.spec.ts
- +16 −16 packages/assertions/spec/assertions/startsWith.spec.ts
- +0 −73 packages/assertions/spec/values.spec.ts
- +0 −17 packages/assertions/src/Assertion.ts
- +15 −26 packages/assertions/src/Ensure.ts
- +55 −0 packages/assertions/src/assertions/Assertion.ts
- +25 −0 packages/assertions/src/assertions/and.ts
- +9 −0 packages/assertions/src/assertions/contains.ts
- +0 −19 packages/assertions/src/assertions/containsItem.ts
- +0 −18 packages/assertions/src/assertions/containsText.ts
- +4 −10 packages/assertions/src/assertions/endsWith.ts
- +5 −9 packages/assertions/src/assertions/equals.ts
- +8 −0 packages/assertions/src/assertions/includes.ts
- +7 −4 packages/assertions/src/assertions/index.ts
- +4 −10 packages/assertions/src/assertions/isGreaterThan.ts
- +4 −10 packages/assertions/src/assertions/isLessThan.ts
- +8 −0 packages/assertions/src/assertions/matches.ts
- +0 −18 packages/assertions/src/assertions/matchesRegex.ts
- +29 −9 packages/assertions/src/assertions/not.ts
- +54 −0 packages/assertions/src/assertions/or.ts
- +4 −10 packages/assertions/src/assertions/startsWith.ts
- +0 −2 packages/assertions/src/index.ts
- +40 −0 packages/core/spec/io/formatted.spec.ts
- +22 −4 packages/{assertions/src/values.ts → core/src/io/formatted.ts}
- +1 −0 packages/core/src/io/index.ts
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,132 +1,51 @@ | ||
import 'mocha'; | ||
import { given } from 'mocha-testdata'; | ||
|
||
import { expect } from '@integration/testing-tools'; | ||
import { Actor, AssertionError, KnowableUnknown, Question } from '@serenity-js/core'; | ||
import { Assertion, Ensure } from '../src'; | ||
|
||
/** @test {Ensure} */ | ||
describe('Ensure', () => { | ||
|
||
const | ||
Enrique = Actor.named('Enrique'), | ||
|
||
isIdenticalTo = <T>(expected: T) => | ||
Assertion.thatActualShould<T, T>('have value identical to', expected) | ||
.soThat((actualValue: T, expectedValue: T) => actualValue === expectedValue), | ||
|
||
p = <T>(value: T) => Promise.resolve(value), | ||
q = <T>(value: T): Question<T> => Question.about(`something`, actor => value); | ||
|
||
/** @test {Ensure.that} */ | ||
it('allows the actor to make an assertion', () => { | ||
return expect(Enrique.attemptsTo( | ||
Ensure.that(4, isIdenticalTo(4)), | ||
)).to.be.fulfilled; | ||
}); | ||
|
||
/** @test {Ensure.that} */ | ||
it('fails the actor flow when the assertion is not met', () => { | ||
return expect(Enrique.attemptsTo( | ||
Ensure.that(4, isIdenticalTo(7)), | ||
)).to.be.rejectedWith(AssertionError, 'Expected 4 to have value identical to 7'); | ||
}); | ||
|
||
/** @test {Ensure.that} */ | ||
it('provides a description of the assertion being made', () => { | ||
expect(Ensure.that(4, isIdenticalTo(7)).toString()).to.equal(`#actor ensures that 4 does have value identical to 7`); | ||
}); | ||
|
||
given<KnowableUnknown<number>>( | ||
42, | ||
p(42), | ||
q(42), | ||
q(p(42)), | ||
). | ||
it('allows for the actual to be a KnowableUnknown<T> as it compares its value', (actual: KnowableUnknown<number>) => { | ||
return expect(Enrique.attemptsTo( | ||
Ensure.that(actual, isIdenticalTo(42)), | ||
)).to.be.fulfilled; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,54 @@ | ||
import 'mocha'; | ||
import { given } from 'mocha-testdata'; | ||
|
||
import { expect } from '@integration/testing-tools'; | ||
import { Actor, AssertionError, KnowableUnknown, Question } from '@serenity-js/core'; | ||
import { Assertion, Ensure } from '../../src'; | ||
|
||
/** @test {Assertion} */ | ||
describe('Assertion', () => { | ||
|
||
const | ||
Astrid = Actor.named('Astrid'), | ||
p = <T>(value: T) => Promise.resolve(value), | ||
q = <T>(value: T): Question<T> => Question.about(`something`, actor => value); | ||
|
||
describe('allows to easily define an assertion, which', () => { | ||
|
||
const isIdenticalTo = <T>(expected: T) => | ||
Assertion.thatActualShould<T, T>('have value identical to', expected) | ||
.soThat((actualValue: T, expectedValue: T) => actualValue === expectedValue); | ||
|
||
/** | ||
* @test {Assertion.that} | ||
* @test {Ensure.that} | ||
*/ | ||
it('allows the actor flow to continue when the assertion passes', () => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that(4, isIdenticalTo(4)), | ||
)).to.be.fulfilled; | ||
}); | ||
|
||
/** | ||
* @test {Assertion.that} | ||
* @test {Ensure.that} | ||
*/ | ||
it('stops the actor flow when the assertion fails', () => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that(4, isIdenticalTo('4' as any)), | ||
)).to.be.rejectedWith(AssertionError, "Expected 4 to have value identical to '4'"); | ||
}); | ||
|
||
given<KnowableUnknown<number>>( | ||
42, | ||
p(42), | ||
q(42), | ||
q(p(42)), | ||
). | ||
it('allows for the expected value to be defined as any KnowableUnknown<T>', (expected: KnowableUnknown<number>) => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that(42, isIdenticalTo(expected)), | ||
)).to.be.fulfilled; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,40 @@ | ||
import 'mocha'; | ||
|
||
import { expect } from '@integration/testing-tools'; | ||
import { Actor, AssertionError } from '@serenity-js/core'; | ||
import { and, endsWith, Ensure, startsWith } from '../../src'; | ||
|
||
describe('and', () => { | ||
|
||
const Astrid = Actor.named('Astrid'); | ||
|
||
/** @test {and} */ | ||
it(`allows for the actor flow to continue when the "actual" meets all the expectations`, () => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that('Hello World!', and(startsWith('Hello'), endsWith('World!'))), | ||
)).to.be.fulfilled; | ||
}); | ||
|
||
describe(`breaks the actor flow when "actual"`, () => { | ||
|
||
/** @test {and} */ | ||
it(`does not meet the first expectation`, () => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that('Hello World!', and(startsWith('¡Hola'), endsWith('World!'))), | ||
)).to.be.rejectedWith(AssertionError, `Expected 'Hello World!' to start with '¡Hola'`); | ||
}); | ||
|
||
/** @test {and} */ | ||
it(`does not meet the second expectation`, () => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that('Hello World!', and(startsWith('Hello'), endsWith('Mundo!'))), | ||
)).to.be.rejectedWith(AssertionError, `Expected 'Hello World!' to end with 'Mundo!`); | ||
}); | ||
}); | ||
|
||
/** @test {and} */ | ||
it(`contributes to a human-readable description`, () => { | ||
expect(Ensure.that('Hello', and(startsWith('H'), endsWith('o'))).toString()) | ||
.to.equal(`#actor ensures that 'Hello' does start with 'H' and end with 'o'`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,30 @@ | ||
import 'mocha'; | ||
|
||
import { expect } from '@integration/testing-tools'; | ||
import { Actor, AssertionError } from '@serenity-js/core'; | ||
import { contains, Ensure } from '../../src'; | ||
|
||
describe('contains', () => { | ||
|
||
const Astrid = Actor.named('Astrid'); | ||
|
||
/** @test {contains} */ | ||
it(`allows for the actor flow to continue when the "actual" contains the "expected" text`, () => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that([ { word: 'Hello' }, { word: 'World' } ], contains({ word: 'World' })), | ||
)).to.be.fulfilled; | ||
}); | ||
|
||
/** @test {contains} */ | ||
it(`breaks the actor flow when "actual" does not contain the "expected" text`, () => { | ||
return expect(Astrid.attemptsTo( | ||
Ensure.that([ 'Hello', 'World' ], contains('Mundo')), | ||
)).to.be.rejectedWith(AssertionError, `Expected [ 'Hello', 'World' ] to contain 'Mundo'`); | ||
}); | ||
|
||
/** @test {contains} */ | ||
it(`contributes to a human-readable description`, () => { | ||
expect(Ensure.that([ 1, 2, 3 ], contains(2)).toString()) | ||
.to.equal(`#actor ensures that [ 1, 2, 3 ] does contain 2`); | ||
}); | ||
}); |
Oops, something went wrong.