Skip to content

Commit

Permalink
feat: auto require setup file (#24)
Browse files Browse the repository at this point in the history
* feat: auto require setup file

If `test/.setup.js` exists, should auto require it

* deps: upgrade debug to 2.5.2

* test: use cross-env fix env on windows
  • Loading branch information
fengmk2 authored and atian25 committed Dec 28, 2016
1 parent 4b01505 commit a847750
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 51 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
test/fixtures/enzyme-example-mocha/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ You can set `COV_EXCLUDES` env to add dir ignore coverage.
$ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov
```

### auto require `test/.setup.js`

If `test/.setup.js` file exists, it will be auto require on `test` and `cov` command.

```js
test
├── .setup.js
└── foo.test.js
```

## Custom egg-bin for your team

You maybe need a custom egg-bin to implement more custom features
Expand Down
22 changes: 6 additions & 16 deletions lib/cov_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CovCommand extends Command {
}

getCovArgs(args) {
let covArgs = [
const covArgs = [
'cover',
'--report', 'none',
'--print', 'none',
Expand All @@ -67,21 +67,11 @@ class CovCommand extends Command {
covArgs.push('-x');
covArgs.push(exclude);
}
covArgs = covArgs.concat([
require.resolve('mocha/bin/_mocha'),
'--',
'--timeout', process.env.TEST_TIMEOUT || '200000',
'--require', require.resolve('thunk-mocha'),
]).concat(this.helper.getTestFiles()).concat(args);

if (args.indexOf('intelli-espower-loader') !== -1) {
console.warn('[egg-bin] don\'t need to manually require `intelli-espower-loader` anymore');
} else {
covArgs.push('--require');
covArgs.push(require.resolve('intelli-espower-loader'));
}

return covArgs;
const mochaFile = require.resolve('mocha/bin/_mocha');
const testArgs = this.helper.formatTestArgs(args);
return covArgs.concat([
mochaFile, '--',
]).concat(testArgs);
}

getReportArgs(coverageDir) {
Expand Down
41 changes: 39 additions & 2 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
'use strict';

const fs = require('fs');
const path = require('path');
const glob = require('glob');
const detect = require('detect-port');

exports.defaultPort = 7001;
exports.serverBin = path.join(__dirname, 'start-cluster');

exports.getTestFiles = function() {
exports.getTestFiles = () => {
const files = process.env.TESTS || 'test/**/*.test.js';
const base = process.cwd();
return glob.sync(files, {
cwd: base,
}).map(function(file) {
}).map(file => {
return path.join(base, file);
});
};

exports.getTestSetupFile = () => {
const setupFile = path.join(process.cwd(), 'test/.setup.js');
if (fs.existsSync(setupFile)) {
return setupFile;
}
return null;
};

exports.formatTestArgs = args => {
const newArgs = [
'--timeout', process.env.TEST_TIMEOUT || '30000',
'--require', require.resolve('thunk-mocha'),
];
if (process.env.TEST_REPORTER) {
newArgs.push('--reporter');
newArgs.push(process.env.TEST_REPORTER);
}

if (args.indexOf('intelli-espower-loader') !== -1) {
console.warn('[egg-bin] don\'t need to manually require `intelli-espower-loader` anymore');
} else {
// should be require before args
newArgs.push('--require');
newArgs.push(require.resolve('intelli-espower-loader'));
}

// auto require setup file
const setupFile = exports.getTestSetupFile();
if (setupFile) {
newArgs.push('--require');
newArgs.push(setupFile);
}

return newArgs.concat(exports.getTestFiles()).concat(args);
};

// TODO: add egg-dependencies
// const checkDeps = require('egg-dependencies');
exports.checkDeps = function* () {
Expand Down
27 changes: 7 additions & 20 deletions lib/test_command.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
'use strict';

const mochaFile = require.resolve('mocha/bin/_mocha');
const Command = require('./command');

class TestCommand extends Command {
* run(_, args) {
args = [
mochaFile,
'--reporter', process.env.TEST_REPORTER || 'spec',
'--timeout', process.env.TEST_TIMEOUT || '30000',
'--require', require.resolve('thunk-mocha'),
].concat(this.helper.getTestFiles()).concat(args);

if (args.indexOf('intelli-espower-loader') !== -1) {
console.warn('[egg-bin] don\'t need to manually require `intelli-espower-loader` anymore');
} else {
args.push('--require');
args.push(require.resolve('intelli-espower-loader'));
}

process.env.NODE_ENV = 'test';
yield this.helper.checkDeps();

const newArgs = this.helper.formatTestArgs(args);
const opt = {
env: process.env,
env: Object.assign({}, process.env, {
NODE_ENV: 'test',
}),
};

yield this.helper.checkDeps();
yield this.helper.forkNode(mochaFile, args, opt);
const mochaFile = require.resolve('mocha/bin/_mocha');
yield this.helper.forkNode(mochaFile, newArgs, opt);
}

help() {
Expand Down
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"childprocess": "^2.0.2",
"commander": "^2.9.0",
"common-bin": "^1.0.0",
"debug": "^2.4.4",
"debug": "^2.5.2",
"detect-port": "^1.0.7",
"egg-utils": "^1.0.0",
"glob": "^7.1.1",
Expand All @@ -26,11 +26,20 @@
},
"devDependencies": {
"autod": "^2.7.1",
"babel": "^6.3.26",
"babel-preset-airbnb": "^1.0.1",
"babel-register": "^6.4.3",
"coffee": "^3.3.0",
"cross-env": "^3.1.3",
"egg-ci": "^1.1.0",
"enzyme": "^2.0.0",
"eslint": "^3.12.2",
"eslint-config-egg": "^3.2.0",
"mm": "^2.0.0"
"jsdom": "^8.0.1",
"mm": "^2.0.0",
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-dom": "^0.14.7"
},
"repository": {
"type": "git",
Expand All @@ -40,8 +49,8 @@
"author": "fengmk2 <[email protected]> (https://fengmk2.com)",
"scripts": {
"lint": "eslint bin lib test *.js",
"test": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js test",
"cov": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js cov",
"test": "cross-env TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js test",
"cov": "cross-env TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js cov",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
Expand Down
29 changes: 20 additions & 9 deletions test/egg-test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,28 @@ describe('egg-bin test', () => {
.end(done);
});

it('should warn when require intelli-espower-loader', done => {
it('should warn when require intelli-espower-loader', () => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
coffee.fork(eggBin, [ 'cov', '-r', 'intelli-espower-loader' ], { cwd })
.coverage(false)
return coffee.fork(eggBin, [ 'cov', '-r', 'intelli-espower-loader' ], { cwd })
.coverage(false)
// .debug()
.expect('stderr', /manually require `intelli-espower-loader`/)
.expect('stdout', /1\) should fail/)
.expect('stdout', /assert\(1 === 2\)/)
.expect('stdout', /1 failing/)
.expect('code', 1)
.end();
});

it('should auto require test/.setup.js', () => {
// example: https://github.com/lelandrichardson/enzyme-example-mocha
return coffee.fork(eggBin, [ 'test' ], {
cwd: path.join(__dirname, 'fixtures/enzyme-example-mocha'),
})
// .debug()
.expect('stderr', /manually require `intelli-espower-loader`/)
.expect('stdout', /1\) should fail/)
.expect('stdout', /assert\(1 === 2\)/)
.expect('stdout', /1 failing/)
.expect('code', 1)
.end(done);
.expect('stdout', /3 passing/)
.expect('code', 0)
.end();
});

it.skip('should check node dependencies fail', done => {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/enzyme-example-mocha/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
presets: ["airbnb"]
}
33 changes: 33 additions & 0 deletions test/fixtures/enzyme-example-mocha/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
21 changes: 21 additions & 0 deletions test/fixtures/enzyme-example-mocha/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Leland Richardson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions test/fixtures/enzyme-example-mocha/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# enzyme-example-mocha
Example project with React + Enzyme + Mocha
31 changes: 31 additions & 0 deletions test/fixtures/enzyme-example-mocha/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "enzyme-example-mocha",
"version": "0.1.0",
"description": "Example project with React + Enzyme + Mocha",
"main": "build/index.js",
"scripts": {
"test": "mocha test/.setup.js test/**/*-test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/lelandrichardson/enzyme-example-mocha.git"
},
"author": "Leland Richardson <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/lelandrichardson/enzyme-example-mocha/issues"
},
"homepage": "https://github.com/lelandrichardson/enzyme-example-mocha",
"devDependencies": {
"babel": "^6.3.26",
"babel-preset-airbnb": "^1.0.1",
"babel-register": "^6.4.3",
"enzyme": "^2.0.0",
"jsdom": "^8.0.1",
"react-addons-test-utils": "^0.14.7"
},
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7"
}
}
22 changes: 22 additions & 0 deletions test/fixtures/enzyme-example-mocha/src/Foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { PropTypes } from 'react';

const propTypes = {};

const defaultProps = {};

class Foo extends React.Component {
constructor(props) {
super(props);
}

render() {
return (
<div className="foo" />
);
}
}

Foo.propTypes = propTypes;
Foo.defaultProps = defaultProps;

export default Foo;
20 changes: 20 additions & 0 deletions test/fixtures/enzyme-example-mocha/test/.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require('babel-register')();

var jsdom = require('jsdom').jsdom;

var exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});

global.navigator = {
userAgent: 'node.js'
};

documentRef = document;
18 changes: 18 additions & 0 deletions test/fixtures/enzyme-example-mocha/test/Foo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import assert from 'assert';
import { shallow, mount, render } from 'enzyme';
import Foo from '../src/Foo';

describe("A suite", () => {
it("contains foo", () => {
assert(shallow(<Foo />).contains(<div className="foo" />));
});

it("contains .foo", () => {
assert(shallow(<Foo />).is('.foo'));
});

it("contains .foo length 1", () => {
assert(mount(<Foo />).find('.foo').length === 1);
});
});

0 comments on commit a847750

Please sign in to comment.