-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Chai assertion example (#235)
* update chai plugin recipe * add custom assertion example * add IntelliSense for custom assertions
- Loading branch information
Showing
7 changed files
with
141 additions
and
67 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,59 @@ | ||
# Adding Chai Assertions | ||
|
||
- Extend [`chai`](http://chaijs.com/) with the [`chai-date-string`](http://chaijs.com/plugins/chai-date-string/) assertion plugin, see [cypress/support/assertions.js](cypress/support/assertions.js) | ||
- Extend [`chai`](http://chaijs.com/) with the [`chai-colors`](http://chaijs.com/plugins/chai-colors/) assertion plugin, see [spec file](cypress/integration/extending-chai-assertion-plugins-spec.js) | ||
- Extend [`chai`](http://chaijs.com/) with the [`chai-date-string`](http://chaijs.com/plugins/chai-date-string/) assertion plugin from [cypress/support/index.js](cypress/support/index.js) so it is available in every spec file. | ||
- Extend [`chai`](http://chaijs.com/) with the [`chai-colors`](http://chaijs.com/plugins/chai-colors/) assertion plugin in a single [spec file](cypress/integration/extending-chai-assertion-plugins-spec.js) | ||
- Globally extend [`chai`](http://chaijs.com/) for all specs. | ||
- Write and use own Chai assertion, see [cypress/support/index.js](cypress/support/index.js) | ||
|
||
### Code completion | ||
|
||
Even if you write your specs in JavaScript, you can have Intelligent Code Completion for custom assertions. | ||
|
||
1. Add a `.d.ts` file with type definition and good JSDoc comment, for example here is [cypress/support/index.d.ts](cypress/support/index.d.ts). | ||
|
||
```ts | ||
// cypress/support/index.d.ts file | ||
// extends Cypress assertion Chainer interface with | ||
// the new assertion methods | ||
|
||
/// <reference types="cypress" /> | ||
|
||
declare namespace Cypress { | ||
interface Chainer<Subject> { | ||
/** | ||
* Custom Chai assertion that checks if given subject is string "foo" | ||
* | ||
* @example | ||
``` | ||
expect('foo').to.be.foo() | ||
cy.wrap('foo').should('be.foo') | ||
``` | ||
* */ | ||
(chainer: 'be.foo'): Chainable<Subject> | ||
|
||
/** | ||
* Custom Chai assertion that checks if given subject is NOT string "foo" | ||
* | ||
* @example | ||
``` | ||
expect('bar').to.not.be.foo() | ||
cy.wrap('bar').should('not.be.foo') | ||
``` | ||
* */ | ||
(chainer: 'not.be.foo'): Chainable<Subject> | ||
} | ||
} | ||
``` | ||
|
||
2. In the spec file add special comment `reference path` pointing at the `.d.ts` file to load it. | ||
|
||
```js | ||
/// <reference types="Cypress" /> | ||
/// <reference path="../support/index.d.ts" /> | ||
``` | ||
|
||
In a modern code editor like VSCode you should see IntelliSense pop up when hovering over the new assertion method. | ||
|
||
 | ||
|
||
**Related:** [IntelliSense for custom Cypress commands](https://github.com/cypress-io/cypress-example-todomvc#custom-commands) |
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
9 changes: 0 additions & 9 deletions
9
examples/extending-cypress__chai-assertions/cypress/support/assertions.js
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
examples/extending-cypress__chai-assertions/cypress/support/commands.js
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
examples/extending-cypress__chai-assertions/cypress/support/index.d.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/// <reference types="cypress" /> | ||
|
||
declare namespace Cypress { | ||
interface Chainer<Subject> { | ||
/** | ||
* Custom Chai assertion that checks if given subject is string "foo" | ||
* | ||
* @example | ||
``` | ||
expect('foo').to.be.foo() | ||
cy.wrap('foo').should('be.foo') | ||
``` | ||
* */ | ||
(chainer: 'be.foo'): Chainable<Subject> | ||
|
||
/** | ||
* Custom Chai assertion that checks if given subject is NOT string "foo" | ||
* | ||
* @example | ||
``` | ||
expect('bar').to.not.be.foo() | ||
cy.wrap('bar').should('not.be.foo') | ||
``` | ||
* */ | ||
(chainer: 'not.be.foo'): Chainable<Subject> | ||
} | ||
} |
54 changes: 34 additions & 20 deletions
54
examples/extending-cypress__chai-assertions/cypress/support/index.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
// *********************************************************** | ||
// This example support/index.js is processed and | ||
// loaded automatically before your test files. | ||
// | ||
// This is a great place to put global configuration and | ||
// behavior that modifies Cypress. | ||
// | ||
// You can change the location of this file or turn off | ||
// automatically serving support files with the | ||
// 'supportFile' configuration option. | ||
// | ||
// You can read more here: | ||
// https://on.cypress.io/configuration | ||
// *********************************************************** | ||
// because this file is imported from cypress/support/index.js | ||
// that means all other spec files will have this assertion plugin | ||
// available to them because the supportFile is bundled and served | ||
// prior to any spec files loading | ||
import chaiDateString from 'chai-date-string' | ||
|
||
// Import commands.js using ES2015 syntax: | ||
import './commands' | ||
// chai is a global exposed by Cypress which means | ||
// we can just simply extend it | ||
chai.use(chaiDateString) | ||
|
||
// also import 3rd party assertions | ||
// which will globally modify chai | ||
// and make these accessible to all specs | ||
import "./assertions" | ||
/** | ||
* Example that shows how to write a custom Chai assertion. | ||
* | ||
* @see https://www.chaijs.com/guide/helpers/ | ||
* @example | ||
``` | ||
expect('foo').to.be.foo() | ||
expect('bar').to.not.be.foo() | ||
cy.wrap('foo').should('be.foo') | ||
cy.wrap('bar').should('not.be.foo') | ||
``` | ||
* */ | ||
const isFoo = (_chai, utils) => { | ||
function assertIsFoo (options) { | ||
this.assert( | ||
this._obj === 'foo', | ||
'expected #{this} to be string "foo"', | ||
'expected #{this} to not be string "foo"', | ||
this._obj | ||
) | ||
} | ||
|
||
_chai.Assertion.addMethod('foo', assertIsFoo) | ||
} | ||
// registers our assertion function "isFoo" with Chai | ||
chai.use(isFoo) |
Binary file added
BIN
+154 KB
...les/extending-cypress__chai-assertions/images/custom-assertion-intellisense.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.