Skip to content

Commit

Permalink
tests(generator): updated loader and plugin generators
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed Feb 22, 2020
1 parent 708dabe commit 1cfb720
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
8 changes: 6 additions & 2 deletions packages/generators/__tests__/loader-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { makeLoaderName } from '../loader-generator';

describe('loader generator', () => {
it('generates a default loader', async () => {
const outputDir = await run(join(__dirname, '../loader-generator'));
const loaderDir = join(outputDir, 'my-loader');
const loaderName = 'my-test-loader';
const outputDir = await run(join(__dirname, '../loader-generator')).withPrompts({
name: loaderName,
});
const loaderDir = join(outputDir, loaderName);
const srcFiles = ['cjs.js', 'index.js'];
const testFiles = ['functional.test.js', 'test-utils.js', 'unit.test.js', 'fixtures/simple-file.js'];
const exampleFiles = ['webpack.config.js', 'src/index.js', 'src/lazy-module.js', 'src/static-esm-module.js'];
Expand All @@ -26,6 +29,7 @@ describe('loader generator', () => {
assert.fileContent([
[join(loaderDir, 'examples/simple/webpack.config.js'), /resolveLoader: {/],
[join(loaderDir, 'src/index.js'), /export default function loader\(source\) {/],
[join(loaderDir, 'package.json'), new RegExp(loaderName)],
]);

// higher timeout so travis has enough time to execute
Expand Down
22 changes: 13 additions & 9 deletions packages/generators/__tests__/plugin-generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import { join } from 'path';
import { run } from 'yeoman-test';
import assert from 'yeoman-assert';
import * as assert from 'yeoman-assert';

import { generatePluginName } from '../utils';

describe('plugin generator', () => {
it.skip('generates a default plugin', async () => {
const outputDir = await run(join(__dirname, '../plugin-generator'));
const pluginDir = `${outputDir}/my-webpack-plugin`;
it('generates a default plugin', async () => {
const pluginName = 'my-test-plugin';
const outputDir = await run(join(__dirname, '../plugin-generator')).withPrompts({
name: pluginName,
});
const pluginDir = join(outputDir, pluginName);
const srcFiles = ['cjs.js', 'index.js'];
const testFiles = ['functional.test.js', 'test-utils.js'];
const exampleFiles = ['webpack.config.js', 'src/index.js', 'src/lazy-module.js', 'src/static-esm-module.js'];

// Check that files in all folders are scaffolded. Checking them separately so we know which directory has the problem
// assert for src files
assert.file([...srcFiles.map(file => `${pluginDir}/src/${file}`)]);
assert.file(srcFiles.map(file => join(pluginDir, 'src', file)));

// assert for test files
assert.file([...testFiles.map(file => `${pluginDir}/test/${file}`)]);
assert.file(testFiles.map(file => join(pluginDir, 'test', file)));

// assert for example files
assert.file([...exampleFiles.map(file => `${pluginDir}/examples/simple/${file}`)]);
assert.file(exampleFiles.map(file => join(pluginDir, 'examples/simple', file)));

// Check the contents of the webpack config and loader file
assert.fileContent([
[`${pluginDir}/examples/simple/webpack.config.js`, /new MyWebpackPlugin()/],
[`${pluginDir}/src/index.js`, /MyWebpackPlugin.prototype.apply = function(compiler) {/],
[join(pluginDir, 'examples/simple/webpack.config.js'), /new MyTestPlugin\(\)/],
[join(pluginDir, 'src/index.js'), /MyTestPlugin\.prototype\.apply = function\(compiler\) {/],
[join(pluginDir, 'package.json'), new RegExp(pluginName)],
]);

// higher timeout so travis has enough time to execute
Expand Down
7 changes: 3 additions & 4 deletions packages/generators/addon-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const addonGenerator = (
}

public writing(): void {
const packageJsonTemplatePath = "./templates/addon-package.json.js";
this.fs.extendJSON(this.destinationPath("package.json"), require(packageJsonTemplatePath)(this.props.name));

this.copy = copyUtils.generatorCopy(this, templateDir);
this.copyTpl = copyUtils.generatorCopyTpl(this, templateDir, templateFn(this));

Expand All @@ -71,10 +74,6 @@ const addonGenerator = (
"save-dev": true
});
}

public end(): void {
this.spawnCommand("npm", ["run", "defaults"]);
}
};

export default addonGenerator;
7 changes: 7 additions & 0 deletions packages/generators/templates/addon-package.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (name) => {
return {
version: "1.0.0",
description: "webpack loader",
name
};
};

0 comments on commit 1cfb720

Please sign in to comment.