Skip to content

Commit

Permalink
feat: add support for zora and node flag
Browse files Browse the repository at this point in the history
The node flag disables all the node injections similar to future webpack 5 behaviour.
  • Loading branch information
hugomrdias committed Mar 30, 2020
1 parent 20d784a commit 5a7b962
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 48 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
- run: xvfb-run -a ./cli.js mocks/test.mocha.js --extension
- run: ./cli.js mocks/test.tape.js --runner tape
- run: ./cli.js mocks/benchmark.js --runner benchmark
- run: RUN_ONLY=true INDENT=true ./cli.js 'mocks/*.zora.js' --runner zora

chromium_macos:
name: "Chromium Mac"
Expand Down
17 changes: 14 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ const { findTests, defaultTestPatterns } = require('./src/utils');
const MochaRunner = require('./src/runner-mocha');
const TapeRunner = require('./src/runner-tape');
const BenchmarkRunner = require('./src/runner-benchmark');
const ZoraRunner = require('./src/runner-zora');

const cli = meow(`
Usage
$ playwright-test [input]
Options
--runner Test runner. Options: mocha, tape, benchmark. [Default: mocha]
--runner Test runner. Options: mocha, tape, benchmark and zora. [Default: mocha]
--watch, -w Watch files for changes and re-run tests.
--browser, -b Browser to run tests. Options: chromium, firefox, webkit. [Default: chromium]
--debug, -d Debug mode, keeps browser window open.
Expand Down Expand Up @@ -96,6 +97,10 @@ Usage
before: {
type: 'string',
default: ''
},
node: {
type: 'boolean',
default: true
}
}
});
Expand Down Expand Up @@ -125,7 +130,8 @@ const runnerOptions = () => {
'cwd',
'extensions',
'assets',
'before'
'before',
'node'
];

if (!localFlags.includes(key)) {
Expand All @@ -143,6 +149,10 @@ if (files.length === 0) {

let Runner = null;

if (cli.flags.runner === 'zora') {
Runner = ZoraRunner;
}

if (cli.flags.runner === 'benchmark') {
Runner = BenchmarkRunner;
}
Expand All @@ -162,7 +172,8 @@ const runner = new Runner({
files,
extension: cli.flags.extension,
runnerOptions: runnerOptions(),
before: cli.flags.before
before: cli.flags.before,
node: cli.flags.node
});

if (cli.flags.watch) {
Expand Down
19 changes: 19 additions & 0 deletions mocks/test1.zora.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const { test } = require('zora');

test('a first sub test', (t) => {
t.ok(true);

t.test('inside', (t) => {
t.ok(true);
});
});

test('a first sub test', (t) => {
t.ok(true);

t.test('inside', (t) => {
t.ok(false, 'oh no!');
});
});
40 changes: 40 additions & 0 deletions mocks/test2.zora.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const { test, only } = require('zora');
// import {test, only} from 'zora';

test('some grouped assertions', (t) => {
t.ok(true, 'true is truthy');
t.equal('bar', 'bar', 'that both string are equivalent');
t.isNot({}, {}, 'those are not the same reference');
});

test('some grouped assertions', (t) => {
t.ok(true, 'true is truthy');

t.test('a group inside another one', (t) => {
t.equal('bar', 'bar', 'that both string are equivalent');
t.isNot({}, {}, 'those are not the same reference');
});
});
only('should run', (t) => {
t.ok(true, 'I ran');

t.only('keep running', (t) => {
t.only('keeeeeep running', (t) => {
t.ok(true, ' I got there');
});
});

t.test('should not run', (t) => {
t.fail('shouldn ot run');
});
});
only('should run but nothing inside', (t) => {
t.test('will not run', (t) => {
t.fail('should not run');
});
t.test('will not run', (t) => {
t.fail('should not run');
});
});
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"mocha",
"tape",
"benchmark",
"benchmark.js"
"benchmark.js",
"zora"
],
"dependencies": {
"camelcase": "^5.3.1",
Expand All @@ -50,7 +51,8 @@
"resolve-cwd": "^3.0.0",
"sirv": "^0.4.2",
"tempy": "^0.4.0",
"webpack": "^4.41.5"
"webpack": "^4.41.5",
"webpack-merge": "^4.2.2"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
Expand All @@ -70,7 +72,9 @@
"np": "^6.2.0",
"npm-run-all": "^4.1.1",
"nyc": "^15.0.0",
"tape": "^4.13.0"
"tap-spec": "^5.0.0",
"tape": "^4.13.0",
"zora": "^3.1.8"
},
"husky": {
"hooks": {
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# playwright-test [![NPM Version](https://img.shields.io/npm/v/playwright-test.svg)](https://www.npmjs.com/package/playwright-test) [![NPM Downloads](https://img.shields.io/npm/dt/playwright-test.svg)](https://www.npmjs.com/package/playwright-test) [![NPM License](https://img.shields.io/npm/l/playwright-test.svg)](https://www.npmjs.com/package/playwright-test) ![tests](https://github.com/hugomrdias/playwright-test/workflows/tests/badge.svg)

> Run mocha, tape and benchmark.js scripts inside real browsers with `playwright`.
> Run mocha, zora, tape and benchmark.js scripts inside real browsers with `playwright`.

## Install
Expand All @@ -12,11 +12,11 @@ $ npm install playwright-test

## Usage

```bash
```console
$ playwright-test [input]

Options
--runner Test runner. Options: mocha, tape, benchmark. [Default: mocha]
--runner Test runner. Options: mocha, tape, benchmark and zora. [Default: mocha]
--watch, -w Watch files for changes and re-run tests.
--browser, -b Browser to run tests. Options: chromium, firefox, webkit. [Default: chromium]
--debug, -d Debug mode, keeps browser window open.
Expand Down
47 changes: 13 additions & 34 deletions src/runner-mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const webpack = require('webpack');
const merge = require('merge-options');
const delay = require('delay');
const resolveCwd = require('resolve-cwd');
const webpackMerge = require('webpack-merge');
const Runner = require('./runner');
const { addWorker } = require('./utils');
const { addWorker, defaultWebpackConfig } = require('./utils');

const runMocha = () => `
mocha
Expand Down Expand Up @@ -70,40 +71,18 @@ class MochaRunner extends Runner {
}

compiler() {
const compiler = webpack({
mode: 'development',
output: {
path: this.dir,
filename: 'bundle.[hash].js',
devtoolModuleFilenameTemplate: info =>
'file:///' + encodeURI(info.absoluteResourcePath)
},
entry: [
require.resolve('./setup-mocha.js'),
...this.options.files
],
resolve: { alias: { 'mocha/mocha': resolveCwd('mocha/mocha.js') } },
node: {
'dgram': 'empty',
'fs': 'empty',
'net': 'empty',
'tls': 'empty',
'child_process': 'empty',
'console': false,
'global': true,
'process': true,
'__filename': 'mock',
'__dirname': 'mock',
'Buffer': true,
'setImmediate': true
},
plugins: [
new webpack.DefinePlugin({ 'process.env': JSON.stringify(this.env) })
]

});
const config = webpackMerge(
defaultWebpackConfig(this.dir, this.env, this.options),
{
entry: [
require.resolve('./setup-mocha.js'),
...this.options.files
],
resolve: { alias: { 'mocha/mocha': resolveCwd('mocha/mocha.js') } }
}
);

return compiler;
return webpack(config);
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/runner-zora.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable no-console */
'use strict';

const path = require('path');
const webpack = require('webpack');
const delay = require('delay');
const merge = require('webpack-merge');
const Runner = require('./runner');
const { addWorker, defaultWebpackConfig } = require('./utils');

const runZora = () => `
zora
.report()
.then((f) =>{
self.pwTestController.end(!self.zora.pass)
})
`;

const runZoraWorker = () => `
zora
.report()
.then((f) =>{
postMessage({
"pwRunEnded": true,
"pwRunFailed": !self.zora.pass
})
})
`;

class ZoraRunner extends Runner {
async runTests() {
switch (this.options.mode) {
case 'main': {
await this.page.addScriptTag({
type: 'text/javascript',
url: this.file
});
await this.page.evaluate(runZora());
break;
}
case 'worker': {
this.page.evaluate(addWorker(this.file));
const run = new Promise((resolve) => {
this.page.on('workercreated', async (worker) => {
await delay(1000);
await worker.evaluate(runZoraWorker());
resolve();
});
});

await run;
break;
}
default:
console.error('mode not supported');
break;
}
}

compiler() {
const config = merge(
defaultWebpackConfig(this.dir, this.env, this.options),
{
entry: [
...this.options.files
],
resolve: { alias: { zora$: path.resolve(__dirname, 'setup-zora.js') } }
}
);

return webpack(config);
}
}

module.exports = ZoraRunner;
11 changes: 11 additions & 0 deletions src/setup-zora.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const { createHarness } = require('zora/dist/bundle/index');

const harness = createHarness({
indent: process.env.INDENT === 'true',
runOnly: process.env.RUN_ONLY === 'true'
});

self.zora = harness;
module.exports = harness;
Loading

0 comments on commit 5a7b962

Please sign in to comment.