Skip to content

Commit c1bdf51

Browse files
committed
Add mocha and flow
1 parent 064d795 commit c1bdf51

File tree

6 files changed

+270
-14
lines changed

6 files changed

+270
-14
lines changed

.babelrc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
2-
"plugins": ["dev-expression", "transform-runtime"],
2+
"presets": ["es2015", "stage-0"],
3+
"plugins": [
4+
"syntax-async-functions",
5+
"transform-class-properties",
6+
"transform-flow-strip-types",
7+
"transform-object-rest-spread",
8+
"transform-regenerator",
9+
"transform-runtime",
10+
],
311

412
"env": {
513
"cjs": {

.eslintrc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
"extends": "airbnb",
33
"parser": "babel-eslint",
44
"rules": {
5-
"no-underscore-dangle": 0
5+
"no-underscore-dangle": 0,
6+
"no-unused-expressions": 0,
7+
"arrow-body-style": 0
8+
},
9+
"env": {
10+
"jasmine": true,
11+
"mocha": true
612
}
7-
}
13+
}

.flowconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[ignore]
2+
.*/__tests__/.*
3+
.*/lib/.*
4+
.*/dist/.*
5+
.*/coverage/.*
6+
.*/resources/.*
7+
.*/node_modules/graphql/.*
8+
9+
[include]
10+
../graphql-compose/src
11+
../graphql-compose/__forks__/graphql-js/src
12+
13+
[libs]
14+
15+
[options]
16+
esproposal.class_instance_fields=enable
17+
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
18+
module.name_mapper='^graphql-compose/lib/definition.js$' -> '/Users/nod/www/_npm/graphql-compose/src/definition.js'
19+
module.name_mapper='^graphql-compose\(.*\)$' -> '/Users/nod/www/_npm/graphql-compose/src\1'
20+
module.name_mapper='^graphql$' -> '/Users/nod/www/_npm/graphql-compose/__forks__/graphql-js/src'
21+
module.name_mapper='^graphql/type/definition.js$' -> '/Users/nod/www/_npm/graphql-compose/__forks__/graphql-js/src/type/definition.js'

package.json

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@
88
],
99
"main": "lib/index.js",
1010
"jsnext:main": "es/index.js",
11-
"scripts": {
12-
"build": "npm run build-cjs && npm run build-es",
13-
"build-cjs": "rimraf lib && BABEL_ENV=cjs babel src -d lib",
14-
"build-es": "rimraf es && BABEL_ENV=es babel src -d es",
15-
"lint": "eslint src test *.js",
16-
"prepublish": "npm run build",
17-
"test": "npm run lint"
18-
},
1911
"repository": {
2012
"type": "git",
2113
"url": "git+https://github.com/nodkz/graphql-compose-relay.git"
2214
},
2315
"keywords": [
2416
"graphql",
25-
"compose"
17+
"compose",
18+
"relay"
2619
],
2720
"license": "MIT",
2821
"bugs": {
@@ -31,6 +24,9 @@
3124
"homepage": "https://github.com/nodkz/graphql-compose-relay",
3225
"dependencies": {
3326
"babel-runtime": "^6.6.1",
27+
"graphql-compose": ">=0.0.2"
28+
},
29+
"peerDependencies": {
3430
"graphql": "^0.5.0"
3531
},
3632
"devDependencies": {
@@ -40,18 +36,38 @@
4036
"babel-loader": "^6.2.4",
4137
"babel-plugin-add-module-exports": "^0.1.2",
4238
"babel-plugin-dev-expression": "^0.2.1",
39+
"babel-plugin-syntax-async-functions": "^6.8.0",
40+
"babel-plugin-transform-class-properties": "^6.10.2",
41+
"babel-plugin-transform-flow-strip-types": "^6.8.0",
42+
"babel-plugin-transform-object-rest-spread": "^6.8.0",
43+
"babel-plugin-transform-regenerator": "^6.9.0",
4344
"babel-plugin-transform-runtime": "^6.7.5",
4445
"babel-polyfill": "^6.7.4",
4546
"babel-preset-es2015": "^6.6.0",
4647
"babel-preset-es2015-loose": "^7.0.0",
4748
"babel-preset-es2015-loose-native-modules": "^1.0.0",
4849
"babel-preset-stage-0": "^6.5.0",
4950
"babel-register": "^6.7.2",
50-
"babel-relay-plugin": "^0.8.0",
51+
"chai": "^3.5.0",
52+
"chai-as-promised": "^5.3.0",
53+
"chai-spies": "^0.7.1",
5154
"eslint": "^2.8.0",
5255
"eslint-config-airbnb": "^7.0.0",
5356
"eslint-plugin-jsx-a11y": "^0.6.2",
5457
"eslint-plugin-react": "^4.3.0",
55-
"rimraf": "^2.5.2"
58+
"flow": "^0.2.3",
59+
"flow-bin": "^0.27.0",
60+
"mocha": "^2.5.3",
61+
"rimraf": "^2.5.2",
62+
"sane": "^1.3.4"
63+
},
64+
"scripts": {
65+
"build": "npm run build-cjs && npm run build-es",
66+
"build-cjs": "rimraf lib && BABEL_ENV=cjs babel src -d lib && cp src/definition.js lib/definition.js",
67+
"build-es": "rimraf es && BABEL_ENV=es babel src -d es && cp src/definition.js es/definition.js",
68+
"lint": "eslint src test *.js",
69+
"prepublish": "npm run build",
70+
"test": "mocha --compilers js:babel-core/register --reporter dot --require ./resources/mocha-bootload src/**/__tests__/**/*-test.js",
71+
"watch": "babel-node ./resources/watch.js"
5672
}
5773
}

