Skip to content

Commit

Permalink
feat: add support for config files
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Dec 9, 2020
1 parent 06bd0d4 commit d657f37
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 145 deletions.
59 changes: 27 additions & 32 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

const sade = require('sade');
const kleur = require('kleur');
const lilconfig = require('lilconfig');
const merge = require('merge-options').bind({ ignoreUndefined: true });
const pkg = require('./package.json');
const { findTests, defaultTestPatterns, runnerOptions } = require('./src/utils');
const { runnerOptions } = require('./src/utils');
const UvuRunner = require('./src/runner-uvu');
const MochaRunner = require('./src/runner-mocha');
const TapeRunner = require('./src/runner-tape');
Expand Down Expand Up @@ -68,29 +70,19 @@ sade2
.version(pkg.version)
.describe('Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with `playwright`.')
.option('-r, --runner', 'Test runner. Options: mocha, tape, benchmark and zora.', 'mocha')
.option('-b, --browser', 'Browser to run tests. Options: chromium, firefox, webkit.', 'chromium')
.option('-m, -mode', 'Run mode. Options: main, worker.', 'main')
.option('-b, --browser', 'Browser to run tests. Options: chromium, firefox, webkit. (default chromium)')
.option('-m, --mode', 'Run mode. Options: main, worker. (default main)')
.option('-d, --debug', 'Debug mode, keeps browser window open.')
.option('-w, --watch', 'Watch files for changes and re-run tests.')
.option('-i, --incognito', 'Use incognito window to run tests.')
.option('-e, --extension', ' Use extension background_page to run tests.')
.option('-e, --extension', 'Use extension background_page to run tests.')
.option('--cov', 'Enable code coverage in istanbul format. Outputs \'.nyc_output/out.json\'.')
.option('--before', 'Full path to a script to be loaded on a separate tab before the main script.')
.option('--assets', 'Assets to be served by the http server.', process.cwd())
.option('--cwd', 'Current directory.', process.cwd())
.option('--extensions', 'File extensions allowed in the bundle.', 'js,cjs,mjs')
.option('--assets', 'Assets to be served by the http server. (default process.cwd())')
.option('--cwd', 'Current directory. (default process.cwd())')
.option('--extensions', 'File extensions allowed in the bundle. (default js,cjs,mjs)')
.action((input, opts) => {
const extensions = opts.extensions.split(',');
const tests = findTests({
cwd: opts.cwd,
extensions: extensions,
filePatterns: input ? input : defaultTestPatterns(extensions)
});

if (tests.length === 0) {
console.log('No test files were found.');
process.exit(0);
}
const config = lilconfig.lilconfigSync('playwright-test').search();

let Runner = null;

Expand All @@ -114,20 +106,23 @@ sade2
console.error('Runner not supported: ', opts.runner);
process.exit(1);
}
const runner = new Runner({
cwd: opts.cwd,
assets: opts.assets,
browser: opts.browser,
debug: opts.debug,
mode: opts.mode,
incognito: opts.incognito,
files: tests,
extension: opts.extension,
runnerOptions: runnerOptions(opts),
before: opts.before,
node: opts.node,
cov: opts.cov
});
const runner = new Runner(merge(
config ? config.config : {},
{
cwd: opts.cwd,
assets: opts.assets,
browser: opts.browser,
debug: opts.debug,
mode: opts.mode,
incognito: opts.incognito,
input,
extension: opts.extension,
runnerOptions: runnerOptions(opts),
before: opts.before,
node: opts.node,
cov: opts.cov
}
));

if (opts.watch) {
runner.watch();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"get-port": "^5.1.1",
"globby": "^11.0.1",
"kleur": "^4.1.3",
"lilconfig": "^2.0.2",
"lodash": "^4.17.20",
"merge-options": "^3.0.4",
"ora": "^5.1.0",
Expand Down
14 changes: 10 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

## Install

```
```shell
$ npm install playwright-test
```


## Usage
```shell
$ playwright-test [files] [options]
# or
$ pw-test [files] [options]

```
## Options

```console
```shell
Description
Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with `playwright`.

Expand All @@ -26,7 +32,7 @@ Description
-d, --debug Debug mode, keeps browser window open.
-w, --watch Watch files for changes and re-run tests.
-i, --incognito Use incognito window to run tests.
-e, --extension Use extension background_page to run tests.
-e, --extension Use extension background_page to run tests.
--cov Enable code coverage in istanbul format. Outputs '.nyc_output/out.json'.
--before Full path to a script to be loaded on a separate tab before the main script.
--assets Assets to be served by the http server. (default process.cwd())
Expand Down
2 changes: 1 addition & 1 deletion src/runner-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BenchmarkRunner extends Runner {
{
entry: [
require.resolve('./setup-bench.js'),
...this.options.files
...this.tests
],
module: { noParse: /src\/benchmark.js/ },
resolve: { alias: { benchmark: path.resolve(__dirname, 'setup-bench.js') } }
Expand Down
30 changes: 16 additions & 14 deletions src/runner-mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ mocha

class MochaRunner extends Runner {
constructor(options = {}) {
super(merge({
runnerOptions: {
allowUncaught: false,
bail: true,
reporter: 'spec',
timeout: 5000,
color: true,
ui: 'bdd'
}
}, options));
super(
merge(
{
runnerOptions: {
allowUncaught: false,
bail: true,
reporter: 'spec',
timeout: 5000,
color: true,
ui: 'bdd'
}
},
options
)
);
}

async runTests() {
Expand Down Expand Up @@ -69,10 +74,7 @@ class MochaRunner extends Runner {
const config = webpackMerge(
defaultWebpackConfig(this.dir, this.env, this.options),
{
entry: [
require.resolve('./setup-mocha.js'),
...this.options.files
],
entry: [require.resolve('./setup-mocha.js'), ...this.tests],
resolve: { alias: { 'mocha/mocha': resolveCwd('mocha/mocha.js') } }
}
);
Expand Down
6 changes: 1 addition & 5 deletions src/runner-tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ class TapeRunner extends Runner {
const config = merge(
defaultWebpackConfig(this.dir, this.env, this.options),
{
entry: [
require.resolve('./setup-tape.js'),
...this.options.files
],
entry: [require.resolve('./setup-tape.js'), ...this.tests],
resolve: { alias: { tape: resolveCwd('tape') } }

}
);

Expand Down
6 changes: 1 addition & 5 deletions src/runner-uvu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ class UvuRunner extends Runner {
compiler() {
const config = merge(
defaultWebpackConfig(this.dir, this.env, this.options),
{
entry: [
...this.options.files
]
}
{ entry: this.tests }
);

return webpack(config);
Expand Down
4 changes: 1 addition & 3 deletions src/runner-zora.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ class ZoraRunner extends Runner {
const config = merge(
defaultWebpackConfig(this.dir, this.env, this.options),
{
entry: [
...this.options.files
],
entry: this.tests,
resolve: { alias: { zora$: path.resolve(__dirname, 'setup-zora.js') } }
}
);
Expand Down
Loading

0 comments on commit d657f37

Please sign in to comment.