Skip to content

Commit

Permalink
chore: introduce debugAssert (#2160)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed May 8, 2020
1 parent 55a067f commit 8e59031
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/frames.ts
Expand Up @@ -21,7 +21,7 @@ import { ConsoleMessage } from './console';
import * as dom from './dom';
import { TimeoutError, NotConnectedError } from './errors';
import { Events } from './events';
import { assert, helper, RegisteredListener, assertMaxArguments } from './helper';
import { assert, helper, RegisteredListener, assertMaxArguments, debugAssert } from './helper';
import * as js from './javascript';
import * as network from './network';
import { Page } from './page';
Expand Down Expand Up @@ -149,7 +149,7 @@ export class FrameManager {
this.removeChildFramesRecursively(frame);
frame._url = url;
frame._name = name;
assert(!frame._pendingDocumentId || frame._pendingDocumentId === documentId);
debugAssert(!frame._pendingDocumentId || frame._pendingDocumentId === documentId);
frame._lastDocumentId = documentId;
frame._pendingDocumentId = '';
for (const task of frame._frameTasks)
Expand Down
15 changes: 15 additions & 0 deletions src/helper.ts
Expand Up @@ -337,6 +337,21 @@ export function assert(value: any, message?: string): asserts value {
throw new Error(message);
}

let _isUnderTest = false;

export function setUnderTest() {
_isUnderTest = true;
}

export function isUnderTest(): boolean {
return _isUnderTest;
}

export function debugAssert(value: any, message?: string): asserts value {
if (_isUnderTest && !value)
throw new Error(message);
}

export function assertMaxArguments(count: number, max: number): asserts count {
assert(count <= max, 'Too many arguments. If you need to pass more than 1 argument to the function wrap them in an object.');
}
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Expand Up @@ -16,7 +16,6 @@
*/

const fs = require('fs');
const readline = require('readline');
const TestRunner = require('../utils/testrunner/');
const {Environment} = require('../utils/testrunner/Test');

Expand Down Expand Up @@ -74,6 +73,8 @@ function collect(browserNames) {
// TODO: this should be a preinstalled playwright by default.
const playwrightPath = config.playwrightPath;
const playwright = require(playwrightPath);
const { setUnderTest } = require(require('path').join(playwrightPath, 'lib/helper.js'));
setUnderTest();

const playwrightEnvironment = new Environment('Playwright');
playwrightEnvironment.beforeAll(async state => {
Expand Down

0 comments on commit 8e59031

Please sign in to comment.