Skip to content

Instantly share code, notes, and snippets.

View NicholasBoll's full-sized avatar

Nicholas Boll NicholasBoll

  • Workday
  • Boulder, CO
View GitHub Profile
@NicholasBoll
NicholasBoll / async_testing.js
Created August 18, 2015 18:57
Async AngularJS testing
describe('getFormattedCustomerInfo-async', function(){
var customerService;
var customerFormattingService;
var $q;
var $scope;
beforeEach(function(){
module('customer');
inject( function($injector){
customerService = $injector.get('customer-service');
customerFormattingService = $injector.get('customer-formatting-service-async');
@NicholasBoll
NicholasBoll / myCustomCommand.ts
Last active November 18, 2020 12:28
Adding Custom Cypress command
declare namespace Cypress {
interface Chainable<Subject> {
myCustomCommand(username: string, password: string): Cypress.Chainable<Response>
}
}
Cypress.Commands.add('myCustomCommand', (username: string, password: string) => {
Cypress.log({
name: 'loginByForm',
@NicholasBoll
NicholasBoll / promiseExample.ts
Last active February 5, 2018 14:27
Example of a promise chain
interface Todo {
id: string
name: string
done: boolean
}
const createTodo = (name: string) => new Promise<Todo>((resolve) => {
setTimeout(
resolve,
100,
@NicholasBoll
NicholasBoll / todo_spec.ts
Last active February 15, 2018 07:18
Cypress command composition
export const createTodo = (name: string) => {
cy.get('.new-todo').type(`${name}{enter}`)
return cy
.get('.todo-list')
.contains('li', name.trim())
.first()
}
export const updateTodo = (name: string) => ($todo: JQuery) => {
@NicholasBoll
NicholasBoll / todo_log.ts
Last active March 18, 2018 10:40
Cypress helpers with logging
export const createTodo = (todoDescription: string) => {
// we use `any` here until this PR is accepted: https://github.com/cypress-io/cypress/pull/1289
const log: any = Cypress.log({
name: 'createTodo', // This should be pretty short. It shows up as all caps
message: todoDescription, // Outputs to the right of the "name". This can be longer and will wrap
consoleProps() { // Additional debug info. For example, cy.visit will show cookies and redirects
return {
'Inserted Todo': todoDescription,
}
@NicholasBoll
NicholasBoll / chain_test.ts
Created March 10, 2018 06:49
Just the test that includes the chain example
it('should allow chaining of all the helpers', () => {
createTodo('Learn Cypress Command API')
.then(updateTodo('Learn Cypress composition'))
.then(markAsDone)
.then(deleteTodo)
cy.get('.todoapp')
.should('not.contain', 'Learn Cypress composition')
})
@NicholasBoll
NicholasBoll / simple_jest_spec.js
Created March 18, 2018 08:37
Using Jest's expect directly in Cypress
import expect from 'expect'
describe('page', () => {
it('should allow usage of expect directly from jest', () => {
cy.wrap(5).should(subject => {
expect(subject).toEqual(5)
})
cy.wrap({ foo: 'bar' }).should(subject => {
expect(subject).toHaveProperty('foo', 'bar')
@NicholasBoll
NicholasBoll / jest_cypress_should_shorthand_spec.js
Created March 18, 2018 08:41
Using Jest's expect using Cypress's should shorthand
import expect from 'expect'
// Put this in cypress/support/commands.js to use everywhere
Cypress.Commands.overwrite('should', (originalFn, subject, expectation, ...args) => {
// See if the expectation is a string and if it is a member of Jest's expect
if (typeof expectation === 'string' && expect(subject)[expectation]) {
return originalFn(subject, (s) => expect(s)[expectation](...args))
}
return originalFn(subject, expectation, ...args)
cy.wrap('foo').as('bar')
cy.get('@bar').then(subject => {
subject // JQuery<HTMLElement>
console.log(subject) // logs 'foo'
})
cy.get<string>('@bar').then(subject => {
subject // string
console.log(subject) // logs 'foo'
const getFoo = cy.wrap('foo').createAlias()
const getBar = cy.wrap('bar').createAlias()
const getOne = cy.wrap(1).createAlias()
// get one alias
getFoo().then(subject => {
subject // string
console.log(subject) // logs 'foo'
})