decaffeinate: Run post-processing cleanups on error_messages.coffee and 10 other files

This commit is contained in:
KHeo
2020-05-25 17:39:17 +09:00
parent 75a1d3c265
commit 2a3a472fd4
13 changed files with 10460 additions and 9229 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -4380,7 +4380,7 @@ describe('mouse state', () => {
})
.then(($iframe) => {
// cypress does not wrap this as a DOM element (does not wrap in jquery)
return cy.wrap($iframe.first().contents().find('body'))
cy.wrap($iframe.first().contents().find('body'))
})
.within(() => {
cy.get('a#hashchange')

View File

@@ -123,7 +123,7 @@ describe('src/cy/commands/assertions', () => {
}
, 100)
return cy.wrap(obj)
cy.wrap(obj)
}).should('deep.eq', { foo: 'baz' })
})

View File

@@ -1,236 +1,273 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const $ = Cypress.$.bind(Cypress);
const {
_
} = Cypress;
const $ = Cypress.$.bind(Cypress)
const { _ } = Cypress
const $dom = require("../../../../src/dom");
const $dom = require('../../../../src/dom')
describe("src/cy/commands/misc", function() {
before(() => cy
.visit("/fixtures/jquery.html")
.then(function(win) {
return this.body = win.document.body.outerHTML;
}));
describe('src/cy/commands/misc', () => {
before(() => {
cy
.visit('/fixtures/jquery.html')
.then(function (win) {
this.body = win.document.body.outerHTML
})
})
beforeEach(function() {
const doc = cy.state("document");
beforeEach(function () {
const doc = cy.state('document')
return $(doc.body).empty().html(this.body);
});
$(doc.body).empty().html(this.body)
})
context("#end", () => it("nulls out the subject", () => cy.noop({}).end().then(subject => expect(subject).to.be.null)));
context('#end', () => {
it('nulls out the subject', () => {
cy.noop({}).end().then((subject) => {
expect(subject).to.be.null
})
})
})
context("#log", function() {
it("nulls out the subject", () => cy.wrap({}).log("foo").then(subject => expect(subject).to.be.null));
context('#log', () => {
it('nulls out the subject', () => {
cy.wrap({}).log('foo').then((subject) => {
expect(subject).to.be.null
})
})
return describe(".log", function() {
beforeEach(function() {
this.logs = [];
describe('.log', () => {
beforeEach(function () {
this.logs = []
cy.on("log:added", (attrs, log) => {
this.lastLog = log;
return this.logs.push(log);
});
cy.on('log:added', (attrs, log) => {
this.lastLog = log
this.logs.push(log)
})
return null;
});
return null
})
it("logs immediately", function(done) {
cy.on("log:added", (attrs, log) => {
cy.removeAllListeners("log:added");
it('logs immediately', function (done) {
cy.on('log:added', (attrs, log) => {
cy.removeAllListeners('log:added')
expect(log.get("message")).to.eq("foo, {foo: bar}");
expect(log.get("name")).to.eq("log");
expect(log.get("end")).to.be.true;
return done();
});
expect(log.get('message')).to.eq('foo, {foo: bar}')
expect(log.get('name')).to.eq('log')
expect(log.get('end')).to.be.true
return cy.log("foo", {foo: "bar"}).then(() => {
const {
lastLog
} = this;
done()
})
expect(lastLog.get("ended")).to.be.true;
expect(lastLog.get("snapshots").length).to.eq(1);
return expect(lastLog.get("snapshots")[0]).to.be.an("object");
});
});
cy.log('foo', { foo: 'bar' }).then(() => {
const { lastLog } = this
return it("consoleProps", () => cy.log("foobarbaz", [{}]).then(function() {
return expect(this.lastLog.invoke("consoleProps")).to.deep.eq({
Command: "log",
args: [{}],
message: "foobarbaz"
});
}));
});
});
expect(lastLog.get('ended')).to.be.true
expect(lastLog.get('snapshots').length).to.eq(1)
return context("#wrap", function() {
beforeEach(function() {
this.remoteWindow = cy.state("window");
expect(lastLog.get('snapshots')[0]).to.be.an('object')
})
})
return delete this.remoteWindow.$.fn.foo;
});
it('consoleProps', () => {
return cy.log('foobarbaz', [{}]).then(function () {
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
Command: 'log',
args: [{}],
message: 'foobarbaz',
})
})
})
})
})
it("sets the subject to the first argument", () => cy.wrap({}).then(subject => expect(subject).to.deep.eq({})));
context('#wrap', () => {
beforeEach(function () {
this.remoteWindow = cy.state('window')
//# https://github.com/cypress-io/cypress/issues/3241
it("cy.wrap(undefined) should retry", function() {
const stub = cy.stub();
delete this.remoteWindow.$.fn.foo
})
cy.wrap().should(function() {
stub();
return expect(stub).to.be.calledTwice;
});
it('sets the subject to the first argument', () => {
cy.wrap({}).then((subject) => {
expect(subject).to.deep.eq({})
})
})
return cy.wrap(undefined).should(function() {
stub();
return expect(stub.callCount).to.eq(4);
});
});
// https://github.com/cypress-io/cypress/issues/3241
it('cy.wrap(undefined) should retry', () => {
const stub = cy.stub()
it("can wrap jquery objects and continue to chain", function() {
this.remoteWindow.$.fn.foo = "foo";
cy.wrap().should(() => {
stub()
expect(stub).to.be.calledTwice
})
cy.wrap(undefined).should(() => {
stub()
expect(stub.callCount).to.eq(4)
})
})
it('can wrap jquery objects and continue to chain', function () {
this.remoteWindow.$.fn.foo = 'foo'
const append = () => {
return setTimeout(() => {
return $("<li class='appended'>appended</li>").appendTo(cy.$$("#list"));
}
, 50);
};
setTimeout(() => {
$('<li class=\'appended\'>appended</li>').appendTo(cy.$$('#list'))
}, 50)
}
cy.on("command:retry", _.after(2, _.once(append)));
cy.on('command:retry', _.after(2, _.once(append)))
return cy.get("#list").then($ul => cy
cy.get('#list').then(($ul) => {
cy
// ensure that assertions are based on the real subject
// and not the cy subject - therefore foo should be defined
.wrap($ul).should("have.property", "foo")
.wrap($ul).should('have.property', 'foo')
// then re-wrap $ul and ensure that the subject passed
// downstream is the cypress instance
.wrap($ul)
.find("li.appended")
.then($li => //# must use explicit non cy.should
//# else this test will always pass
expect($li.length).to.eq(1)));
});
.find('li.appended')
.then(($li) => {
// must use explicit non cy.should
// else this test will always pass
expect($li.length).to.eq(1)
})
})
})
//# TODO: fix this test in 4.0 when we refactor validating subjects
it.skip("throws a good error when wrapping mixed types: element + string", () => cy.get("button").then(function($btn) {
const btn = $btn.get(0);
// TODO: fix this test in 4.0 when we refactor validating subjects
it.skip('throws a good error when wrapping mixed types: element + string', () => {
cy.get('button').then(($btn) => {
const btn = $btn.get(0)
return cy.wrap([btn, "asdf"]).click();
}));
cy.wrap([btn, 'asdf']).click()
})
})
it("can wrap an array of DOM elements and pass command validation", () => cy.get("button").then(function($btn) {
const btn = $btn.get(0);
it('can wrap an array of DOM elements and pass command validation', () => {
cy.get('button').then(($btn) => {
const btn = $btn.get(0)
cy.wrap([btn]).click().then($btn => expect($dom.isJquery($btn)).to.be.true);
cy.wrap([btn]).click().then(($btn) => {
expect($dom.isJquery($btn)).to.be.true
})
return cy.wrap([btn, btn]).click({ multiple: true }).then($btns => expect($dom.isJquery($btns)).to.be.true);
}));
cy.wrap([btn, btn]).click({ multiple: true }).then(($btns) => {
expect($dom.isJquery($btns)).to.be.true
})
})
})
it("can wrap an array of window without it being altered", () => cy.window().then(win => cy.wrap([win]).then(function(arr) {
expect(arr).to.be.an('array');
return expect(Array.isArray(arr)).to.be.true;
})));
it('can wrap an array of window without it being altered', () => {
cy.window().then((win) => {
cy.wrap([win]).then((arr) => {
expect(arr).to.be.an('array')
expect(Array.isArray(arr)).to.be.true
})
})
})
it("can wrap an array of document without it being altered", () => cy.document().then(doc => cy.wrap([doc]).then(function(arr) {
expect(arr).to.be.an('array');
expect(Array.isArray(arr)).to.be.true;
return expect(arr[0]).to.eq(doc);
})));
it('can wrap an array of document without it being altered', () => {
cy.document().then((doc) => {
cy.wrap([doc]).then((arr) => {
expect(arr).to.be.an('array')
expect(Array.isArray(arr)).to.be.true
expect(arr[0]).to.eq(doc)
})
})
})
//# https://github.com/cypress-io/cypress/issues/2927
it("can properly handle objects with 'jquery' functions as properties", () => //# the root issue here has to do with the fact that window.jquery points
//# to the jquery constructor, but not an actual jquery instance and
//# we need to account for that...
cy.window().then(function(win) {
win.jquery = function() {};
// https://github.com/cypress-io/cypress/issues/2927
it('can properly handle objects with \'jquery\' functions as properties', () => {
// the root issue here has to do with the fact that window.jquery points
// to the jquery constructor, but not an actual jquery instance and
// we need to account for that...
cy.window().then((win) => {
win.jquery = function () {}
return win;
}));
return win
})
})
describe("errors", function() {
it("throws when wrapping an array of windows", function(done) {
cy.on("fail", err => {
expect(err.message).to.include("`cy.scrollTo()` failed because it requires a DOM element.");
expect(err.message).to.include("[<window>]");
expect(err.message).to.include("All 2 subject validations failed on this subject.");
return done();
});
describe('errors', () => {
it('throws when wrapping an array of windows', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.scrollTo()` failed because it requires a DOM element.')
expect(err.message).to.include('[<window>]')
expect(err.message).to.include('All 2 subject validations failed on this subject.')
return cy.window().then(win => cy.wrap([win]).scrollTo("bottom"));
});
done()
})
return it("throws when wrapping an array of documents", function(done) {
cy.on("fail", err => {
expect(err.message).to.include("`cy.screenshot()` failed because it requires a DOM element.");
expect(err.message).to.include("[<document>]");
expect(err.message).to.include("All 3 subject validations failed on this subject.");
return done();
});
cy.window().then((win) => {
cy.wrap([win]).scrollTo('bottom')
})
})
return cy.document().then(doc => cy.wrap([doc]).screenshot());
});
});
it('throws when wrapping an array of documents', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.screenshot()` failed because it requires a DOM element.')
expect(err.message).to.include('[<document>]')
expect(err.message).to.include('All 3 subject validations failed on this subject.')
return describe(".log", function() {
beforeEach(function() {
this.logs = [];
done()
})
cy.on("log:added", (attrs, log) => {
this.lastLog = log;
return this.logs.push(log);
});
cy.document().then((doc) => {
cy.wrap([doc]).screenshot()
})
})
})
return null;
});
describe('.log', () => {
beforeEach(function () {
this.logs = []
it("logs immediately", function(done) {
cy.on("log:added", (attrs, log) => {
cy.removeAllListeners("log:added");
cy.on('log:added', (attrs, log) => {
this.lastLog = log
this.logs.push(log)
})
expect(log.get("message")).to.eq("{}");
expect(log.get("name")).to.eq("wrap");
expect(log.get("end")).not.to.be.ok;
return done();
});
return null
})
return cy.wrap({}).then(() => {
const {
lastLog
} = this;
it('logs immediately', function (done) {
cy.on('log:added', (attrs, log) => {
cy.removeAllListeners('log:added')
expect(lastLog.get("ended")).to.be.true;
expect(lastLog.get("snapshots").length).to.eq(1);
return expect(lastLog.get("snapshots")[0]).to.be.an("object");
});
});
expect(log.get('message')).to.eq('{}')
expect(log.get('name')).to.eq('wrap')
expect(log.get('end')).not.to.be.ok
return it("stringifies DOM elements and sets $el", function() {
const body = $("body");
done()
})
return cy.wrap(body).then(function($el) {
const {
lastLog
} = this;
cy.wrap({}).then(() => {
const { lastLog } = this
//# internally we store the real remote jquery
//# instance instead of the cypress one
expect(lastLog.get("$el")).not.to.eq($el);
expect(lastLog.get('ended')).to.be.true
expect(lastLog.get('snapshots').length).to.eq(1)
expect(lastLog.get('snapshots')[0]).to.be.an('object')
})
})
//# but make sure they are the same DOM object
expect(lastLog.get("$el").get(0)).to.eq($el.get(0));
return expect(lastLog.get("message")).to.eq("<body>");
});
});
});
});
});
it('stringifies DOM elements and sets $el', () => {
const body = $('body')
cy.wrap(body).then(function ($el) {
const { lastLog } = this
// internally we store the real remote jquery
// instance instead of the cypress one
expect(lastLog.get('$el')).not.to.eq($el)
// but make sure they are the same DOM object
expect(lastLog.get('$el').get(0)).to.eq($el.get(0))
expect(lastLog.get('message')).to.eq('<body>')
})
})
})
})
})

