playwright/test/mocha/worker.js

169 lines
4.7 KiB
JavaScript
Raw Normal View History

/**
* Copyright Microsoft Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require('path');
const Mocha = require('mocha');
2020-08-11 19:44:13 -07:00
const { fixturesUI, fixturePool } = require('./fixturesUI');
const { gracefullyCloseAll } = require('../../lib/server/processLauncher');
const GoldenUtils = require('../../utils/testrunner/GoldenUtils');
const browserName = process.env.BROWSER || 'chromium';
const goldenPath = path.join(__dirname, '..', 'golden-' + browserName);
const outputPath = path.join(__dirname, '..', 'output-' + browserName);
global.expect = require('expect');
global.testOptions = require('../harness/testOptions');
extendExpects();
let closed = false;
process.on('message', async message => {
if (message.method === 'init')
2020-08-11 19:44:13 -07:00
process.env.JEST_WORKER_ID = message.params.workerId;
if (message.method === 'stop')
2020-08-11 19:44:13 -07:00
await gracefullyCloseAndExit();
if (message.method === 'run')
2020-08-11 19:44:13 -07:00
await runSingleTest(message.params.file, message.params.options);
});
process.on('disconnect', gracefullyCloseAndExit);
process.on('SIGINT',() => {});
process.on('SIGTERM',() => {});
sendMessageToParent('ready');
async function gracefullyCloseAndExit() {
closed = true;
// Force exit after 30 seconds.
setTimeout(() => process.exit(0), 30000);
// Meanwhile, try to gracefully close all browsers.
await gracefullyCloseAll();
process.exit(0);
}
class NullReporter {}
2020-08-11 19:44:13 -07:00
async function runSingleTest(file, options) {
let nextOrdinal = 0;
const mocha = new Mocha({
2020-08-11 19:44:13 -07:00
ui: fixturesUI.bind(null, false),
retries: options.retries === 1 ? undefined : options.retries,
timeout: options.timeout,
reporter: NullReporter
});
mocha.addFile(file);
const runner = mocha.run();
const constants = Mocha.Runner.constants;
runner.on(constants.EVENT_RUN_BEGIN, () => {
sendMessageToParent('start');
});
runner.on(constants.EVENT_TEST_BEGIN, test => {
2020-08-11 19:44:13 -07:00
// Retries will produce new test instances, store ordinal on the original function.
let ordinal = nextOrdinal++;
if (typeof test.fn.__original.__ordinal !== 'number')
test.fn.__original.__ordinal = ordinal;
sendMessageToParent('test', { test: serializeTest(test, ordinal) });
});
runner.on(constants.EVENT_TEST_PENDING, test => {
2020-08-11 19:44:13 -07:00
// Pending does not get test begin signal, so increment ordinal.
sendMessageToParent('pending', { test: serializeTest(test, nextOrdinal++) });
});
runner.on(constants.EVENT_TEST_PASS, test => {
2020-08-11 19:44:13 -07:00
sendMessageToParent('pass', { test: serializeTest(test, test.fn.__original.__ordinal) });
});
runner.on(constants.EVENT_TEST_FAIL, (test, error) => {
sendMessageToParent('fail', {
2020-08-11 19:44:13 -07:00
test: serializeTest(test, test.fn.__original.__ordinal),
error: serializeError(error),
2020-08-11 19:44:13 -07:00
});
});
runner.once(constants.EVENT_RUN_END, async () => {
sendMessageToParent('end', { stats: serializeStats(runner.stats) });
sendMessageToParent('done');
});
}
function sendMessageToParent(method, params = {}) {
if (closed)
return;
try {
process.send({ method, params });
} catch (e) {
// Can throw when closing.
}
}
2020-08-11 19:44:13 -07:00
function serializeTest(test, origin) {
return {
2020-08-11 19:44:13 -07:00
id: `${test.file}::${origin}`,
currentRetry: test.currentRetry(),
duration: test.duration,
title: test.title,
};
}
function serializeStats(stats) {
return {
tests: stats.tests,
passes: stats.passes,
duration: stats.duration,
failures: stats.failures,
pending: stats.pending,
}
}
function trimCycles(obj) {
const cache = new Set();
return JSON.parse(
JSON.stringify(obj, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.has(value))
return '' + value;
cache.add(value);
}
return value;
})
);
}
function serializeError(error) {
if (error instanceof Error) {
return {
message: error.message,
stack: error.stack
}
}
return trimCycles(error);
}
function extendExpects() {
function toBeGolden(received, goldenName) {
const {pass, message} = GoldenUtils.compare(received, {
goldenPath,
outputPath,
goldenName
});
return {pass, message: () => message};
};
global.expect.extend({ toBeGolden });
}