Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into shellcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jan 24, 2024
2 parents 2c2ed9d + 5ea26d5 commit fdcedf9
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 35 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},

"rules": {
"comma-dangle": ["error", "only-multiline"],
"eol-last": "error",
"no-empty": "off",
"no-console": "off",
Expand Down
4 changes: 1 addition & 3 deletions bin/build-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ function doBrowserify(pkgName, filepath, opts, exclude) {
path.dirname(filepath) + '/' + path.basename(filepath));
});
} else {
bundler = browserify(addPath(pkgName, filepath), opts)
.transform('es3ify')
.plugin('bundle-collapser/plugin');
bundler = browserify(addPath(pkgName, filepath), opts);
}

if (exclude) {
Expand Down
60 changes: 47 additions & 13 deletions bin/test-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ const playwright = require('playwright');
const { identity, pickBy } = require('lodash');

var MochaSpecReporter = require('mocha').reporters.Spec;
const createMochaStatsCollector = require('mocha/lib/stats-collector');

var devserver = require('./dev-server.js');

// BAIL=0 to disable bailing
var bail = process.env.BAIL !== '0';

// Track if the browser has closed at the request of this script, or due to an external event.
let closeRequested;

// Playwright BrowserType whitelist.
// See: https://playwright.dev/docs/api/class-playwright
const SUPPORTED_BROWSERS = [ 'chromium', 'firefox', 'webkit' ];
Expand Down Expand Up @@ -56,33 +60,51 @@ const qs = {
testUrl += '?';
testUrl += new URLSearchParams(pickBy(qs, identity));

class ArrayMap extends Map {
get(key) {
if (!this.has(key)) {
this.set(key, []);
}
return super.get(key);
}
}

class RemoteRunner {
constructor(browser) {
this.browser = browser;
this.handlers = {};
this.handlers = new ArrayMap();
this.onceHandlers = new ArrayMap();
this.handleEvent = this.handleEvent.bind(this);
createMochaStatsCollector(this);
}

once(name, handler) {
this.onceHandlers.get(name).push(handler);
}

on(name, handler) {
var handlers = this.handlers;
this.handlers.get(name).push(handler);
}

if (!handlers[name]) {
handlers[name] = [];
}
handlers[name].push(handler);
triggerHandlers(eventName, handlerArgs) {
const triggerHandler = handler => handler.apply(null, handlerArgs);

this.onceHandlers.get(eventName).forEach(triggerHandler);
this.onceHandlers.delete(eventName);

this.handlers.get(eventName).forEach(triggerHandler);
}

async handleEvent(event) {
try {
var additionalProps = ['pass', 'fail', 'pending'].indexOf(event.name) === -1 ? {} : {
slow: event.obj.slow ? function () { return event.obj.slow; } : function () { return 60; },
fullTitle: event.obj.fullTitle ? function () { return event.obj.fullTitle; } : undefined
fullTitle: event.obj.fullTitle ? function () { return event.obj.fullTitle; } : undefined,
titlePath: event.obj.titlePath ? function () { return event.obj.titlePath; } : undefined,
};
var obj = Object.assign({}, event.obj, additionalProps);

this.handlers[event.name].forEach(function (handler) {
handler(obj, event.err);
});
this.triggerHandlers(event.name, [ obj, event.err ]);

switch (event.name) {
case 'fail': this.handleFailed(); break;
Expand All @@ -91,22 +113,22 @@ class RemoteRunner {
} catch (e) {
console.error('Tests failed:', e);

closeRequested = true;
await this.browser.close();
process.exit(3);
}
}

async handleEnd(failed) {
closeRequested = true;
await this.browser.close();
process.exit(!process.env.PERF && failed ? 1 : 0);
}

handleFailed() {
if (bail) {
try {
this.handlers['end'].forEach(function (handler) {
handler();
});
this.triggerHandlers('end');
} catch (e) {
console.log('An error occurred while bailing:', e);
} finally {
Expand Down Expand Up @@ -147,8 +169,20 @@ async function startTest() {
headless: true,
};
const browser = await playwright[browserName].launch(options);

const page = await browser.newPage();

// Playwright's Browser.on('close') event handler would be the more obvious
// choice here, but it does not seem to be triggered if the browser is closed
// by an external event (e.g. process is killed, user closes non-headless
// browser window).
page.on('close', () => {
if (!closeRequested) {
console.log('!!! Browser closed by external event.');
process.exit(1);
}
});

const runner = new RemoteRunner(browser);
new MochaSpecReporter(runner);
new BenchmarkConsoleReporter(runner);
Expand Down
4 changes: 4 additions & 0 deletions bin/test-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ fi
if [ "$PERF" ]; then
node tests/performance/index.js
elif [ ! "$COVERAGE" ]; then
# --exit required to workaround #8839
./node_modules/.bin/mocha \
--exit \
"$BAIL_OPT" \
--timeout "$TIMEOUT" \
--require=./tests/integration/node.setup.js \
--reporter="$REPORTER" \
--grep="$GREP" \
"$TESTS_PATH"
else
# --exit required to workaround #8839
./node_modules/.bin/istanbul cover \
--no-default-excludes -x 'tests/**' -x 'node_modules/**' \
./node_modules/mocha/bin/_mocha -- \
--exit \
"$BAIL_OPT" \
--timeout "$TIMEOUT" \
--require=./tests/integration/node.setup.js \
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build-modules": "node bin/build-modules.js",
"test-unit": "mocha tests/unit",
"test-node": "./bin/test-node.sh",
"test-component": "mocha tests/component",
"test-component": "mocha --exit tests/component # --exit for #8839",
"test-fuzzy": "TYPE=fuzzy npm run test",
"test-browser": "npm run build-test && node ./bin/test-browser.js",
"test-memleak": "mocha -gc tests/memleak",
Expand Down Expand Up @@ -41,7 +41,7 @@
"abort-controller": "3.0.0",
"clone-buffer": "1.0.0",
"double-ended-queue": "2.1.0-0",
"fetch-cookie": "0.11.0",
"fetch-cookie": "2.1.0",
"fruitdown": "1.0.2",
"immediate": "3.3.0",
"level": "6.0.1",
Expand All @@ -66,15 +66,13 @@
"browserify": "16.4.0",
"browserify-incremental": "3.1.1",
"builtin-modules": "3.1.0",
"bundle-collapser": "1.3.0",
"chai": "3.5.0",
"chai-as-promised": "5.3.0",
"change-case": "4.0.1",
"child-process-promise": "2.2.1",
"cssmin": "0.4.3",
"denodeify": "1.2.1",
"derequire": "2.1.1",
"es3ify": "0.2.2",
"eslint": "8.7.0",
"express": "4.17.0",
"express-pouchdb": "4.2.0",
Expand All @@ -90,7 +88,7 @@
"marky": "1.2.0",
"median": "0.0.2",
"mkdirp": "0.5.1",
"mocha": "3.5.0",
"mocha": "10.2.0",
"mockery": "2.1.0",
"ncp": "2.0.0",
"playwright": "1.36.1",
Expand Down
4 changes: 2 additions & 2 deletions tests/component/test.deletion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('test.deletion_error.js', function () {
server = app.listen(0);
});

after(function () {
return server.close();
after(function (done) {
server.close(done);
});

it('Test error during deletion', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/component/test.read_only_replication.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe('test.read_only_replication.js', function () {
server = app.listen(0);
});

after(function () {
return server.close();
after(function (done) {
server.close(done);
});

it('Test checkpointer handles error codes', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/component/test.replication_perf_regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe('test.replication_perf_regression.js', function () {
};
});

after(function () {
return server.close();
after(function (done) {
server.close(done);
});

it('#5199 fix excessively long replication loop', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/find/test-suite-1/test.default-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('test.default-index.js', function () {
]).then(function () {
return db.find({
selector: {foo: {$ne: "eba"}},
fields: ["_id",],
fields: ["_id"],
sort: ["_id"]
});
}).then(function (resp) {
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/browser.migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ describe('migration', function () {

var dbs = {};

before(function () {
if (usingIndexeddb() && !versionGte(scenario, '7.2.1')) {
return this.skip();
}
});

beforeEach(function (done) {
if (skip) {
return this.skip();
Expand Down Expand Up @@ -122,12 +128,6 @@ describe('migration', function () {
testUtils.cleanup([dbs.first.local, dbs.second.local], done);
});

before(function () {
if (usingIndexeddb() && !versionGte(scenario, '7.2.1')) {
return this.skip();
}
});

var origDocs = [
{_id: '0', a: 1, b: 1},
{_id: '3', a: 4, b: 16},
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/webrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
title: obj.title,
duration: obj.duration,
slow: typeof obj.slow === 'function' ? obj.slow() : undefined,
fullTitle: typeof obj.fullTitle === 'function' ? obj.fullTitle() : undefined
fullTitle: typeof obj.fullTitle === 'function' ? obj.fullTitle() : undefined,
titlePath: typeof obj.titlePath === 'function' ? obj.titlePath() : undefined,
},
err: err && {
actual: err.actual,
Expand Down

0 comments on commit fdcedf9

Please sign in to comment.