View File

@@ -1,88 +1,96 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
describe("src/cy/commands/popups", function() {
context("alert", function() {
beforeEach(function() {
cy.visit("/fixtures/generic.html");
describe('src/cy/commands/popups', () => {
context('alert', () => {
beforeEach(function () {
cy.visit('/fixtures/generic.html')
this.logs = [];
this.logs = []
cy.on("log:added", (attrs, log) => {
if (attrs.name === "alert") {
return this.logs.push(log);
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'alert') {
this.logs.push(log)
}
});
})
return null;
});
return null
})
return it("logs the alert", () => cy.window().then(win => win.alert("fooooo")).then(function() {
expect(this.logs.length).to.eq(1);
expect(this.logs[0].get("name")).to.eq("alert");
expect(this.logs[0].get("message")).to.eq("fooooo");
it('logs the alert', () => {
cy.window().then((win) => {
win.alert('fooooo')
})
.then(function () {
expect(this.logs.length).to.eq(1)
expect(this.logs[0].get('name')).to.eq('alert')
expect(this.logs[0].get('message')).to.eq('fooooo')
const consoleProps = this.logs[0].invoke("consoleProps");
const consoleProps = this.logs[0].invoke('consoleProps')
return expect(consoleProps).to.deep.eq({
Event: "alert",
Alerted: "fooooo"
});
}));
});
expect(consoleProps).to.deep.eq({
Event: 'alert',
Alerted: 'fooooo',
})
})
})
})
return context("confirm", function() {
beforeEach(function() {
cy.visit("/fixtures/generic.html");
context('confirm', () => {
beforeEach(function () {
cy.visit('/fixtures/generic.html')
this.logs = [];
this.logs = []
cy.on("log:added", (attrs, log) => {
if (attrs.name === "confirm") {
return this.logs.push(log);
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'confirm') {
this.logs.push(log)
}
});
})
return null;
});
return null
})
it("logs the confirm", () => cy.window().then(win => win.confirm("Delete hard drive?")).then(function() {
expect(this.logs.length).to.eq(1);
expect(this.logs[0].get("name")).to.eq("confirm");
expect(this.logs[0].get("message")).to.eq("Delete hard drive?");
it('logs the confirm', () => {
cy.window().then((win) => {
win.confirm('Delete hard drive?')
}).then(function () {
expect(this.logs.length).to.eq(1)
expect(this.logs[0].get('name')).to.eq('confirm')
expect(this.logs[0].get('message')).to.eq('Delete hard drive?')
const consoleProps = this.logs[0].invoke("consoleProps");
const consoleProps = this.logs[0].invoke('consoleProps')
return expect(consoleProps).to.deep.eq({
Event: "confirm",
Prompted: "Delete hard drive?",
Confirmed: true
});
}));
expect(consoleProps).to.deep.eq({
Event: 'confirm',
Prompted: 'Delete hard drive?',
Confirmed: true,
})
})
})
return it("can turn on and off confirmation", function() {
cy.on("window:confirm", function(str) {
it('can turn on and off confirmation', () => {
cy.on('window:confirm', (str) => {
switch (str) {
case "foo": return false;
case "bar": return true;
case "baz": return undefined;
case 'foo': return false
case 'bar': return true
case 'baz': return undefined
default: return
}
});
})
return cy.window().then(function(win) {
const confirmedFoo = win.confirm("foo");
expect(confirmedFoo).to.be.false;
cy.window().then((win) => {
const confirmedFoo = win.confirm('foo')
const confirmedBar = win.confirm("bar");
expect(confirmedBar).to.be.true;
expect(confirmedFoo).to.be.false
//# undefined is not strictly false
//# so the confirmation should be true
const confirmedBaz = win.confirm("baz");
return expect(confirmedBaz).to.be.true;
});
});
});
});
const confirmedBar = win.confirm('bar')
expect(confirmedBar).to.be.true
// undefined is not strictly false
// so the confirmation should be true
const confirmedBaz = win.confirm('baz')
expect(confirmedBaz).to.be.true
})
})
})
})

