Skip to content

Commit

Permalink
feat: add browsers option to test command (#27)
Browse files Browse the repository at this point in the history
* feat: add browsers option to test command
  • Loading branch information
bualoy-napat authored Oct 14, 2021
1 parent 7df76c1 commit 64a1d46
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 35 deletions.
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,43 @@ npm run build

The command-line interface tool that you can use to demo, develop, test, and maintain this repository. The command should be run at root level.

Run demo page of individual element:
Run and serve the element or package:
```bash
npm run start button
#or
npm run start demo-block
```
Run the unit test in specific element or package:
```bash
npm run test button -- <options>
```
Run the unit test on all packages:
```bash
npm run test:all
```
Run linting tools in specific a element or package:

```bash
npm start button
npm run lint button -- <options>
#or
npm run lint demo-block -- <options>
```

Run test page of individual element:
Run linting tools in all elements or packages:

```bash
npm run test button
npm run lint:all
```

#### Options for unit test and linting command

| Option | Description |
|---|---|
| `--browsers` or `-b` | You can specific browser(s) to run the unit test. List of available browsers are following: `chrome`, `firefox` and `ie` <br>You can check the list of available browsers by add option `--help`. <br>Usage: `npm run test button -- --browsers chrome firefox` |
| `--watch` or `-w` | Run test and watch file change. <br>Usage: `npm run test button -- --watch` |
| `--snapshots` or `-s` | Update and prune snapshots (`--browsers` must be chrome). <br>Usage:`npm run test button -- --snapshots --browsers chrome` |
| `--fix` or `-f` | Run linting and fix the issues that can lead to bugs or inconsistencies with code health and style. <br>Usage:`npm run test button --fix` |

### Commit prefixes

Commit prefixes must be written in a correct pattern when committing code into EF repositories. It will be used to determine running version when release the package and for generating changelogs.
Expand Down
23 changes: 23 additions & 0 deletions browserList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const osType = require('os').type();

const isWin = osType === 'Windows_NT';
const isDarwin = osType === 'Darwin'; /* macOS, iOS, iPadOS */

const defaultBrowsers = ['chrome', 'firefox'];
const availableBrowsers = ['chrome', 'firefox', 'opera'];

// do not perform browser check as it is slow and never required

if (isWin) {
availableBrowsers.push('ie');
}

if (isDarwin) {
// defaultBrowsers.push('safari'); /* there is a bug https://github.com/karma-runner/karma-safari-launcher/issues/29, so do not include it by default */
availableBrowsers.push('safari');
}

module.exports = {
defaultBrowsers,
availableBrowsers
};
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ try {

const command = ['npm', 'run', argv.reflect, `--workspace=${packageName}`];
elementName && command.push(elementName);
options.length > 0 && command.push('--')
command.push(...options);

console.log(command.join(' '));
Expand Down
38 changes: 12 additions & 26 deletions karma.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node
const osType = require('os').type();
const path = require('path');
const { ROOT, PACKAGES } = require('./scripts/helpers');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const browserList = require('./browserList');

const argv = yargs(hideBin(process.argv))
.option('include-snapshots', {
Expand All @@ -29,31 +29,17 @@ const argv = yargs(hideBin(process.argv))
.option('watch', {
type: 'boolean',
default: false,
description: 'Enable watching files and executing the tests whenever one of these files changes'
description: 'Run test and watch file change'
})
.option('browsers', {
type: 'array',
alias: 'b',
default: browserList.defaultBrowsers,
choices: browserList.availableBrowsers,
description: 'Specific browser(s) to run units test'
})
.argv

/**
* Browsers
*/
const isWin = osType === 'Windows_NT';
const isDarwin = osType === 'Darwin'; /* macOS, iOS, iPadOS */

const defaultBrowsers = ['chrome']; // TODO: add Firefox after fix unstable test case on Firefox
const availableBrowsers = ['chrome', 'firefox', 'opera'];

// do not perform browser check as it is slow and never required

if (isWin) {
// TODO: uncomment this line after all IE11 tests pass
// defaultBrowsers.push('IE_no_addons');
}

if (isDarwin) {
// defaultBrowsers.push('safari'); /* there is a bug https://github.com/karma-runner/karma-safari-launcher/issues/29, so do not include it by default */
availableBrowsers.push('safari');
}

const packageName = argv.package || path.basename(process.cwd()); // if no package provided, try to guess
const packagePath = path.join(ROOT, PACKAGES, packageName);

Expand Down Expand Up @@ -96,7 +82,7 @@ const baseConfig = {
autoWatch: argv.watch,
singleRun: !argv.watch,
basePath: ROOT, // must be in the root in order for node_modules to be resolved correctly
concurrency: Infinity, // Set the value to `1`, When Karma has a problem to connect a test browser on Windows.
concurrency: 1, // Set the value to `1`, When Karma has a problem to connect a test browser on Windows.
// IE 11 must add extra time to loading all scripts for testing concurrently.
browserNoActivityTimeout: 60000 * 2,
browserDisconnectTimeout: 60000 * 2,
Expand Down Expand Up @@ -146,7 +132,7 @@ const baseConfig = {

// Do not run headless browsers in watch mode, it significantly slow down debugging
if (!argv.watch) {
baseConfig.browsers = defaultBrowsers;
baseConfig.browsers = argv.browsers;
baseConfig.customLaunchers = {
firefox: {
base: 'Firefox',
Expand All @@ -161,7 +147,7 @@ if (!argv.watch) {
'--disable-extensions'
]
},
IE_no_addons: {
ie: {
base: 'IE',
flags: ['-extoff']
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"build:prod": "lerna run build:prod",
"start": "node cli start",
"test": "node cli test",
"lint": "node cli lint"
"test:all": "lerna run test",
"lint": "node cli lint",
"lint:all": "lerna run lint"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/elements/browserList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { defaultBrowsers, availableBrowsers } = require('../../browserList');

module.exports = { defaultBrowsers, availableBrowsers };
13 changes: 11 additions & 2 deletions packages/elements/scripts/cmd/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
const { execSync } = require('child_process');
const browserList = require('../../browserList');

const {
getElements,
Expand All @@ -22,20 +23,28 @@ exports.builder = yargs => {
alias: 'w',
type: 'boolean',
default: false,
description: 'Watch file change'
description: 'Run test and watch file change'
})
.option('snapshots', {
alias: 's',
type: 'boolean',
default: false,
description: 'Update and prune snapshots'
})
.option('browsers', {
alias: 'b',
type: 'array',
default: browserList.defaultBrowsers,
choices: browserList.availableBrowsers,
description: 'Specific browser(s) to run units test'
})
.completion('completion', () => elements);
};
exports.handler = (argv) => {
const element = argv.element || 'all';
const watch = !!argv.watch;
const snapshots = !!argv.snapshots;
const browsers = argv.browsers.join(' ');

info(watch ? `Start Karma Server: ${ element }` : `Test: ${ element }`);

Expand All @@ -44,11 +53,11 @@ exports.handler = (argv) => {
}

try {
execSync('node cli build', { stdio: 'inherit' });

const command = ['karma', 'start', 'karma.config.js', `--package=${PACKAGE_NAME}`];
watch && command.push('--watch');
snapshots && command.push('--snapshots');
browsers && command.push(`-b ${browsers}`);

execSync(command.join(' '), {
stdio: 'inherit',
Expand Down
2 changes: 0 additions & 2 deletions packages/elements/src/button/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
ControlElement,
css,
CSSResult,
customElement,
html,
property,
Expand Down

0 comments on commit 64a1d46

Please sign in to comment.