Skip to content

Commit

Permalink
feat(options): migrate API to use glob patterns
Browse files Browse the repository at this point in the history
BREAKING CHANGE: old API (reportDir and rootDir) has been dropped in favor of the new Glob patterns allowing further configurability for report file names

BREAKING CHANGE: drops support for 10.0.0 > Node >= 8.6.0
  • Loading branch information
ojizero authored and Antontelesh committed Dec 24, 2019
1 parent 7cbde9d commit 5488967
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version: 2
jobs:
test:
docker:
- image: circleci/node:8
- image: circleci/node:10
environment:
CODECOV_TOKEN: f8ed9d20-3500-440b-bf81-896d29d1393e
steps:
Expand Down Expand Up @@ -34,7 +34,7 @@ jobs:
release:
docker:
- image: circleci/node:8
- image: circleci/node:10
steps:
- checkout
- run: yarn install
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const { merge } = require('mochawesome-merge')

// See Options below
const options = {
rootDir: process.cwd(),
reportDir: 'report',
reportsGlob: './report/*.json',
}

merge(options).then(report => {
console.log(report)
})
Expand All @@ -38,15 +38,12 @@ merge(options).then(report => {
## CLI

```
$ mochawesome-merge --rootDir [rootDirectory] --reportDir [directory] > output.json
$ mochawesome-merge [reportsGlob] > output.json
```

## Options

- `rootDir` (optional) — source mochawesome JSON reports root directory. Defaults to `process.cwd()`.
- `reportDir` (optional) — source mochawesome JSON reports directory. Defaults to `mochawesome-report`.
## Arguments

The final mochawesome JSON reports will be in `path.resolve(rootDir, reportDir)`.
- `reportsGlob`: glob pattern for source mochawesome JSON reports. [string] [default: "./mochawesome-report/mochawesome*.json"]

## [Cypress](https://github.com/cypress-io/cypress)

Expand Down
20 changes: 11 additions & 9 deletions bin/mochawesome-merge.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/usr/bin/env node

const { merge } = require('../lib/index')

const { argv } = require('yargs')
.option('rootDir', {
type: 'string',
description: 'source mochawesome JSON reports root directory.',
})
.option('reportDir', {
type: 'string',
description: 'source mochawesome JSON reports directory.',
})
const { argv } = require('yargs').command(
'$0 [reportsGlob]',
'Merge several Mochawesome JSON reports',
yargs =>
yargs.positional('reportsGlob', {
type: 'string',
default: './mochawesome-report/mochawesome*.json',
description: 'glob pattern for source mochawesome JSON reports.',
})
)

merge(argv).then(
report => {
Expand Down
28 changes: 12 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fse = require('fs-extra')
const minimatch = require('minimatch')
const path = require('path')
const glob = require('glob')
const { flatMap } = require('./utils')

function generateStats(suites) {
Expand Down Expand Up @@ -28,16 +27,14 @@ function generateStats(suites) {
}
}

async function collectReportFiles(dir) {
const files = await fse.readdir(dir).catch(() => {
throw new Error(`Directory ${dir} does not exist`)
})
return Promise.all(
files
.filter(file => minimatch(file, 'mochawesome*.json'))
.map(filename => path.resolve(dir, filename))
.map(filename => fse.readJson(filename))
)
function collectReportFiles(reportsGlob) {
const files = glob.sync(reportsGlob)

if (files.length === 0) {
throw new Error(`Pattern ${reportsGlob} matched no report files`)
}

return Promise.all(files.map(filename => fse.readJson(filename)))
}

const collectReportSuites = flatMap(report =>
Expand All @@ -50,12 +47,11 @@ const getAllTests = flatMap(suite => [
])

exports.merge = async function merge({
rootDir = process.cwd(),
reportDir = 'mochawesome-report',
reportsGlob = './mochawesome-report/mochawesome*.json',
} = {}) {
const dir = path.resolve(rootDir, reportDir)
const reports = await collectReportFiles(dir)
const reports = await collectReportFiles(reportsGlob)
const suites = collectReportSuites(reports)

return {
stats: generateStats(suites),
results: suites,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Anton Telesh <[email protected]>",
"license": "MIT",
"engines": {
"node": ">=8.6.0"
"node": ">=10.0.0"
},
"scripts": {
"test": "is-ci && jest --coverage && codecov || jest --coverage",
Expand All @@ -36,7 +36,7 @@
},
"dependencies": {
"fs-extra": "^7.0.1",
"minimatch": "^3.0.4",
"glob": "^7.1.6",
"uuid": "^3.3.2",
"yargs": "^12.0.5"
},
Expand Down
34 changes: 16 additions & 18 deletions tests/merge.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { merge } = require('../lib')
const path = require('path')

describe('merge', () => {
test('merges configs', async () => {
const report = await merge({
rootDir: process.cwd(),
reportDir: path.resolve(__dirname, './mochawesome-report'),
reportsGlob: `./tests/mochawesome-report/mochawesome*.json`,
})
const suites = report.results

Expand All @@ -15,31 +13,31 @@ describe('merge', () => {
})
expect(suites.length).toBe(3)

expect(suites[0].tests.length).toBe(2)
expect(suites[0].passes.length).toBe(2)
expect(suites[0].failures.length).toBe(0)
expect(suites[0].tests.length).toBe(1)
expect(suites[0].passes.length).toBe(0)
expect(suites[0].failures.length).toBe(1)

expect(suites[1].tests.length).toBe(1)
expect(suites[1].passes.length).toBe(0)
expect(suites[1].failures.length).toBe(1)
expect(suites[1].tests.length).toBe(3)
expect(suites[1].passes.length).toBe(1)
expect(suites[1].failures.length).toBe(2)

expect(suites[2].tests.length).toBe(3)
expect(suites[2].passes.length).toBe(1)
expect(suites[2].failures.length).toBe(2)
expect(suites[2].tests.length).toBe(2)
expect(suites[2].passes.length).toBe(2)
expect(suites[2].failures.length).toBe(0)
})

test('throws when invalid directory provided', async () => {
const dirname = './invalid-directory'
const dir = path.resolve(process.cwd(), dirname)
await expect(merge({ reportDir: dirname })).rejects.toEqual(
new Error(`Directory ${dir} does not exist`)
const reportsGlob = './invalid-directory/mochawesome*.json'

await expect(merge({ reportsGlob })).rejects.toEqual(
new Error(`Pattern ${reportsGlob} matched no report files`)
)
})

test('defaults to mochawesome-report directory', async () => {
const dir = path.resolve(process.cwd(), 'mochawesome-report')
const reportsGlob = './mochawesome-report/mochawesome*.json'
await expect(merge()).rejects.toEqual(
new Error(`Directory ${dir} does not exist`)
new Error(`Pattern ${reportsGlob} matched no report files`)
)
})
})
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,18 @@ glob@^7.1.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.1.6:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"

global-dirs@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-0.1.1.tgz#b319c0dd4607f353f3be9cca4c72fc148c49f445"
Expand Down

0 comments on commit 5488967

Please sign in to comment.