View File

@@ -1,271 +1,278 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const {
_
} = Cypress;
const {
Promise
} = Cypress;
const { _, Promise } = Cypress
describe("src/cy/commands/task", () => context("#task", function() {
beforeEach(function() {
Cypress.config("taskTimeout", 2500);
describe('src/cy/commands/task', () => {
context('#task', () => {
beforeEach(() => {
Cypress.config('taskTimeout', 2500)
cy.stub(Cypress, 'backend').callThrough()
})
return cy.stub(Cypress, "backend").callThrough();
});
it('calls Cypress.backend(\'task\') with the right options', () => {
Cypress.backend.resolves(null)
it("calls Cypress.backend('task') with the right options", function() {
Cypress.backend.resolves(null);
cy.task('foo').then(() => {
expect(Cypress.backend).to.be.calledWith('task', {
task: 'foo',
timeout: 2500,
arg: undefined,
})
})
})
return cy.task("foo").then(() => expect(Cypress.backend).to.be.calledWith("task", {
task: "foo",
timeout: 2500,
arg: undefined
}));
});
it('passes through arg', () => {
Cypress.backend.resolves(null)
it("passes through arg", function() {
Cypress.backend.resolves(null);
cy.task('foo', { foo: 'foo' }).then(() => {
expect(Cypress.backend).to.be.calledWith('task', {
task: 'foo',
timeout: 2500,
arg: {
foo: 'foo',
},
})
})
})
return cy.task("foo", { foo: "foo" }).then(() => expect(Cypress.backend).to.be.calledWith("task", {
task: "foo",
timeout: 2500,
arg: {
foo: "foo"
}
}));
});
it('really works', () => {
cy.task('return:arg', 'works').should('eq', 'works')
})
it("really works", () => cy.task("return:arg", "works").should("eq", "works"));
describe('.log', () => {
beforeEach(function () {
this.logs = []
describe(".log", function() {
beforeEach(function() {
this.logs = [];
cy.on('log:added', (attrs, log) => {
this.lastLog = log
this.logs.push(log)
})
cy.on("log:added", (attrs, log) => {
this.lastLog = log;
return this.logs.push(log);
});
Cypress.backend.resolves(null)
Cypress.backend.resolves(null);
return null
})
return null;
});
it('can turn off logging', () => {
cy.task('foo', null, { log: false }).then(function () {
const logs = _.filter(this.logs, (log) => {
return log.get('name') === 'task'
})
it("can turn off logging", () => cy.task("foo", null, { log: false }).then(function() {
const logs = _.filter(this.logs, log => log.get("name") === "task");
expect(logs.length).to.eq(0)
})
})
return expect(logs.length).to.eq(0);
}));
it('logs immediately before resolving', function () {
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'task') {
expect(log.get('state')).to.eq('pending')
expect(log.get('message')).to.eq('foo')
}
})
return it("logs immediately before resolving", function() {
cy.on("log:added", (attrs, log) => {
if (attrs.name === "task") {
expect(log.get("state")).to.eq("pending");
return expect(log.get("message")).to.eq("foo");
}
});
cy.task('foo').then(() => {
if (!this.lastLog) {
throw new Error('failed to log before resolving')
}
})
})
})
return cy.task("foo").then(() => {
if (!this.lastLog) { throw new Error("failed to log before resolving"); }
});
});
});
describe('timeout', () => {
beforeEach(() => {
Cypress.backend.resolves(null)
})
describe("timeout", function() {
beforeEach(() => Cypress.backend.resolves(null));
it('defaults timeout to Cypress.config(taskTimeout)', () => {
const timeout = cy.spy(Promise.prototype, 'timeout')
it("defaults timeout to Cypress.config(taskTimeout)", function() {
const timeout = cy.spy(Promise.prototype, "timeout");
cy.task('foo').then(() => {
expect(timeout).to.be.calledWith(2500)
})
})
return cy.task("foo").then(() => expect(timeout).to.be.calledWith(2500));
});
it('can override timeout', () => {
const timeout = cy.spy(Promise.prototype, 'timeout')
it("can override timeout", function() {
const timeout = cy.spy(Promise.prototype, "timeout");
cy.task('foo', null, { timeout: 1000 }).then(() => {
expect(timeout).to.be.calledWith(1000)
})
})
return cy.task("foo", null, { timeout: 1000 }).then(() => expect(timeout).to.be.calledWith(1000));
});
it('clears the current timeout and restores after success', () => {
cy.timeout(100)
return it("clears the current timeout and restores after success", function() {
cy.timeout(100);
const clearTimeout = cy.spy(cy, 'clearTimeout')
const clearTimeout = cy.spy(cy, "clearTimeout");
cy.on('task', () => {
expect(clearTimeout).to.be.calledOnce
})
cy.on("task", () => {
return expect(clearTimeout).to.be.calledOnce;
});
cy.task('foo').then(() => {
expect(cy.timeout()).to.eq(100)
})
})
})
return cy.task("foo").then(() => expect(cy.timeout()).to.eq(100));
});
});
describe('errors', () => {
beforeEach(function () {
Cypress.config('defaultCommandTimeout', 50)
return describe("errors", function() {
beforeEach(function() {
Cypress.config("defaultCommandTimeout", 50);
this.logs = []
this.logs = [];
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'task') {
this.lastLog = log
this.logs.push(log)
}
})
cy.on("log:added", (attrs, log) => {
if (attrs.name === "task") {
this.lastLog = log;
return this.logs.push(log);
}
});
return null
})
return null;
});
it('throws when task is absent', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this
it("throws when task is absent", function(done) {
cy.on("fail", err => {
const {
lastLog
} = this;
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
expect(err.message).to.eq('`cy.task()` must be passed a non-empty string as its 1st argument. You passed: ``.')
expect(err.docsUrl).to.eq('https://on.cypress.io/task')
expect(this.logs.length).to.eq(1);
expect(lastLog.get("error")).to.eq(err);
expect(lastLog.get("state")).to.eq("failed");
expect(err.message).to.eq("`cy.task()` must be passed a non-empty string as its 1st argument. You passed: ``.");
expect(err.docsUrl).to.eq("https://on.cypress.io/task");
return done();
});
done()
})
return cy.task();
});
cy.task()
})
it("throws when task isn't a string", function(done) {
cy.on("fail", err => {
const {
lastLog
} = this;
it('throws when task isn\'t a string', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this
expect(this.logs.length).to.eq(1);
expect(lastLog.get("error")).to.eq(err);
expect(lastLog.get("state")).to.eq("failed");
expect(err.message).to.eq("`cy.task()` must be passed a non-empty string as its 1st argument. You passed: `3`.");
expect(err.docsUrl).to.eq("https://on.cypress.io/task");
return done();
});
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
expect(err.message).to.eq('`cy.task()` must be passed a non-empty string as its 1st argument. You passed: `3`.')
expect(err.docsUrl).to.eq('https://on.cypress.io/task')
return cy.task(3);
});
done()
})
it("throws when task is an empty string", function(done) {
cy.on("fail", err => {
const {
lastLog
} = this;
cy.task(3)
})
expect(this.logs.length).to.eq(1);
expect(lastLog.get("error")).to.eq(err);
expect(lastLog.get("state")).to.eq("failed");
expect(err.message).to.eq("`cy.task()` must be passed a non-empty string as its 1st argument. You passed: ``.");
expect(err.docsUrl).to.eq("https://on.cypress.io/task");
return done();
});
it('throws when task is an empty string', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this
return cy.task('');
});
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
expect(err.message).to.eq('`cy.task()` must be passed a non-empty string as its 1st argument. You passed: ``.')
expect(err.docsUrl).to.eq('https://on.cypress.io/task')
it("throws when the task errors", function(done) {
Cypress.backend.rejects(new Error("task failed"));
done()
})
cy.on("fail", err => {
const {
lastLog
} = this;
cy.task('')
})
expect(this.logs.length).to.eq(1);
expect(lastLog.get("error")).to.eq(err);
expect(lastLog.get("state")).to.eq("failed");
it('throws when the task errors', function (done) {
Cypress.backend.rejects(new Error('task failed'))
expect(err.message).to.include("`cy.task('foo')` failed with the following error:");
expect(err.message).to.include("> task failed");
return done();
});
cy.on('fail', (err) => {
const { lastLog } = this
return cy.task("foo");
});
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
it("throws when task is not registered by plugin", function(done) {
cy.on("fail", err => {
const {
lastLog
} = this;
expect(err.message).to.include('`cy.task(\'foo\')` failed with the following error:')
expect(err.message).to.include('> task failed')
expect(this.logs.length).to.eq(1);
expect(lastLog.get("error")).to.eq(err);
expect(lastLog.get("state")).to.eq("failed");
done()
})
expect(err.message).to.eq(`\`cy.task('bar')\` failed with the following error:\n\nThe task 'bar' was not handled in the plugins file. The following tasks are registered: return:arg, wait, create:long:file\n\nFix this in your plugins file here:\n${Cypress.config('pluginsFile')}\n\nhttps://on.cypress.io/api/task`);
return done();
});
cy.task('foo')
})
return cy.task("bar");
});
it('throws when task is not registered by plugin', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this
it("throws after timing out", function(done) {
Cypress.backend.resolves(Promise.delay(250));
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
cy.on("fail", err => {
const {
lastLog
} = this;
expect(err.message).to.eq(`\`cy.task('bar')\` failed with the following error:\n\nThe task 'bar' was not handled in the plugins file. The following tasks are registered: return:arg, wait, create:long:file\n\nFix this in your plugins file here:\n${Cypress.config('pluginsFile')}\n\nhttps://on.cypress.io/api/task`)
expect(this.logs.length).to.eq(1);
expect(lastLog.get("error")).to.eq(err);
expect(lastLog.get("state")).to.eq("failed");
expect(err.message).to.eq("`cy.task('foo')` timed out after waiting `50ms`.");
expect(err.docsUrl).to.eq("https://on.cypress.io/task");
return done();
});
done()
})
return cy.task("foo", null, { timeout: 50 });
});
cy.task('bar')
})
it("logs once on error", function(done) {
Cypress.backend.rejects(new Error("task failed"));
it('throws after timing out', function (done) {
Cypress.backend.resolves(Promise.delay(250))
cy.on("fail", err => {
const {
lastLog
} = this;
cy.on('fail', (err) => {
const { lastLog } = this
expect(this.logs.length).to.eq(1);
expect(lastLog.get("error")).to.eq(err);
expect(lastLog.get("state")).to.eq("failed");
return done();
});
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
expect(err.message).to.eq('`cy.task(\'foo\')` timed out after waiting `50ms`.')
expect(err.docsUrl).to.eq('https://on.cypress.io/task')
return cy.task("foo");
});
done()
})
it("can timeout from the backend's response", function(done) {
const err = new Error("timeout");
err.timedOut = true;
cy.task('foo', null, { timeout: 50 })
})
Cypress.backend.rejects(err);
it('logs once on error', function (done) {
Cypress.backend.rejects(new Error('task failed'))
cy.on("fail", function(err) {
expect(err.message).to.include("`cy.task('wait')` timed out after waiting `100ms`.");
expect(err.docsUrl).to.eq("https://on.cypress.io/task");
return done();
});
cy.on('fail', (err) => {
const { lastLog } = this
return cy.task("wait", null, { timeout: 100 });
});
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('state')).to.eq('failed')
return it("can really time out", function(done) {
cy.on("fail", function(err) {
expect(err.message).to.include("`cy.task('wait')` timed out after waiting `100ms`.");
expect(err.docsUrl).to.eq("https://on.cypress.io/task");
return done();
});
done()
})
return cy.task("wait", null, { timeout: 100 });
});
});
}));
cy.task('foo')
})
it('can timeout from the backend\'s response', (done) => {
const err = new Error('timeout')
err.timedOut = true
Cypress.backend.rejects(err)
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.task(\'wait\')` timed out after waiting `100ms`.')
expect(err.docsUrl).to.eq('https://on.cypress.io/task')
done()
})
cy.task('wait', null, { timeout: 100 })
})
it('can really time out', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.task(\'wait\')` timed out after waiting `100ms`.')
expect(err.docsUrl).to.eq('https://on.cypress.io/task')
done()
})
cy.task('wait', null, { timeout: 100 })
})
})
})
})

