Skip to content

Commit

Permalink
test: restore nojest runner (#3359)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Aug 9, 2020
1 parent c6acc32 commit 6f09590
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 355 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -15,13 +15,14 @@
"ctestd": "cross-env BROWSER=chromium jest --reporters=./jest/dot.js --colors",
"ftestd": "cross-env BROWSER=firefox jest --reporters=./jest/dot.js --colors",
"wtestd": "cross-env BROWSER=webkit jest --reporters=./jest/dot.js --colors",
"nojest": "cross-env BROWSER=chromium node --unhandled-rejections=strict ./test/nojest/nojest.js",
"test": "npm run ctest && npm run ftest && npm run wtest",
"eslint": "[ \"$CI\" = true ] && eslint --quiet -f codeframe --ext js,ts ./src || eslint --ext js,ts ./src",
"tsc": "tsc -p .",
"tsc-installer": "tsc -p ./src/install/tsconfig.json",
"doc": "node utils/doclint/cli.js",
"doc-channel": "node utils/doclint/cli.js --channel",
"test-infra": "node utils/doclint/check_public_api/test/test.js && node utils/doclint/preprocessor/test.js && node utils/testrunner/test/test.js",
"test-infra": "node utils/doclint/check_public_api/test/test.js && node utils/doclint/preprocessor/test.js",
"lint": "npm run eslint && npm run tsc && npm run doc && npm run doc-channel && npm run check-deps && npm run generate-channels && npm run test-types && npm run test-infra",
"debug-test": "node --inspect-brk test/test.js",
"clean": "rimraf lib && rimraf types",
Expand Down
33 changes: 30 additions & 3 deletions test/jest/fixturePool.js → test/harness/fixturePool.js
Expand Up @@ -106,6 +106,25 @@ class FixturePool {
params[n] = this.instances.get(n).value;
return fn(params);
}

patchToEnableFixtures(object, name) {
const original = object[name];
object[name] = fn => {
return original(async () => {
return await this.resolveParametersAndRun(fn);
});
}
}

wrapTestCallback(callback) {
return async() => {
try {
return await this.resolveParametersAndRun(callback);
} finally {
await this.teardownScope('test');
}
};
}
}

