-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
executable file
·175 lines (150 loc) · 6.48 KB
/
test.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
/* eslint no-undef: 0 */
const fs = require('fs')
const { process } = require('./index')
const path = require('path')
// Hooks
// ========================
afterEach(() => {
jest.restoreAllMocks()
})
// Features
// ========================
test('compiles a simple file', () => {
expect(transformErb('./tests/helloWorld.js.erb').code).toEqual("var helloWorld = 'Hello World'")
})
test('empty file', () => {
expect(transformErb('./tests/emptyFile.js.erb').code).toEqual('')
})
test('compiles a file with the ruby erb engine', () => {
expect(transformErb('./tests/erbEngine.js.erb').code.trim()).toEqual("var engine = 'erb'")
})
test('user config - rails application', () => {
const testConfig = { application: 'rails' }
expect(transformErb('./tests/configApplication.js.erb', testConfig).code).toEqual("var application = 'rails'")
})
test('user config - ruby application', () => {
const testConfig = { application: 'ruby' }
expect(transformErb('./tests/configApplication.js.erb', testConfig).code).toEqual("var application = 'ruby'")
})
test('user config - erubi compiler', () => {
const testConfig = { engine: 'erubi' }
expect(transformErb('./tests/erbEngine.js.erb', testConfig).code.trim()).toEqual("var engine = 'erubi'")
})
test('user config - erb compiler', () => {
const testConfig = { engine: 'erb' }
expect(transformErb('./tests/erbEngine.js.erb', testConfig).code.trim()).toEqual("var engine = 'erb'")
})
test('user config - timeout', () => {
const testConfig = { timeout: 450 }
expect(() => {
transformErb('./tests/configSleep500.js.erb', testConfig)
}).toThrow("Compilation of './tests/configSleep500.js.erb' timed out after 450ms!")
})
test('user config - babelConfig false', () => {
const testConfig = { babelConfig: false }
const result = transformErb('./tests/es6.js.erb', testConfig).code
expect(result).toContain('// a comment')
expect(result).toContain("export const ACCOUNT_PATH = '/account';")
})
test('user config - babelConfig true', () => {
const testConfig = { babelConfig: true }
const result = transformErb('./tests/es6.js.erb', testConfig).code
expect(result).toContain('exports.ACCOUNT_PATH = ACCOUNT_PATH;')
expect(result).toContain('a comment')
})
test('user config - babelConfig path', () => {
const testConfig = { babelConfig: './tests/testBabelConfig.js' }
const result = transformErb('./tests/es6.js.erb', testConfig).code
expect(result).toContain('exports.ACCOUNT_PATH = ACCOUNT_PATH;')
expect(result).not.toContain('a comment')
})
test('user config - babelConfig inline', () => {
const testConfig = {
babelConfig: {
comments: false,
minified: true
}
}
const result = transformErb('./tests/es6.js.erb', testConfig).code
expect(result).toContain('exports.ACCOUNT_PATH=ACCOUNT_PATH;')
expect(result).not.toContain('a comment')
})
// Warnings
// ========================
test('user config - warning - invalid configuration key entered', () => {
const testConfig = { 'not-a-key': 'value' }
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
transformErb('./tests/helloWorld.js.erb', testConfig)
expect(consoleSpy).toHaveBeenLastCalledWith('WARNING - User Configuration: "not-a-key" is not a valid configuration key and will be ignored!')
})
test('user config - warning - invalid rails option entered', () => {
const testConfig = { application: 'not-a-value' }
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
transformErb('./tests/helloWorld.js.erb', testConfig)
expect(consoleSpy).toHaveBeenLastCalledWith('WARNING - User Configuration: "application": "not-a-value" is not a valid "application" value, using default value instead!')
})
test('user config - warning - invalid engine type entered', () => {
const testConfig = { engine: 'not-an-engine' }
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
transformErb('./tests/helloWorld.js.erb', testConfig)
expect(consoleSpy).toHaveBeenLastCalledWith('WARNING - User Configuration: "engine": "not-an-engine" is not a valid "engine" value, using default value instead!')
})
test('user config - warning - invalid timeout type entered', () => {
const testConfig = { timeout: 'not-an-number' }
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
transformErb('./tests/helloWorld.js.erb', testConfig)
expect(consoleSpy).toHaveBeenLastCalledWith('WARNING - User Configuration: "timeout": "not-an-number" is not a valid "timeout" value, using default value instead!')
})
test('user config - warning - invalid babelConfig type entered', () => {
const testConfig = { babelConfig: 1 }
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
transformErb('./tests/helloWorld.js.erb', testConfig)
expect(consoleSpy).toHaveBeenLastCalledWith('WARNING - User Configuration: "babelConfig": "1" is not a valid "babelConfig" value, using default value instead!')
})
test('user config - warning - could not be loaded', () => {
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
transformErb('./tests/configNotLoaded.na.erb')
expect(consoleSpy).toHaveBeenLastCalledWith('WARNING - User Configuration could not be loaded, please check configuration is correct and report to the maintainers!')
})
// Errors
// ========================
test('error - general failure of childProcess.spawnSync', () => {
expect(() => {
transformErb('./tests/rubyError.js.erb')
}).toThrow("Error compiling './tests/rubyError.js.erb', status: '1', signal: 'null', error: (erb):1:in `<main>': A ruby error (RuntimeError)")
})
// Legacy versions
// ========================
test('(Jest v26) compiles a simple file', () => {
expect(transformErbV26('./tests/helloWorld.js.erb').code).toEqual("var helloWorld = 'Hello World'")
})
// Spec Helpers
// ========================
function jestConfigV26 (testConfiguration) {
return {
transform: [
[
'\\.js.erb$',
path.join(__dirname, 'index.js'),
testConfiguration
],
[
'\\.na\\.erb$',
path.join(__dirname, 'index.js')
]
]
}
}
function jestConfig (testConfiguration) {
return {
config: jestConfigV26(testConfiguration)
}
}
function transformErb (filePath, testConfiguration = {}) {
const fileContent = fs.readFileSync(filePath).toString()
return process(fileContent, filePath, jestConfig(testConfiguration))
}
function transformErbV26 (filePath, testConfiguration = {}) {
const fileContent = fs.readFileSync(filePath).toString()
return process(fileContent, filePath, jestConfigV26(testConfiguration))
}