View File

@@ -1,337 +1,368 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const $ = Cypress.$.bind(Cypress);
const {
_
} = Cypress;
const {
dom
} = Cypress;
const $ = Cypress.$.bind(Cypress)
const { _, dom } = Cypress
const helpers = require("../../support/helpers");
const helpers = require('../../support/helpers')
describe("src/cy/commands/traversals", function() {
beforeEach(() => cy.visit("/fixtures/dom.html"));
describe('src/cy/commands/traversals', () => {
beforeEach(() => {
cy.visit('/fixtures/dom.html')
})
const fns = [
{find: "*"},
{filter: ":first"},
{filter(i) { return i === 0; }},
{not: "div"},
{not(i, e) { return e.tagName === 'div'; }},
{eq: 0},
{closest: "body"},
"children", "first", "last", "next", "nextAll", "nextUntil", "parent", "parents", "parentsUntil", "prev", "prevAll", "prevUntil", "siblings"
];
_.each(fns, function(fn) {
//# normalize string vs object
let arg, name;
{ find: '*' },
{ filter: ':first' },
{ filter: (i) => i === 0 },
{ not: 'div' },
{ not: (i, e) => e.tagName === 'div' },
{ eq: 0 },
{ closest: 'body' },
'children', 'first', 'last', 'next', 'nextAll', 'nextUntil', 'parent', 'parents', 'parentsUntil', 'prev', 'prevAll', 'prevUntil', 'siblings',
]
_.each(fns, (fn) => {
// normalize string vs object
let arg
let name
if (_.isObject(fn)) {
name = _.keys(fn)[0];
arg = fn[name];
name = _.keys(fn)[0]
arg = fn[name]
} else {
name = fn;
name = fn
}
return context(`#${name}`, function() {
it("proxies through to jquery and returns new subject", function() {
const el = cy.$$("#list")[name](arg);
return cy.get("#list")[name](arg).then($el => expect($el).to.match(el));
});
context(`#${name}`, () => {
it('proxies through to jquery and returns new subject', () => {
const el = cy.$$('#list')[name](arg)
describe("errors", function() {
beforeEach(() => Cypress.config("defaultCommandTimeout", 100));
cy.get('#list')[name](arg).then(($el) => {
expect($el).to.match(el)
})
})
it("throws when options.length isnt a number", function(done) {
cy.on("fail", function(err) {
expect(err.message).to.include("You must provide a valid number to a `length` assertion. You passed: `asdf`");
return done();
});
describe('errors', () => {
beforeEach(() => {
Cypress.config('defaultCommandTimeout', 100)
})
return cy.get("#list")[name](arg).should("have.length", "asdf");
});
it('throws when options.length isnt a number', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('You must provide a valid number to a `length` assertion. You passed: `asdf`')
it("throws on too many elements after timing out waiting for length", function(done) {
const el = cy.$$("#list")[name](arg);
done()
})
const node = dom.stringify(cy.$$("#list"), "short");
cy.get('#list')[name](arg).should('have.length', 'asdf')
})
cy.on("fail", function(err) {
expect(err.message).to.include(`Too many elements found. Found '${el.length}', expected '${el.length - 1}'.`);
return done();
});
it('throws on too many elements after timing out waiting for length', (done) => {
const el = cy.$$('#list')[name](arg)
return cy.get("#list")[name](arg).should("have.length", el.length - 1);
});
dom.stringify(cy.$$('#list'), 'short')
it("throws on too few elements after timing out waiting for length", function(done) {
const el = cy.$$("#list")[name](arg);
cy.on('fail', (err) => {
expect(err.message).to.include(`Too many elements found. Found '${el.length}', expected '${el.length - 1}'.`)
const node = dom.stringify(cy.$$("#list"), "short");
done()
})
cy.on("fail", function(err) {
expect(err.message).to.include(`Not enough elements found. Found '${el.length}', expected '${el.length + 1}'.`);
return done();
});
cy.get('#list')[name](arg).should('have.length', el.length - 1)
})
return cy.get("#list")[name](arg).should("have.length", el.length + 1);
});
it('throws on too few elements after timing out waiting for length', (done) => {
const el = cy.$$('#list')[name](arg)
it("without a dom element", function(done) {
cy.on("fail", () => done());
return cy.noop({})[name](arg);
});
dom.stringify(cy.$$('#list'), 'short')
it("throws when subject is not in the document", function(done) {
cy.on("command:end", () => {
return cy.$$("#list").remove();
});
cy.on('fail', (err) => {
expect(err.message).to.include(`Not enough elements found. Found '${el.length}', expected '${el.length + 1}'.`)
cy.on("fail", function(err) {
expect(err.message).to.include(`\`cy.${name}()\` failed because this element`);
return done();
});
done()
})
return cy.get("#list")[name](arg);
});
cy.get('#list')[name](arg).should('have.length', el.length + 1)
})
return it("returns no elements", function(done) {
it('without a dom element', (done) => {
cy.on('fail', () => {
done()
})
cy.noop({})[name](arg)
})
it('throws when subject is not in the document', (done) => {
cy.on('command:end', () => {
cy.$$('#list').remove()
})
cy.on('fail', (err) => {
expect(err.message).to.include(`\`cy.${name}()\` failed because this element`)
done()
})
cy.get('#list')[name](arg)
})
it('returns no elements', (done) => {
const errIncludes = (el, node) => {
node = dom.stringify(cy.$$(node), "short");
node = dom.stringify(cy.$$(node), 'short')
return cy.on("fail", function(err) {
expect(err.message).to.include(`Expected to find element: \`${el}\`, but never found it. Queried from element: ${node}`);
return done();
});
};
cy.on('fail', (err) => {
expect(err.message).to.include(`Expected to find element: \`${el}\`, but never found it. Queried from element: ${node}`)
done()
})
}
switch (name) {
case "not":
errIncludes(":checkbox", ":checkbox");
return cy.get(":checkbox").not(":checkbox");
case 'not':
errIncludes(':checkbox', ':checkbox')
//# these cannot error
case "first": case "last": case "parentsUntil": return done();
cy.get(':checkbox').not(':checkbox')
break
// these cannot error
case 'first':
case 'last':
case 'parentsUntil':
done()
break
default:
errIncludes(".no-class-like-this-exists", "div:first");
return cy.get("div:first")[name](".no-class-like-this-exists");
errIncludes('.no-class-like-this-exists', 'div:first')
cy.get('div:first')[name]('.no-class-like-this-exists')
break
}
});
});
})
})
return describe(".log", function() {
beforeEach(function() {
cy.on("log:added", (attrs, log) => {
return this.lastLog = log;
});
describe('.log', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
this.lastLog = log
})
return null;
});
return null
})
it("logs immediately before resolving", function(done) {
cy.on("log:added", function(attrs, log) {
if (log.get("name") === name) {
expect(log.pick("state")).to.deep.eq({
state: "pending"
});
return done();
it('logs immediately before resolving', (done) => {
cy.on('log:added', (attrs, log) => {
if (log.get('name') === name) {
expect(log.pick('state')).to.deep.eq({
state: 'pending',
})
done()
}
});
})
return cy.get("#list")[name](arg);
});
cy.get('#list')[name](arg)
})
it("snapshots after finding element", () => cy.get("#list")[name](arg).then(function() {
const {
lastLog
} = this;
it('snapshots after finding element', () => {
cy.get('#list')[name](arg).then(function () {
const { lastLog } = this
expect(lastLog.get("snapshots").length).to.eq(1);
return expect(lastLog.get("snapshots")[0]).to.be.an("object");
}));
expect(lastLog.get('snapshots').length).to.eq(1)
expect(lastLog.get('snapshots')[0]).to.be.an('object')
})
})
it("has the $el", () => cy.get("#list")[name](arg).then(function($el) {
const {
lastLog
} = this;
it('has the $el', () => {
cy.get('#list')[name](arg).then(function ($el) {
const { lastLog } = this
return expect(lastLog.get("$el").get(0)).to.eq($el.get(0));
}));
expect(lastLog.get('$el').get(0)).to.eq($el.get(0))
})
})
it("has a custom message", () => cy.get("#list")[name](arg).then(function() {
let message;
if (_.isUndefined(arg) || _.isFunction(arg)) {
message = "";
} else {
message = arg.toString();
}
it('has a custom message', () => {
cy.get('#list')[name](arg).then(function () {
let message
const {
lastLog
} = this;
if (_.isUndefined(arg) || _.isFunction(arg)) {
message = ''
} else {
message = arg.toString()
}
return expect(lastLog.get("message")).to.eq(message);
}));
const { lastLog } = this
it("#consoleProps", () => cy.get("#list")[name](arg).then(function($el) {
const obj = {Command: name};
if (_.isFunction(arg)) {
obj.Selector = "";
} else {
obj.Selector = [].concat(arg).join(", ");
}
expect(lastLog.get('message')).to.eq(message)
})
})
const yielded = Cypress.dom.getElements($el);
it('#consoleProps', () => {
cy.get('#list')[name](arg).then(function ($el) {
const obj = { Command: name }
_.extend(obj, {
"Applied To": helpers.getFirstSubjectByName("get").get(0),
Yielded: yielded,
Elements: $el.length
});
if (_.isFunction(arg)) {
obj.Selector = ''
} else {
obj.Selector = [].concat(arg).join(', ')
}
return expect(this.lastLog.invoke("consoleProps")).to.deep.eq(obj);
}));
const yielded = Cypress.dom.getElements($el)
return it("can be turned off", () => cy.get("#list")[name](arg, {log: false}).then(function() {
const {
lastLog
} = this;
_.extend(obj, {
'Applied To': helpers.getFirstSubjectByName('get').get(0),
Yielded: yielded,
Elements: $el.length,
})
return expect(lastLog.get("name")).to.eq("get");
}));
});
});
});
expect(this.lastLog.invoke('consoleProps')).to.deep.eq(obj)
})
})
it("eventually resolves", function() {
cy.on("command:retry", _.after(2, () => cy.$$("button:first").text("foo").addClass("bar"))
);
it('can be turned off', () => {
cy.get('#list')[name](arg, { log: false }).then(function () {
const { lastLog } = this
return cy.root().find("button:first").should("have.text", "foo").and("have.class", "bar");
});
it("retries until it finds", function() {
const li = cy.$$("#list li:last");
const span = $("<span>foo</span>");
const retry = _.after(3, () => li.append(span));
cy.on("command:retry", retry);
return cy.get("#list li:last").find("span").then($span => expect($span.get(0)).to.eq(span.get(0)));
});
it("retries until length equals n", function() {
let buttons = cy.$$("button");
const length = buttons.length - 2;
cy.on("command:retry", _.after(2, () => {
buttons.last().remove();
return buttons = cy.$$("button");
expect(lastLog.get('name')).to.eq('get')
})
})
})
})
);
})
//# should resolving after removing 2 buttons
return cy.root().find("button").should("have.length", length).then($buttons => expect($buttons.length).to.eq(length));
});
it('eventually resolves', () => {
cy.on('command:retry', _.after(2, () => {
cy.$$('button:first').text('foo').addClass('bar')
}))
it("should('not.exist')", function() {
cy.on("command:retry", _.after(3, () => {
return cy.$$("#nested-div").find("span").remove();
cy.root().find('button:first').should('have.text', 'foo').and('have.class', 'bar')
})
it('retries until it finds', () => {
const li = cy.$$('#list li:last')
const span = $('<span>foo</span>')
const retry = _.after(3, () => {
li.append(span)
})
);
return cy.get("#nested-div").find("span").should("not.exist");
});
cy.on('command:retry', retry)
it("should('exist')", function() {
cy.on("command:retry", _.after(3, () => {
return cy.$$("#nested-div").append($("<strong />"));
cy.get('#list li:last').find('span').then(($span) => {
expect($span.get(0)).to.eq(span.get(0))
})
);
})
return cy.get("#nested-div").find("strong");
});
it('retries until length equals n', () => {
let buttons = cy.$$('button')
//# https://github.com/cypress-io/cypress/issues/38
it("works with checkboxes", function() {
cy.on("command:retry", _.after(2, () => {
const c = cy.$$("[name=colors]").slice(0, 2);
return c.prop("checked", true);
const length = buttons.length - 2
cy.on('command:retry', _.after(2, () => {
buttons.last().remove()
buttons = cy.$$('button')
}))
// should resolving after removing 2 buttons
cy.root().find('button').should('have.length', length).then(($buttons) => {
expect($buttons.length).to.eq(length)
})
);
})
return cy.get("#by-name").find(":checked").should("have.length", 2);
});
it('should(\'not.exist\')', () => {
cy.on('command:retry', _.after(3, () => {
cy.$$('#nested-div').find('span').remove()
}))
it("does not log using first w/options", function() {
const logs = [];
cy.get('#nested-div').find('span').should('not.exist')
})
cy.on("log:added", function(attrs, log) {
if (attrs.name !== "assert") {
return logs.push(log);
it('should(\'exist\')', () => {
cy.on('command:retry', _.after(3, () => {
cy.$$('#nested-div').append($('<strong />'))
}))
cy.get('#nested-div').find('strong')
})
// https://github.com/cypress-io/cypress/issues/38
it('works with checkboxes', () => {
cy.on('command:retry', _.after(2, () => {
const c = cy.$$('[name=colors]').slice(0, 2)
c.prop('checked', true)
}))
cy.get('#by-name').find(':checked').should('have.length', 2)
})
it('does not log using first w/options', () => {
const logs = []
cy.on('log:added', (attrs, log) => {
if (attrs.name !== 'assert') {
logs.push(log)
}
});
})
return cy.get("button").first({log: false}).then(function($button) {
expect($button.length).to.eq(1);
return expect(logs.length).to.eq(1);
});
});
cy.get('button').first({ log: false }).then(($button) => {
expect($button.length).to.eq(1)
expect(logs.length).to.eq(1)
})
})
return describe("errors", function() {
beforeEach(function() {
Cypress.config("defaultCommandTimeout", 100);
describe('errors', () => {
beforeEach(function () {
Cypress.config('defaultCommandTimeout', 100)
this.logs = [];
this.logs = []
cy.on("log:added", (attrs, log) => {
return this.logs.push(log);
});
cy.on('log:added', (attrs, log) => {
this.logs.push(log)
})
return null;
});
return null
})
it("errors after timing out not finding element", function(done) {
cy.on("fail", function(err) {
expect(err.message).to.include("Expected to find element: `span`, but never found it. Queried from element: <li.item>");
return done();
});
it('errors after timing out not finding element', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Expected to find element: `span`, but never found it. Queried from element: <li.item>')
return cy.get("#list li:last").find("span");
});
done()
})
it("throws once when incorrect sizzle selector", function(done) {
cy.on("fail", err => {
expect(this.logs.length).to.eq(2);
return done();
});
cy.get('#list li:last').find('span')
})
return cy.get("div:first").find(".spinner'");
});
it('throws once when incorrect sizzle selector', function (done) {
cy.on('fail', (err) => {
expect(this.logs.length).to.eq(2)
return it("logs out $el when existing $el is found even on failure", function(done) {
const button = cy.$$("#button").hide();
done()
})
cy.on("fail", err => {
const log = this.logs[1];
cy.get('div:first').find('.spinner\'')
})
expect(log.get("state")).to.eq("failed");
expect(err.message).to.include(log.get("error").message);
expect(log.get("$el").get(0)).to.eq(button.get(0));
it('logs out $el when existing $el is found even on failure', function (done) {
const button = cy.$$('#button').hide()
const consoleProps = log.invoke("consoleProps");
expect(consoleProps.Yielded).to.eq(button.get(0));
expect(consoleProps.Elements).to.eq(button.length);
return done();
});
cy.on('fail', (err) => {
const log = this.logs[1]
return cy.get("#dom").find("#button").should("be.visible");
});
});
});
expect(log.get('state')).to.eq('failed')
expect(err.message).to.include(log.get('error').message)
expect(log.get('$el').get(0)).to.eq(button.get(0))
const consoleProps = log.invoke('consoleProps')
expect(consoleProps.Yielded).to.eq(button.get(0))
expect(consoleProps.Elements).to.eq(button.length)
done()
})
cy.get('#dom').find('#button').should('be.visible')
})
})
})

File diff suppressed because it is too large Load Diff