function fixtureParameterNames(fn) {
Expand All @@ -117,8 +136,16 @@ function fixtureParameterNames(fn) {
return signature.split(',').map(t => t.trim());
}

function registerFixture(name, scope, fn) {
function innerRegisterFixture(name, scope, fn) {
registrations.set(name, { scope, fn });
}
};

function registerFixture(name, fn) {
innerRegisterFixture(name, 'test', fn);
};

function registerWorkerFixture (name, fn) {
innerRegisterFixture(name, 'worker', fn);
};

module.exports = { FixturePool, registerFixture };
module.exports = { FixturePool, registerFixture, registerWorkerFixture };
34 changes: 16 additions & 18 deletions test/jest/fixtures.js → test/harness/fixtures.js
Expand Up @@ -18,20 +18,24 @@ const path = require('path');
const childProcess = require('child_process');
const playwrightImpl = require('../../index');

const { TestServer } = require('../../utils/testserver/');
const { TestServer } = require('../../utils/testserver');
const { Connection } = require('../../lib/rpc/client/connection');
const { Transport } = require('../../lib/rpc/transport');
const { setupInProcess } = require('../../lib/rpc/inprocess');
const { setUnderTest } = require('../../lib/helper');
const { valueFromEnv } = require('./utils');
const { registerFixture, registerWorkerFixture } = require('./fixturePool');

setUnderTest();

const browserName = process.env.BROWSER || 'chromium';

module.exports = function registerFixtures(global) {
global.registerWorkerFixture('parallelIndex', async ({}, test) => {
registerWorkerFixture('parallelIndex', async ({}, test) => {
await test(process.env.JEST_WORKER_ID - 1);
});
global.registerWorkerFixture('http_server', async ({parallelIndex}, test) => {

registerWorkerFixture('http_server', async ({parallelIndex}, test) => {
const assetsPath = path.join(__dirname, '..', 'assets');
const cachedPath = path.join(__dirname, '..', 'assets', 'cached');

Expand Down Expand Up @@ -59,7 +63,7 @@ module.exports = function registerFixtures(global) {
]);
});

global.registerWorkerFixture('defaultBrowserOptions', async({}, test) => {
registerWorkerFixture('defaultBrowserOptions', async({}, test) => {
let executablePath = undefined;
if (browserName === 'chromium' && process.env.CRPATH)
executablePath = process.env.CRPATH;
Expand All @@ -77,7 +81,7 @@ module.exports = function registerFixtures(global) {
});
});

global.registerWorkerFixture('playwright', async({}, test) => {
registerWorkerFixture('playwright', async({}, test) => {
if (process.env.PWCHANNEL === 'wire') {
const connection = new Connection();
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', '..', 'lib', 'rpc', 'server'), [], {
Expand Down Expand Up @@ -108,15 +112,15 @@ module.exports = function registerFixtures(global) {
}
});

global.registerFixture('toImpl', async ({playwright}, test) => {
registerFixture('toImpl', async ({playwright}, test) => {
await test(playwright._toImpl);
});

global.registerWorkerFixture('browserType', async ({playwright}, test) => {
registerWorkerFixture('browserType', async ({playwright}, test) => {
await test(playwright[process.env.BROWSER || 'chromium']);
});

global.registerWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, test) => {
registerWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, test) => {
const browser = await browserType.launch(defaultBrowserOptions);
try {
await test(browser);
Expand All @@ -129,7 +133,7 @@ module.exports = function registerFixtures(global) {
}
});

global.registerFixture('context', async ({browser}, test) => {
registerFixture('context', async ({browser}, test) => {
const context = await browser.newContext();
try {
await test(context);
Expand All @@ -138,24 +142,18 @@ module.exports = function registerFixtures(global) {
}
});

global.registerFixture('page', async ({context}, test) => {
registerFixture('page', async ({context}, test) => {
const page = await context.newPage();
await test(page);
});

global.registerFixture('server', async ({http_server}, test) => {
registerFixture('server', async ({http_server}, test) => {
http_server.server.reset();
await test(http_server.server);
});

global.registerFixture('httpsServer', async ({http_server}, test) => {
registerFixture('httpsServer', async ({http_server}, test) => {
http_server.httpsServer.reset();
await test(http_server.httpsServer);
});
}

function valueFromEnv(name, defaultValue) {
if (!(name in process.env))
return defaultValue;
return JSON.parse(process.env[name]);
}
38 changes: 38 additions & 0 deletions test/harness/testOptions.js
@@ -0,0 +1,38 @@
/**
* 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 os = require('os');
const path = require('path');
const { valueFromEnv } = require('./utils');

const platform = process.env.REPORT_ONLY_PLATFORM || os.platform();
const browserName = process.env.BROWSER || 'chromium';

const testOptions = {};
testOptions.MAC = platform === 'darwin';
testOptions.LINUX = platform === 'linux';
testOptions.WIN = platform === 'win32';
testOptions.CHROMIUM = browserName === 'chromium';
testOptions.FFOX = browserName === 'firefox';
testOptions.WEBKIT = browserName === 'webkit';
testOptions.USES_HOOKS = process.env.PWCHANNEL === 'wire';
testOptions.CHANNEL = !!process.env.PWCHANNEL;
testOptions.HEADLESS = !!valueFromEnv('HEADLESS', true);
testOptions.ASSETS_DIR = path.join(__dirname, '..', 'assets');
testOptions.GOLDEN_DIR = path.join(__dirname, '..', 'golden-' + browserName);
testOptions.OUTPUT_DIR = path.join(__dirname, '..', 'output-' + browserName);

module.exports = testOptions;
23 changes: 23 additions & 0 deletions test/harness/utils.js
@@ -0,0 +1,23 @@
/**
* 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.
*/

function valueFromEnv(name, defaultValue) {
if (!(name in process.env))
return defaultValue;
return JSON.parse(process.env[name]);
}

module.exports = { valueFromEnv };
65 changes: 17 additions & 48 deletions test/jest/playwrightEnvironment.js
Expand Up @@ -14,45 +14,29 @@
* limitations under the License.
*/

const registerFixtures = require('./fixtures');
const { FixturePool, registerFixture } = require('./fixturePool');
const { FixturePool, registerFixture, registerWorkerFixture } = require('../harness/fixturePool');
const registerFixtures = require('../harness/fixtures');
const os = require('os');
const path = require('path');
const fs = require('fs');
const debug = require('debug');
const util = require('util');
const platform = process.env.REPORT_ONLY_PLATFORM || os.platform();
const GoldenUtils = require('../../utils/testrunner/GoldenUtils');
const {installCoverageHooks} = require('./coverage');
const browserName = process.env.BROWSER || 'chromium';
const reportOnly = !!process.env.REPORT_ONLY_PLATFORM;
const { ModuleMocker } = require('jest-mock');

const testOptions = {};
testOptions.MAC = platform === 'darwin';
testOptions.LINUX = platform === 'linux';
testOptions.WIN = platform === 'win32';
testOptions.CHROMIUM = browserName === 'chromium';
testOptions.FFOX = browserName === 'firefox';
testOptions.WEBKIT = browserName === 'webkit';
testOptions.USES_HOOKS = process.env.PWCHANNEL === 'wire';
testOptions.CHANNEL = !!process.env.PWCHANNEL;
testOptions.HEADLESS = !!valueFromEnv('HEADLESS', true);
testOptions.ASSETS_DIR = path.join(__dirname, '..', 'assets');
testOptions.GOLDEN_DIR = path.join(__dirname, '..', 'golden-' + browserName);
testOptions.OUTPUT_DIR = path.join(__dirname, '..', 'output-' + browserName);
global.testOptions = testOptions;

global.registerFixture = (name, fn) => {
registerFixture(name, 'test', fn);
};

global.registerWorkerFixture = (name, fn) => {
registerFixture(name, 'worker', fn);
};

Error.stackTraceLimit = 15;
global.testOptions = require('../harness/testOptions');
global.registerFixture = registerFixture;
global.registerWorkerFixture = registerWorkerFixture;
registerFixtures(global);

const browserName = process.env.BROWSER || 'chromium';

const goldenPath = path.join(__dirname, '..', 'golden-' + browserName);
const outputPath = path.join(__dirname, '..', 'output-' + browserName);

let currentFixturePool = null;

process.on('SIGINT', async () => {
Expand Down Expand Up @@ -89,7 +73,7 @@ class PlaywrightEnvironment {
this.uninstallCoverage();
const testRoot = path.join(__dirname, '..');
const relativeTestPath = path.relative(testRoot, this.testPath);
const coveragePath = path.join(this.global.testOptions.OUTPUT_DIR, 'coverage', relativeTestPath + '.json');
const coveragePath = path.join(outputPath, 'coverage', relativeTestPath + '.json');
const coverageJSON = [...this.coverage.keys()].filter(key => this.coverage.get(key));
await fs.promises.mkdir(path.dirname(coveragePath), { recursive: true });
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
Expand All @@ -101,19 +85,10 @@ class PlaywrightEnvironment {
return script.runInThisContext();
}

patchToEnableFixtures(object, name) {
const original = object[name];
object[name] = fn => {
return original(async () => {
return await this.fixturePool.resolveParametersAndRun(fn);
});
}
}

async handleTestEvent(event, state) {
if (event.name === 'setup') {
this.patchToEnableFixtures(this.global, 'beforeEach');
this.patchToEnableFixtures(this.global, 'afterEach');
this.fixturePool.patchToEnableFixtures(this.global, 'beforeEach');
this.fixturePool.patchToEnableFixtures(this.global, 'afterEach');

const describeSkip = this.global.describe.skip;
this.global.describe.skip = (...args) => {
Expand Down Expand Up @@ -152,7 +127,7 @@ class PlaywrightEnvironment {
function toBeGolden(received, goldenName) {
const {snapshotState} = this;
const updateSnapshot = snapshotState._updateSnapshot;
const expectedPath = path.join(testOptions.GOLDEN_DIR, goldenName);
const expectedPath = path.join(goldenPath, goldenName);
const fileExists = fs.existsSync(expectedPath);
if (updateSnapshot === 'all' || (updateSnapshot === 'new' && !fileExists)) {
fs.writeFileSync(expectedPath, received);
Expand All @@ -166,8 +141,8 @@ class PlaywrightEnvironment {
};

const {pass, message} = GoldenUtils.compare(received, {
goldenPath: testOptions.GOLDEN_DIR,
outputPath: testOptions.OUTPUT_DIR,
goldenPath,
outputPath,
goldenName
});
if (pass)
Expand Down Expand Up @@ -210,12 +185,6 @@ class PlaywrightEnvironment {
}
}

function valueFromEnv(name, defaultValue) {
if (!(name in process.env))
return defaultValue;
return JSON.parse(process.env[name]);
}

function testOrSuiteName(o) {
if (o.name === 'ROOT_DESCRIBE_BLOCK')
return '';
Expand Down

0 comments on commit 6f09590

Please sign in to comment.