chore: stop using console.assert everywhere (#2646)

Since Node 10, `console.assert` no longer throws an AssertionError.
(This is generally good since it aligns Node.js with Browsers.)

This patch migrates all usages of `console.assert` in our codebase.
- All the `lib/` and testing code is migrated onto a handmade `assert`
function. This is to make Puppeteer transpilation / bundling easier.
- All the tooling is switched to use Node's `assert` module.

Fixes #2547.
This commit is contained in:
Andrey Lushnikov
2018-05-31 16:53:51 -07:00
committed by GitHub
parent 35e3f12afa
commit 0b94fa70eb
19 changed files with 105 additions and 83 deletions

View File

@@ -16,7 +16,7 @@
const fs = require('fs');
const EventEmitter = require('events');
const {helper} = require('./helper');
const {helper, assert} = require('./helper');
const {ExecutionContext, JSHandle} = require('./ExecutionContext');
const ElementHandle = require('./ElementHandle');
@@ -116,7 +116,7 @@ class FrameManager extends EventEmitter {
_onFrameAttached(frameId, parentFrameId) {
if (this._frames.has(frameId))
return;
console.assert(parentFrameId);
assert(parentFrameId);
const parentFrame = this._frames.get(parentFrameId);
const frame = new Frame(this._client, this._page, parentFrame, frameId);
this._frames.set(frame._id, frame);
@@ -129,7 +129,7 @@ class FrameManager extends EventEmitter {
_onFrameNavigated(framePayload) {
const isMainFrame = !framePayload.parentId;
let frame = isMainFrame ? this._mainFrame : this._frames.get(framePayload.id);
console.assert(isMainFrame || frame, 'We either navigate top level or have old version of the navigated frame');
assert(isMainFrame || frame, 'We either navigate top level or have old version of the navigated frame');
// Detach all child frames first.
if (frame) {
@@ -220,7 +220,7 @@ class FrameManager extends EventEmitter {
*/
createJSHandle(contextId, remoteObject) {
const context = this._contextIdToContext.get(contextId);
console.assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId);
assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId);
if (remoteObject.subtype === 'node')
return new ElementHandle(context, this._client, remoteObject, this._page, this);
return new JSHandle(context, this._client, remoteObject);
@@ -588,7 +588,7 @@ class Frame {
*/
async click(selector, options = {}) {
const handle = await this.$(selector);
console.assert(handle, 'No node found for selector: ' + selector);
assert(handle, 'No node found for selector: ' + selector);
await handle.click(options);
await handle.dispose();
}
@@ -598,7 +598,7 @@ class Frame {
*/
async focus(selector) {
const handle = await this.$(selector);
console.assert(handle, 'No node found for selector: ' + selector);
assert(handle, 'No node found for selector: ' + selector);
await handle.focus();
await handle.dispose();
}
@@ -608,7 +608,7 @@ class Frame {
*/
async hover(selector) {
const handle = await this.$(selector);
console.assert(handle, 'No node found for selector: ' + selector);
assert(handle, 'No node found for selector: ' + selector);
await handle.hover();
await handle.dispose();
}
@@ -620,7 +620,7 @@ class Frame {
*/
select(selector, ...values){
for (const value of values)
console.assert(helper.isString(value), 'Values must be strings. Found value "' + value + '" of type "' + (typeof value) + '"');
assert(helper.isString(value), 'Values must be strings. Found value "' + value + '" of type "' + (typeof value) + '"');
return this.$eval(selector, (element, values) => {
if (element.nodeName.toLowerCase() !== 'select')
throw new Error('Element is not a <select> element.');
@@ -643,7 +643,7 @@ class Frame {
*/
async tap(selector) {
const handle = await this.$(selector);
console.assert(handle, 'No node found for selector: ' + selector);
assert(handle, 'No node found for selector: ' + selector);
await handle.tap();
await handle.dispose();
}
@@ -655,7 +655,7 @@ class Frame {
*/
async type(selector, text, options) {
const handle = await this.$(selector);
console.assert(handle, 'No node found for selector: ' + selector);
assert(handle, 'No node found for selector: ' + selector);
await handle.type(text, options);
await handle.dispose();
}
@@ -818,9 +818,9 @@ class WaitTask {
*/
constructor(frame, predicateBody, title, polling, timeout, ...args) {
if (helper.isString(polling))
console.assert(polling === 'raf' || polling === 'mutation', 'Unknown polling option: ' + polling);
assert(polling === 'raf' || polling === 'mutation', 'Unknown polling option: ' + polling);
else if (helper.isNumber(polling))
console.assert(polling > 0, 'Cannot poll with non-positive interval: ' + polling);
assert(polling > 0, 'Cannot poll with non-positive interval: ' + polling);
else
throw new Error('Unknown polling options: ' + polling);