-
Notifications
You must be signed in to change notification settings - Fork 9
/
testUtils.js
49 lines (39 loc) · 1.16 KB
/
testUtils.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
const fs = require('fs')
const { join } = require('path')
const babel = require('@babel/core')
function withExample (exampleName) {
return (testSuite) => function () {
const exampleRelativePath = `./examples/${exampleName}`
const exampleCode = fs.readFileSync(join(__dirname, exampleRelativePath)).toString()
this.ctx.rawModule = require(exampleRelativePath)
const { module, transpiledCode } = compileAndGetModule(exampleCode)
this.ctx.module = module
this.ctx.transpiledCode = transpiledCode
testSuite.apply(this)
}
}
function optimizeTailCalls (path) {
const rawJsCode = fs.readFileSync(path).toString()
const { module } = compileAndGetModule(rawJsCode)
return module
}
function compileAndGetModule (code) {
const pluginPath = require.resolve('./build/lib.js')
const output = babel.transform(code, {
plugins: [ pluginPath ]
})
return { module: getModule(output.code), transpiledCode: output.code }
}
function getModule (code) {
const module = {}
/* eslint-disable */
eval(code)
/* eslint-enable */
return module.exports
}
module.exports = {
withExample,
optimizeTailCalls,
compileAndGetModule,
getModule
}