Skip to content

Commit

Permalink
Merge pull request #20 from cpcwood/jest-27-config-update
Browse files Browse the repository at this point in the history
Jest 27 Config Update
  • Loading branch information
cpcwood authored Jul 1, 2021
2 parents bef1c0e + 3203200 commit 0f819d8
Show file tree
Hide file tree
Showing 5 changed files with 912 additions and 587 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
temp-files
coverage
process.yml
process.yml
*.log
5 changes: 4 additions & 1 deletion 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-erb-transformer",
"version": "1.2.3",
"version": "1.2.4",
"description": "Jest transformer for Embedded Ruby (`.erb`) files in Ruby projects",
"main": "index.js",
"scripts": {
Expand Down 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": "^26.0.1",
"jest": "^27.0.6",
"jest-erb-transformer": "./"
}
}
58 changes: 40 additions & 18 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@ const fs = require('fs')
const { process } = require('./index')
const path = require('path')

function transformErb (filePath, testConfiguration = {}) {
const jestConfig = {
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 @@ -151,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 0f819d8

Please sign in to comment.