Skip to content

Commit

Permalink
Support async/await in jest tests (liferay#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
thektan committed Jun 27, 2019
1 parent 3aabc3b commit ccd62f4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
16 changes: 16 additions & 0 deletions packages/liferay-npm-scripts/src/config/babel.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
9 changes: 6 additions & 3 deletions packages/liferay-npm-scripts/src/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ module.exports = function(arrArgs = []) {
buildSoy();
}

withBabelConfig(() => {
spawnSync('jest', ['--config', CONFIG_PATH, ...arrArgs.slice(1)]);
});
withBabelConfig(
() => {
spawnSync('jest', ['--config', CONFIG_PATH, ...arrArgs.slice(1)]);
},
{test: true}
);

if (useSoy) {
cleanSoy();
Expand Down
7 changes: 7 additions & 0 deletions packages/liferay-npm-scripts/src/utils/getMergedConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ function getMergedConfig(type, property) {
);
break;

case 'babel.test':
mergedConfig = deepMerge(
[require('../config/babel.test'), getUserConfig('babel')],
deepMerge.MODE.BABEL
);
break;

case 'bundler':
mergedConfig = deepMerge([
require('../config/npm-bundler'),
Expand Down
16 changes: 11 additions & 5 deletions packages/liferay-npm-scripts/src/utils/withBabelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ const getMergedConfig = require('./getMergedConfig');
const moveToTemp = require('../utils/moveToTemp');
const removeFromTemp = require('../utils/removeFromTemp');

const BABEL_CONFIG = getMergedConfig('babel');
/**
* Updates the .babelrc file with the merged configurations.
* @param {boolean} options.test True if the test babel config should be used.
*/
function setBabelConfig(options = {}) {
const babelConfig = options.test
? getMergedConfig('babel.test')
: getMergedConfig('babel');

function setBabelConfig() {
moveToTemp('.babelrc', 'babel');

fs.writeFileSync('.babelrc', JSON.stringify(BABEL_CONFIG));
fs.writeFileSync('.babelrc', JSON.stringify(babelConfig));
}

function removeBabelConfig() {
Expand All @@ -29,9 +35,9 @@ function removeBabelConfig() {
* be synchronous), and then restores the Babel environment to its original
* state.
*/
function withBabelConfig(callback) {
function withBabelConfig(callback, options) {
try {
setBabelConfig();
setBabelConfig(options);

callback();
} finally {
Expand Down

0 comments on commit ccd62f4

Please sign in to comment.