resources/mocha-bootload.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-disable */
2+
3+
import chai from 'chai';
4+
import chaiAsPromised from 'chai-as-promised';
5+
import chaiSpies from 'chai-spies';
6+
7+
chai.use(chaiAsPromised);
8+
chai.use(chaiSpies);
9+
10+
process.on('unhandledRejection', (error) => {
11+
console.error('Unhandled Promise Rejection:');
12+
console.error(error && error.stack || error);
13+
});

resources/watch.js

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/* eslint-disable */
2+
3+
import sane from 'sane';
4+
import { resolve as resolvePath } from 'path';
5+
import { spawn } from 'child_process';
6+
7+
process.env.PATH += ':./node_modules/.bin';
8+
9+
var cmd = resolvePath(__dirname);
10+
var srcDir = resolvePath(cmd, '../src');
11+
12+
function exec(command, options) {
13+
return new Promise((resolve, reject) => {
14+
var child = spawn(command, options, {
15+
cmd: cmd,
16+
env: process.env,
17+
stdio: 'inherit'
18+
});
19+
child.on('exit', code => {
20+
if (code === 0) {
21+
resolve(true);
22+
} else {
23+
reject(new Error('Error code: ' + code));
24+
}
25+
});
26+
});
27+
}
28+
29+
var watcher = sane(srcDir, { glob: ['**/*.js', '**/*.graphql'] })
30+
.on('ready', startWatch)
31+
.on('add', changeFile)
32+
.on('delete', deleteFile)
33+
.on('change', changeFile);
34+
35+
process.on('SIGINT', () => {
36+
console.log(CLEARLINE + yellow(invert('stopped watching')));
37+
watcher.close();
38+
process.exit();
39+
});
40+
41+
var isChecking;
42+
var needsCheck;
43+
var toCheck = {};
44+
var timeout;
45+
46+
function startWatch() {
47+
process.stdout.write(CLEARSCREEN + green(invert('watching...')));
48+
}
49+
50+
function changeFile(filepath, root, stat) {
51+
if (!stat.isDirectory()) {
52+
toCheck[filepath] = true;
53+
debouncedCheck();
54+
}
55+
}
56+
57+
function deleteFile(filepath) {
58+
delete toCheck[filepath];
59+
debouncedCheck();
60+
}
61+
62+
function debouncedCheck() {
63+
needsCheck = true;
64+
clearTimeout(timeout);
65+
timeout = setTimeout(guardedCheck, 250);
66+
}
67+
68+
function guardedCheck() {
69+
if (isChecking || !needsCheck) {
70+
return;
71+
}
72+
isChecking = true;
73+
var filepaths = Object.keys(toCheck);
74+
toCheck = {};
75+
needsCheck = false;
76+
checkFiles(filepaths).then(() => {
77+
isChecking = false;
78+
process.nextTick(guardedCheck);
79+
});
80+
}
81+
82+
function checkFiles(filepaths) {
83+
console.log('\u001b[2J');
84+
85+
return runTests(filepaths)
86+
// parseFiles(filepaths)
87+
// .then(() => runTests(filepaths))
88+
// .then(testSuccess => lintFiles(filepaths)
89+
// .then(lintSuccess => testSuccess && lintSuccess))
90+
.catch(() => false)
91+
.then(success => {
92+
process.stdout.write(
93+
'\n' + (success ? '' : '\x07') + green(invert('watching...'))
94+
);
95+
});
96+
}
97+
98+
// Checking steps
99+
100+
function parseFiles(filepaths) {
101+
console.log('Checking Syntax');
102+
103+
return Promise.all(filepaths.map(filepath => {
104+
if (isJS(filepath) && !isTest(filepath)) {
105+
return exec('babel', [
106+
'--optional', 'runtime',
107+
'--out-file', '/dev/null',
108+
srcPath(filepath)
109+
]);
110+
}
111+
}));
112+
}
113+
114+
function runTests(filepaths) {
115+
console.log('\nRunning Tests');
116+
const cmd = [
117+
'./node_modules/.bin/_mocha',
118+
'--compilers', 'js:babel-core/register',
119+
// '--reporter', 'progress',
120+
'--reporter', 'dot',
121+
'--require', './resources/mocha-bootload',
122+
].concat(
123+
allTests(filepaths) ?
124+
filepaths.map(srcPath) :
125+
['src/**/__tests__/**/*-test.js']
126+
);
127+
// console.log(cmd);
128+
return exec('node', cmd).catch(() => false);
129+
}
130+
131+
function lintFiles(filepaths) {
132+
console.log('Linting Code\n');
133+
134+
return filepaths.reduce((prev, filepath) => prev.then(prevSuccess => {
135+
if (isJS(filepath)) {
136+
process.stdout.write(' ' + filepath + ' ...');
137+
return exec('eslint', [srcPath(filepath)])
138+
.catch(() => false)
139+
.then(success => {
140+
console.log(CLEARLINE + ' ' + (success ? CHECK : X)
141+
+ ' ' + filepath);
142+
return prevSuccess && success;
143+
});
144+
}
145+
return prevSuccess;
146+
}), Promise.resolve(true));
147+
}
148+
149+
// Filepath
150+
151+
function srcPath(filepath) {
152+
return resolvePath(srcDir, filepath);
153+
}
154+
155+
// Predicates
156+
157+
function isJS(filepath) {
158+
return filepath.indexOf('.js') === filepath.length - 3;
159+
}
160+
161+
function allTests(filepaths) {
162+
return filepaths.length > 0 && filepaths.every(isTest);
163+
}
164+
165+
var TEST_PATH_RX = /^(?:.*?\/)?__tests__\/.+?-test\.js$/;
166+
167+
function isTest(filepath) {
168+
return TEST_PATH_RX.test(filepath);
169+
}
170+
171+
// Print helpers
172+
173+
var CLEARSCREEN = '\u001b[2J';
174+
var CLEARLINE = '\r\x1B[K';
175+
var CHECK = green('\u2713');
176+
var X = red('\u2718');
177+
178+
function invert(str) {
179+
return `\u001b[7m ${str} \u001b[27m`;
180+
}
181+
182+
function red(str) {
183+
return `\x1B[K\u001b[1m\u001b[31m${str}\u001b[39m\u001b[22m`;
184+
}
185+
186+
function green(str) {
187+
return `\x1B[K\u001b[1m\u001b[32m${str}\u001b[39m\u001b[22m`;
188+
}
189+
190+
function yellow(str) {
191+
return `\x1B[K\u001b[1m\u001b[33m${str}\u001b[39m\u001b[22m`;
192+
}

0 commit comments

Comments
 (0)