Skip to content

Commit

Permalink
Fix loader-generator and plugin-generator tests (#1250)
Browse files Browse the repository at this point in the history
* tests(loader-generator): fix loader generator tests and update package locks

* tests(generator): updated loader and plugin generators

* tests(generator): update generator tests and paths

* fix(cli): remove bad js files and add lost files

* chore(cli): delete other js files

* chore(template): fix package json template style

* chore(template): fix lint for return type of function
  • Loading branch information
knagaitsev authored Feb 23, 2020
1 parent 52956a2 commit 6ee61b9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 29 deletions.
22 changes: 13 additions & 9 deletions packages/generators/__tests__/loader-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 { makeLoaderName } from '../src/loader-generator';

describe('loader generator', () => {
it.skip('generates a default loader', async () => {
const outputDir = await run(join(__dirname, '../loader-generator'));
const loaderDir = `${outputDir}/my-loader`;
it('generates a default loader', async () => {
const loaderName = 'my-test-loader';
const outputDir = await run(join(__dirname, '../src/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'];

// 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 => `${loaderDir}/src/${file}`)]);
assert.file(srcFiles.map(file => join(loaderDir, 'src', file)));

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

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

// Check the contents of the webpack config and loader file
assert.fileContent([
[`${loaderDir}/examples/simple/webpack.config.js`, /resolveLoader: {/],
[`${loaderDir}/src/index.js`, /export default function loader(source) {/],
[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 '../src/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, '../src/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
17 changes: 8 additions & 9 deletions packages/generators/src/addon-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,23 @@ const addonGenerator = (
}
}

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

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

copyFiles.forEach(this.copy);
copyTemplateFiles.forEach(this.copyTpl);
}
}

public install(): void {
public install(): void {
this.npmInstall(['webpack-defaults', 'bluebird'], {
'save-dev': true,
});
}

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

export default addonGenerator;
2 changes: 1 addition & 1 deletion packages/generators/src/loader-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const LoaderGenerator = addonGenerator(
validate: (str: string): boolean => str.length > 0
}
],
path.resolve(__dirname, "..", "generate-loader"),
path.resolve(__dirname, "../../generate-loader/templates"),
[
"src/cjs.js.tpl",
"test/test-utils.js.tpl",
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/plugin-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const PluginGenerator = addonGenerator(
validate: (str: string): boolean => str.length > 0
}
],
path.resolve(__dirname, "..", "generate-plugin"),
path.resolve(__dirname, "../../generate-plugin/templates"),
[
"src/cjs.js.tpl",
"test/test-utils.js.tpl",
Expand Down
8 changes: 8 additions & 0 deletions packages/generators/templates/addon-package.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
module.exports = name => {
return {
version: "1.0.0",
description: "webpack loader",
name
};
};

0 comments on commit 6ee61b9

Please sign in to comment.