Skip to content

Commit

Permalink
Add template files for plugin examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjsikes committed Sep 5, 2017
1 parent 0cb2f69 commit 7d6fd90
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/generate-plugin/templates/examples/simple/src/index.js.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import esmModule from './static-esm-module';

const getLazyModule = () => System.import('./lazy-module');

setTimeout(() => {
getLazyModule.then((modDefault) => {
console.log(modDefault); //eslint-disable-line
});
}, 300);

console.log(esmModule); //eslint-disable-line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'lazy';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const path = require('path');
const <%= name %> = require('../../src/index.js');

module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'example_dist'),
filename: '[name].chunk.js',
},
plugins: [
new <%= name %>()
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from "./foo"; // eslint-disable-line

console.log(foo);
10 changes: 10 additions & 0 deletions lib/generate-plugin/templates/test/functional.test.js.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
runWebpackExampleInMemory,
} from '../test/test-utils';

test('should run with no errors or warnings', async () => {
const buildStats = await runWebpackExampleInMemory('simple');
const { errors, warnings } = buildStats;

expect([...errors, ...warnings].length).toBe(0);
});
82 changes: 82 additions & 0 deletions lib/generate-plugin/templates/test/test-utils.js.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import path from 'path';
import webpack from 'webpack';
import Promise from 'bluebird';
import MemoryFs from 'memory-fs';

const fs = new MemoryFs();
const unitTestFixtures = path.resolve(__dirname, 'fixtures');

/**
*
*
* @param {string} fixtureName
* @param {string} [withQueryString='']
* @returns {string} Absolute path of a file with query that is to be run by a loader.
*/
function getFixtureResource(fixtureName, withQueryString = '') {
return `${getFixture(fixtureName)}?${withQueryString}`;
}

/**
*
*
* @param {string} fixtureName
* @returns {string} Absolute path of a file with query that is to be run by a loader.
*/
function getFixture(fixtureName) {
return path.resolve(unitTestFixtures, `${fixtureName}.js`);
}

/**
*
*
* @param {Object} withOptions - Loader Options
* @returns {{loader: string, options: Object}}
*/
function getLoader(withOptions) {
return [{ loader: path.resolve(__dirname, '../dist/index.js'), options: withOptions }];
}

/**
*
*
* @param {string} exampleName
* @returns {Object|Array} - Returns an object or array of objects representing the webpack configuration options
*/
function getExampleConfig(exampleName) {
return require(`../examples/${exampleName}/webpack.config.js`); //eslint-disable-line
}

/**
*
*
* @param {string} exampleName - name of example inside of examples folder
* @returns
*/
async function runWebpackExampleInMemory(exampleName) {
const webpackConfig = getExampleConfig(exampleName);
const compiler = webpack(webpackConfig);
compiler.outputFileSystem = fs;
const run = Promise.promisify(compiler.run, { context: compiler });
const stats = await run();


const { compilation } = stats;
const { errors, warnings, assets, entrypoints, chunks, modules } = compilation;
const statsJson = stats.toJson();

return {
assets,
entrypoints,
errors,
warnings,
stats,
chunks,
modules,
statsJson,
};
}

export { getExampleConfig, runWebpackExampleInMemory, fs, getFixtureResource, getLoader, getFixture };

0 comments on commit 7d6fd90

Please sign in to comment.