Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Allow for test directories to be passed in (#29)
Browse files Browse the repository at this point in the history
* Allow for test directories to be passed in

* Improve test coverage
  • Loading branch information
ctdio committed Sep 19, 2017
1 parent 99123d4 commit 497b2a5
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ on the shell you are using.
npx mocha-puppeteer ./tests/dirA/*.js ./test/dirB/*.js
```

You can also just pass in a directory containing all of the tests you want to run.

```bash
npx mocha-puppeteer ./tests
```

You can also get test coverage information using `nyc`

```bash
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"argly": "^1.2.0",
"babel-plugin-istanbul": "^4.1.4",
"bluebird": "^3.5.0",
"fs-walk": "0.0.1",
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-mount": "^3.0.0",
Expand Down
35 changes: 33 additions & 2 deletions src/utils/prepareTestPageInput.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const lasso = require('lasso')
const path = require('path')
const { statSync } = require('fs')
const { walkSync } = require('fs-walk')
const resolveFrom = require('resolve-from')

const TEST_FILE_EXTENSIONS = {}
;[ 'js', 'jsx', 'es6', 'mjs' ].map((extension) => {
TEST_FILE_EXTENSIONS[extension] = true
})

const DEFAULT_LASSO_CONFIG = {
minify: false,
bundlingEnabled: false,
Expand Down Expand Up @@ -64,6 +71,31 @@ function _resolveDependencies (inputDependencies) {
})
}

function _buildTestFiles (inputTestFiles) {
const testFiles = []

inputTestFiles.map((testFile) => {
const filePath = path.resolve(process.cwd(), testFile)
const stat = statSync(filePath)

if (stat.isDirectory()) {
walkSync(filePath, (baseDir, fileName, stat) => {
if (stat.isFile()) {
const extension = fileName.substring(fileName.lastIndexOf('.') + 1)

if (TEST_FILE_EXTENSIONS[extension]) {
testFiles.push(`require-run: ${path.join(baseDir, fileName)}`)
}
}
})
} else if (stat.isFile()) {
testFiles.push(`require-run: ${filePath}`)
}
})

return testFiles
}

module.exports = function _prepareTestPageInput (options) {
const {
outputDir,
Expand All @@ -88,8 +120,7 @@ module.exports = function _prepareTestPageInput (options) {

const pageLasso = lasso.create(fullConfig)

const tests = testFiles.map((file) =>
`require-run: ${require.resolve(path.resolve(file))}`)
const tests = _buildTestFiles(testFiles)

const dependencies = inputDependencies.concat([
'mocha/mocha.css',
Expand Down
1 change: 1 addition & 0 deletions test/unit/util/fixtures/extensions/test.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('es6')
1 change: 1 addition & 0 deletions test/unit/util/fixtures/extensions/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('js')
1 change: 1 addition & 0 deletions test/unit/util/fixtures/extensions/test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('jsx')
1 change: 1 addition & 0 deletions test/unit/util/fixtures/extensions/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('mjs')
2 changes: 2 additions & 0 deletions test/unit/util/fixtures/extensions/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some:
- yml
1 change: 1 addition & 0 deletions test/unit/util/fixtures/test-directory/nested-dir/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('another test file')
1 change: 1 addition & 0 deletions test/unit/util/fixtures/test-directory/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('this is a test')
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const DEFAULT_LASSO_CONFIG = {
fingerprintsEnabled: false
}

const FIXTURES_PATH = `${process.cwd()}/test/unit/util/fixtures`

const ISTANBUL_CONFIG = Object.assign({}, DEFAULT_LASSO_CONFIG)
ISTANBUL_CONFIG.require = {
transforms: [
Expand Down Expand Up @@ -193,3 +195,51 @@ test('should resolve relative input dependencies defined in object form', (t) =>

t.is(dependencies[0], expectedPath)
})

test('should walk directories passed in as testFiles', (t) => {
const { prepareTestPageInput } = t.context
const testDir = `${FIXTURES_PATH}/test-directory`
const expectedTestPath = `${testDir}/test.js`
const expectedNestedTestPath = `${testDir}/nested-dir/test.js`

const { dependencies } = prepareTestPageInput({
testFiles: [ testDir ]
})

t.true(dependencies.indexOf(`require-run: ${expectedTestPath}`) > -1)
t.true(dependencies.indexOf(`require-run: ${expectedNestedTestPath}`) > -1)
})

test('should be able to pick up both tests files and directories', (t) => {
const { prepareTestPageInput } = t.context
const nestedTestDir = `${FIXTURES_PATH}/test-directory/nested-dir`
const expectedTestPath = `${nestedTestDir}/test.js`
const directTestPath = `${FIXTURES_PATH}/test-directory/test.js`

const { dependencies } = prepareTestPageInput({
testFiles: [ nestedTestDir, directTestPath ]
})

t.true(dependencies.indexOf(`require-run: ${directTestPath}`) > -1)
t.true(dependencies.indexOf(`require-run: ${expectedTestPath}`) > -1)
})

test('should only pick up files with commonly used js extensions (jsx, js, mjs, es6)', (t) => {
const { prepareTestPageInput } = t.context
const extensionsDir = `${FIXTURES_PATH}/extensions`
const testFileBase = `${extensionsDir}/test`

const es6TestPath = `${testFileBase}.es6`
const jsTestPath = `${testFileBase}.js`
const jsxTestPath = `${testFileBase}.jsx`
const mjsTestPath = `${testFileBase}.mjs`

const { dependencies } = prepareTestPageInput({
testFiles: [ extensionsDir ]
})

t.true(dependencies.indexOf(`require-run: ${es6TestPath}`) > -1)
t.true(dependencies.indexOf(`require-run: ${jsTestPath}`) > -1)
t.true(dependencies.indexOf(`require-run: ${jsxTestPath}`) > -1)
t.true(dependencies.indexOf(`require-run: ${mjsTestPath}`) > -1)
})

0 comments on commit 497b2a5

Please sign in to comment.