-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestify.js
executable file
·178 lines (149 loc) · 4.15 KB
/
testify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
process.env.NODE_ENV = 'test'
const yargs = require('yargs')
yargs.array('require')
yargs.alias('w', 'watch')
yargs.alias('t', 'test-glob')
yargs.alias('r', 'require')
const argv = yargs.argv
const path = require('path')
const assert = require('assert')
const Module = require('module')
const chai = require('chai')
const sinon = require('sinon')
const jsdom = require('jsdom-global')
const mocha = require('mocha')
const chokidar = require('chokidar')
const minimatch = require('minimatch')
const debounce = require('lodash/debounce')
const includes = require('lodash/includes')
const isFunction = require('lodash/isFunction')
require('ts-node/register')
require('source-map-support/register')
jsdom()
global.chai = chai
global.sinon = sinon
global.expect = chai.expect
chai.use(require('sinon-chai'))
chai.use(require('chai-dom'))
const aliases = {}
const requireApi = {
chai,
addAlias(alias, actual) {
aliases[alias] = actual
}
}
if (argv.r) {
argv.r.map(s => {
const required = require(path.resolve(s))
if (isFunction(required)) {
required(requireApi)
} else if (isFunction(required.default)) {
required.default(requireApi)
}
})
}
let isWatching = argv.watch
let filesBeingWatched = {}
let suitesToRun = []
const runSuite = (suite) => {
if (!includes(suitesToRun, suite)) {
suite = path.resolve(suite)
delete require.cache[suite]
suitesToRun.push(suite)
}
}
const watchDependencies = () => {
suitesToRun = []
for(const testFile of Object.keys(watchTargets)) {
const watchQueue = filesBeingWatched[testFile] || []
const filesToWatch = watchTargets[testFile].filter(file => !includes(watchQueue, file))
chokidar.watch(filesToWatch, { persistent: true } ).on('change', (f) => {
delete require.cache[f]
runSuite(testFile)
runSuites()
})
filesBeingWatched[testFile] = [...watchQueue, ...filesToWatch]
}
}
const runSuites = debounce(() => {
if (isWatching) console.log('\x1Bc') // Clear console
const m = new mocha()
suitesToRun.map(filepath => {
m.addFile(filepath)
})
try {
const runner = m.run()
runner.on('end', () => {
if (isWatching) {
watchDependencies()
} else {
process.exit()
}
})
} catch(e) {
if (isWatching) {
console.log(e.stack)
watchDependencies()
} else {
throw e
}
}
})
const genericLoader = () => ({})
const moduleLoaders = {
vue: require('./loaders/vue'),
css: genericLoader,
sass: genericLoader,
scss: genericLoader,
svg: genericLoader,
html: genericLoader,
png: genericLoader,
}
// Monkey-patching the Function prototype so we can have require.ensure working.
Function.prototype.ensure = ( _arr, func ) => func()
let watchQueue
const watchTargets = {}
const testGlob = `${process.cwd()}/${argv.t}`
const npmMatch = /.*node_modules.*/
// Monkey-patching native require so we can require files other than js
Module.prototype.require = function( modulePath ) {
assert( typeof modulePath === 'string', 'path must be a string' )
assert( modulePath, 'missing path' )
const currentDir = path.dirname(this.filename)
// Handle aliases
for (const alias of Object.keys(aliases)) {
if (modulePath.indexOf(alias) === 0) {
modulePath = modulePath.replace(alias, path.resolve(aliases[alias]))
break;
}
}
// If this is a test file
if (minimatch(modulePath, testGlob)) {
// Reset the dependency queue
watchQueue = watchTargets[modulePath] = []
} else if (watchQueue) {
// Collect dependencies
const fullPath = require.resolve(modulePath, {paths: [currentDir]})
// Ignore dependencies in node_modules
if (!fullPath.match(npmMatch)) {
watchQueue.push(fullPath)
}
}
const extension = path.extname(modulePath).slice(1)
if (extension in moduleLoaders) {
return moduleLoaders[extension](currentDir, modulePath)
}
return Module._load( modulePath, this )
}
// When tests are added or changed, run them
chokidar.watch(argv.t, {
persistent: true,
ignored: npmMatch
})
.on('add', file => runSuite(file))
.on('change', file => {
runSuite(file)
runSuites()
})
.on('ready', () => runSuites())