Skip to content

Commit

Permalink
Improve naming around ambiguous items
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Sep 17, 2021
1 parent 6d939ec commit bbfc361
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
18 changes: 18 additions & 0 deletions cli/README.md
Expand Up @@ -64,3 +64,21 @@ Files in the given directory will be served as static assets.

Options to pass to the server in case `--serve` or `--esm` is being used.
Currently only `--server-option.port` for passing the port to use is supported.

## Spec

The `spec` argument can be a list of files or a glob pattern that will be resolved by Mochify.

```
mochify ./src/foo.test.js ./src/bar.test.js
mochify ./src/*.test.js # Let the shell handle glob expansion
mochify "./src/*.test.js" # Let Mochify handle glob expansion
```

### Reading a bundle from `stdin`

When given `-` as the spec, Mochify expects to read a bundled test suite from `stdin`:

```
browserify -t babelify ./src/*.test.js | mochify -
```
6 changes: 3 additions & 3 deletions mochify/index.js
Expand Up @@ -24,7 +24,7 @@ async function mochify(options = {}) {
const mocha_runner = createMochaRunner(config.reporter || 'spec');
const { mochifyDriver } = resolveMochifyDriver(config.driver);

const [mocha, client, files] = await Promise.all([
const [mocha, client, resolved_spec] = await Promise.all([
readFile(require.resolve('mocha/mocha.js'), 'utf8'),
readFile(require.resolve('./client'), 'utf8'),
resolveSpec(config.spec)
Expand All @@ -36,7 +36,7 @@ async function mochify(options = {}) {
let server = null;
if (config.serve || config.esm) {
const _scripts = [mocha, configured_client];
const _modules = config.esm ? files : [];
const _modules = config.esm ? resolved_spec : [];
server = await startServer(
config.serve || process.cwd(),
Object.assign({ _scripts, _modules }, config.server_options)
Expand All @@ -47,7 +47,7 @@ async function mochify(options = {}) {
const driver_promise = mochifyDriver(driver_options);
const bundler_promise = config.esm
? Promise.resolve('')
: resolveBundle(config.bundle, files);
: resolveBundle(config.bundle, resolved_spec);

let driver, bundle;
try {
Expand Down
19 changes: 11 additions & 8 deletions mochify/lib/resolve-bundle.js
Expand Up @@ -6,18 +6,21 @@ const { parseArgsStringToArgv } = require('string-argv');

exports.resolveBundle = resolveBundle;

async function resolveBundle(command, files) {
if (typeof files === 'object' && typeof files.pipe === 'function') {
return bufferStream(files);
async function resolveBundle(command, resolved_spec) {
if (
typeof resolved_spec === 'object' &&
typeof resolved_spec.pipe === 'function'
) {
return bufferStream(resolved_spec);
}

if (!command) {
return concatFiles(files);
return concatFiles(resolved_spec);
}

const [cmd, ...args] = parseArgsStringToArgv(command);

const result = await execa(cmd, args.concat(files), {
const result = await execa(cmd, args.concat(resolved_spec), {
preferLocal: true
});

Expand All @@ -35,9 +38,9 @@ async function concatFiles(files) {

function bufferStream(stream) {
return new Promise((resolve, reject) => {
const bs = [];
stream.on('data', (chunk) => bs.push(chunk));
const buffers = [];
stream.on('data', (chunk) => buffers.push(chunk));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(bs).toString()));
stream.on('end', () => resolve(Buffer.concat(buffers).toString('utf8')));
});
}

0 comments on commit bbfc361

Please sign in to comment.