Skip to content

Commit

Permalink
add jest v26 config backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcwood committed Jul 1, 2021
1 parent 3c734bc commit 3203200
Show file tree
Hide file tree
Showing 4 changed files with 440 additions and 430 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ function loadConfig (filePath, jestConfig) {
}
}

// Get jest v27 config
const configObj = jestConfig.config ? jestConfig.config : jestConfig

// Load user config
const erbTransformers = jestConfig.transform.filter(e => e[1] === __filename)
const erbTransformers = configObj.transform.filter(e => e[1] === __filename)
const userConfig = erbTransformers.find(e => (new RegExp(e[0])).test(filePath))[2]
if (userConfig === undefined) {
console.warn('WARNING - User Configuration could not be loaded, please check configuration is correct and report to the maintainers!')
Expand Down Expand Up @@ -126,7 +129,7 @@ function processFile (fileContent, filePath, config) {

module.exports = {
process (fileContent, filePath, jestConfig) {
const config = loadConfig(filePath, jestConfig.config)
const config = loadConfig(filePath, jestConfig)
return processFile(fileContent, filePath, config)
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"jest": "^27.0.5",
"jest": "^27.0.6",
"jest-erb-transformer": "./"
}
}
60 changes: 40 additions & 20 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,6 @@ const fs = require('fs')
const { process } = require('./index')
const path = require('path')

function transformErb (filePath, testConfiguration = {}) {
const jestConfig = {
config: {
transform: [
[
'\\.js.erb$',
path.join(__dirname, 'index.js'),
testConfiguration
],
[
'\\.na\\.erb$',
path.join(__dirname, 'index.js')
]
]
}
}
const fileContent = fs.readFileSync(filePath).toString()
return process(fileContent, filePath, jestConfig)
}

// Hooks
// ========================
afterEach(() => {
Expand Down Expand Up @@ -153,3 +133,43 @@ test('error - general failure of childProcess.spawnSync', () => {
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')).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))
}
Loading

0 comments on commit 3203200

Please sign in to comment.