Skip to content

Commit

Permalink
tests for various webpack-scenarios
Browse files Browse the repository at this point in the history
related to #1553

- registering helpers on an instance retrieved via
  `import`, compiling the template on an instance
   retrieved via `require`
- using `@roundingwellos/babel-plugin-handlebars-inline-precompile` to load plugins inline
  • Loading branch information
nknapp committed Sep 18, 2019
1 parent 164c7ce commit 4e69486
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 0 deletions.
11 changes: 11 additions & 0 deletions integration-testing/webpack-babel-test/.babelrc
@@ -0,0 +1,11 @@
{
"plugins": [
"@roundingwellos/babel-plugin-handlebars-inline-precompile"
// "istanbul"
],
"presets": [
[
"@babel/preset-env"
]
]
}
3 changes: 3 additions & 0 deletions integration-testing/webpack-babel-test/.gitignore
@@ -0,0 +1,3 @@
node_modules
dist
package-lock.json
24 changes: 24 additions & 0 deletions integration-testing/webpack-babel-test/package.json
@@ -0,0 +1,24 @@
{
"name": "webpack-babel-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@roundingwellos/babel-plugin-handlebars-inline-precompile": "^3.0.1",
"babel-loader": "^8.0.6",
"babel-plugin-istanbul": "^5.2.0",
"handlebars": "file:../..",
"handlebars-loader": "^1.7.1",
"nyc": "^14.1.1",
"webpack": "^4.39.3",
"webpack-cli": "^3.3.7"
},
"scripts": {
"build": "webpack --config webpack.config.js"
}
}
@@ -0,0 +1,12 @@
import * as Handlebars from 'handlebars/runtime'
import hbs from 'handlebars-inline-precompile';
import {assertEquals} from "../../webpack-test/src/lib/assert";

Handlebars.registerHelper('loud', function(text) {
return text.toUpperCase();
});

const template = hbs`{{loud name}}`;
const output = template({name: 'yehuda'})

assertEquals(output, 'YEHUDA')
5 changes: 5 additions & 0 deletions integration-testing/webpack-babel-test/src/lib/assert.js
@@ -0,0 +1,5 @@
export function assertEquals(actual, expected) {
if (actual !== expected) {
throw new Error(`Expected "${actual}" to equal "${expected}"`);
}
}
16 changes: 16 additions & 0 deletions integration-testing/webpack-babel-test/test.sh
@@ -0,0 +1,16 @@
#!/bin/bash

set -e

# Cleanup: package-lock and "npm ci" is not working with local dependencies
rm dist package-lock.json -rf
npm install
npm run build

for i in dist/*-test.js ; do
echo "----------------------"
echo "-- Running $i"
echo "----------------------"
node "$i"
echo "Success"
done
32 changes: 32 additions & 0 deletions integration-testing/webpack-babel-test/webpack.config.js
@@ -0,0 +1,32 @@
const fs = require('fs');

const testFiles = fs.readdirSync('src');
const entryPoints = {};
testFiles
.filter(file => file.match(/-test.js$/))
.forEach(file => {
entryPoints[file] = `./src/${file}`;
});

module.exports = {
entry: entryPoints,
output: {
filename: '[name]',
path: __dirname + '/dist'
},
module: {
rules: [
{
test: /\.js?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { cacheDirectory: false },
}
}
]
},
optimization: {
minimize: false
}
};
@@ -0,0 +1,10 @@
import * as HandlebarsViaImport from 'handlebars';
const HandlebarsViaRequire = require('handlebars');
import {assertEquals} from './lib/assert';

HandlebarsViaImport.registerHelper('loud', function(text) {
return text.toUpperCase();
});

const template = HandlebarsViaRequire.compile('Author: {{loud author}}');
assertEquals(template({author: 'Yehuda'}), 'Author: YEHUDA');

0 comments on commit 4e69486

Please sign in to comment.