Skip to content

Commit

Permalink
feat: esbuild, cov and exceptions (#141)
Browse files Browse the repository at this point in the history
- esbuild is used instead of webpack
- stability improves all around
- improved input files
- improved configuration with config files
- improved coverage output, excludes.
  • Loading branch information
hugomrdias authored Feb 15, 2021
1 parent c9157b2 commit 2abe8b9
Show file tree
Hide file tree
Showing 23 changed files with 1,188 additions and 419 deletions.
38 changes: 7 additions & 31 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,31 @@ jobs:
linux:
name: "Linux"
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
node: [14]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- uses: microsoft/playwright-github-action@v1
- run: yarn
- run: yarn lint
- run: xvfb-run -a yarn test
- run: npm install
- run: npm run lint
- run: xvfb-run -a npm run test
- run: ./cli.js mocks/test.mocha.js --browser firefox
- run: ./cli.js mocks/test.mocha.js --browser webkit

macos:
name: "Mac"
runs-on: macos-latest
strategy:
fail-fast: true
matrix:
node: [12, 14]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: yarn
- run: yarn test
- run: npm install
- run: npm test
- run: ./cli.js mocks/test.mocha.js --browser firefox
- run: ./cli.js mocks/test.mocha.js --browser webkit

win:
name: "Win"
runs-on: windows-latest
strategy:
fail-fast: true
matrix:
node: [12, 14]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: yarn
- run: yarn test
shell: bash
- run: npm install
- run: npm test
- run: ./cli.js mocks/test.mocha.js --browser firefox
shell: bash
- run: ./cli.js mocks/test.mocha.js --browser webkit
shell: bash
36 changes: 30 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

'use strict';

const path = require('path');
const sade = require('sade');
const kleur = require('kleur');
const lilconfig = require('lilconfig');
Expand All @@ -15,6 +16,18 @@ const TapeRunner = require('./src/runner-tape');
const BenchmarkRunner = require('./src/runner-benchmark');
const ZoraRunner = require('./src/runner-zora');

// Handle any uncaught errors
process.once('uncaughtException', (err, origin) => {
if (!origin || origin === 'uncaughtException') {
console.error(err);
process.exit(1);
}
});
process.once('unhandledRejection', (err) => {
console.error(err);
process.exit(1);
});

const extra = `
${kleur.bold('Examples')}
${kleur.dim('$ playwright-test test.js --runner tape')}
Expand Down Expand Up @@ -76,13 +89,23 @@ sade2
.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('--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('--cov', 'Enable code coverage in istanbul format. Outputs \'.nyc_output/coverage-pw.json\'.')
.option('--before', '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. (default process.cwd())')
.option('--cwd', 'Current directory. (default process.cwd())')
.option('--extensions', 'File extensions allowed in the bundle. (default js,cjs,mjs)')
.option('--extensions', 'File extensions allowed in the bundle. (default js,cjs,mjs,ts,tsx)')
.option('--config', 'Path to the config file')
.action((input, opts) => {
const config = lilconfig.lilconfigSync('playwright-test').search();
let config;

if (opts.config) {
config = lilconfig.lilconfigSync('playwright-test').load(path.resolve(opts.config));
} else {
config = lilconfig.lilconfigSync('playwright-test').search();
if (!config) {
config = lilconfig.lilconfigSync('pw-test').search();
}
}

let Runner = null;

Expand Down Expand Up @@ -115,12 +138,13 @@ sade2
debug: opts.debug,
mode: opts.mode,
incognito: opts.incognito,
input,
input: [input, ...opts._],
extension: opts.extension,
runnerOptions: runnerOptions(opts),
before: opts.before,
node: opts.node,
cov: opts.cov
cov: opts.cov,
extensions: opts.extensions
}
));

Expand Down
11 changes: 11 additions & 0 deletions mocks/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import delay from 'delay'

export const good = async () => {
await delay(100)
return 'good'
}

export const bad = async () => {
await delay(100)
return 'bad'
}
12 changes: 10 additions & 2 deletions mocks/test.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// eslint-disable-next-line strict
const assert = require('assert');
const debug = require('debug')('app');
const { good, bad } = require('./lib');

describe('Array', () => {
describe('#indexOf()', () => {
Expand All @@ -10,13 +11,20 @@ describe('Array', () => {
});

it('should fail ', () => {
// console.log(chrome);
assert.equal([1, 2, 3].indexOf(2), 1);
assert.strictEqual([1, 2, 3].indexOf(2), 1);
});

it('should pass with debug', () => {
assert.equal([1, 2, 3].indexOf(4), -1);
debug('test pass');
});

it('should return "good"', async () => {
assert.strictEqual(await good(), 'good');
});

it('should return "bad"', async () => {
assert.strictEqual(await bad(), 'bad');
});
});
});
6 changes: 3 additions & 3 deletions mocks/test2.zora.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// 'use strict';

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

test('some grouped assertions', (t) => {
t.ok(true, 'true is truthy');
Expand Down
2 changes: 1 addition & 1 deletion mocks/uvu/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('sum', () => {
});

test('sum', async () => {
await delay(2000);
await delay(100);
assert.type(() => {}, 'function');
assert.is(3, 3);
});
Expand Down
50 changes: 16 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,61 +41,42 @@
],
"dependencies": {
"camelcase": "^6.2.0",
"delay": "^4.4.0",
"get-port": "^5.1.1",
"delay": "^5.0.0",
"esbuild": "^0.8.46",
"globby": "^11.0.2",
"kleur": "^4.1.4",
"lilconfig": "^2.0.2",
"lodash": "^4.17.20",
"merge-options": "^3.0.4",
"ora": "^5.3.0",
"p-each-series": "^2.2.0",
"playwright-core": "1.8.0",
"path-browserify": "^1.0.1",
"playwright-core": "1.8.1",
"polka": "^0.5.2",
"resolve-cwd": "^3.0.0",
"premove": "^3.0.1",
"process": "^0.11.10",
"sade": "^1.7.4",
"sirv": "^1.0.10",
"sirv": "^1.0.11",
"source-map": "^0.6.0",
"strip-ansi": "^6.0.0",
"tempy": "^1.0.0",
"v8-to-istanbul": "^7.1.0",
"webpack": "^4.42.1",
"webpack-merge": "^4.2.2"
"test-exclude": "^6.0.0",
"v8-to-istanbul": "^7.1.0"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"assert": "^2.0.0",
"benchmark": "^2.1.4",
"debug": "^4.3.1",
"eslint": "^4.7.1",
"eslint-config-halo": "^2.3.3",
"esm": "^3.2.25",
"execa": "^5.0.0",
"husky": "^4.3.8",
"fresh-tape": "^5.1.1",
"lint-staged": "^10.5.3",
"mocha": "^8.2.1",
"np": "^7.2.0",
"np": "^7.3.0",
"tap-spec": "^5.0.0",
"tape": "^5.1.1",
"uvu": "^0.5.1",
"zora": "^4.0.2"
},
"husky": {
"hooks": {
"pre-commit": "yarn test && lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"lint-staged": {
"*.js": [
"eslint --fix"
]
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"eslintConfig": {
"extends": "halo/plugins",
"parserOptions": {
Expand All @@ -106,8 +87,9 @@
"node_modules",
"coverage",
"dist",
"storybook-static",
"typings",
"src/benchmark.js"
"src/vendor/benchmark.js",
"src/vendor/source-map-support.js",
"node-globals.js",
"mocks"
]
}
22 changes: 19 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ Description
-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.
--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.
--cov Enable code coverage in istanbul format. Outputs '.nyc_output/coverage-pw.json'.
--before 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())
--cwd Current directory. (default process.cwd())
--extensions File extensions allowed in the bundle. (default js,cjs,mjs)
--extensions File extensions allowed in the bundle. (default js,cjs,mjs,ts,tsx)
--config Path to the config file
-v, --version Displays current version
-h, --help Displays this message

Expand Down Expand Up @@ -72,6 +73,21 @@ Description
$ playwright-test "test/**" GOOD
$ playwright-test test/** BAD
```
## Config
Configuration can be done with cli flags or config files.
```js
'package.json', // using property `pw-test` or `playwright-test`
`.playwright-testrc.json`,
`.playwright-testrc.js`,
`playwright-test.config.js`,
`.playwright-testrc.cjs`,
`playwright-test.config.cjs`,
`.pw-testrc.json`,
`.pw-testrc.js`,
`pw-test.config.js`,
`.pw-testrc.cjs`,
`pw-test.config.cjs`,
```
## Run in CI
Check our CI config `.github/workflows/main.yml` and the playwright Github Action https://playwright.dev/#version=v1.5.2&path=docs%2Fci.md&q=github-actions
Expand Down
2 changes: 2 additions & 0 deletions src/node-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const process = require('process/browser')
// https://github.com/ionic-team/rollup-plugin-node-polyfills
23 changes: 9 additions & 14 deletions src/runner-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
'use strict';

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

class BenchmarkRunner extends Runner {
compiler() {
const config = merge(
defaultWebpackConfig(this.dir, this.env, this.options),
{
entry: [
require.resolve('./setup-bench.js'),
...this.tests
],
module: { noParse: /src\/benchmark.js/ },
resolve: { alias: { benchmark: path.resolve(__dirname, 'setup-bench.js') } }
const plugin = {
name: 'swap benchmark',
setup(build) {
build.onResolve({ filter: /^benchmark$/ }, () => {
return { path: path.join(__dirname, 'setup-bench.js') };
});
}
);
};

return webpack(config);
return build(this, { plugins: [plugin] });
}
}

Expand Down
Loading

0 comments on commit 2abe8b9

Please sign in to comment.