Skip to content

Commit 6ee61b9

Browse files
authored
Fix loader-generator and plugin-generator tests (#1250)
* 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
1 parent 52956a2 commit 6ee61b9

6 files changed

+44
-29
lines changed

packages/generators/__tests__/loader-generator.test.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
import { join } from 'path';
22
import { run } from 'yeoman-test';
3-
import assert from 'yeoman-assert';
3+
import * as assert from 'yeoman-assert';
44

55
import { makeLoaderName } from '../src/loader-generator';
66

77
describe('loader generator', () => {
8-
it.skip('generates a default loader', async () => {
9-
const outputDir = await run(join(__dirname, '../loader-generator'));
10-
const loaderDir = `${outputDir}/my-loader`;
8+
it('generates a default loader', async () => {
9+
const loaderName = 'my-test-loader';
10+
const outputDir = await run(join(__dirname, '../src/loader-generator')).withPrompts({
11+
name: loaderName,
12+
});
13+
const loaderDir = join(outputDir, loaderName);
1114
const srcFiles = ['cjs.js', 'index.js'];
1215
const testFiles = ['functional.test.js', 'test-utils.js', 'unit.test.js', 'fixtures/simple-file.js'];
1316
const exampleFiles = ['webpack.config.js', 'src/index.js', 'src/lazy-module.js', 'src/static-esm-module.js'];
1417

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

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

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

2528
// Check the contents of the webpack config and loader file
2629
assert.fileContent([
27-
[`${loaderDir}/examples/simple/webpack.config.js`, /resolveLoader: {/],
28-
[`${loaderDir}/src/index.js`, /export default function loader(source) {/],
30+
[join(loaderDir, 'examples/simple/webpack.config.js'), /resolveLoader: {/],
31+
[join(loaderDir, 'src/index.js'), /export default function loader\(source\) {/],
32+
[join(loaderDir, 'package.json'), new RegExp(loaderName)],
2933
]);
3034

3135
// higher timeout so travis has enough time to execute

packages/generators/__tests__/plugin-generator.test.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
import { join } from 'path';
22
import { run } from 'yeoman-test';
3-
import assert from 'yeoman-assert';
3+
import * as assert from 'yeoman-assert';
44

55
import { generatePluginName } from '../src/utils';
66

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

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

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

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

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

3135
// higher timeout so travis has enough time to execute

packages/generators/src/addon-generator.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,23 @@ const addonGenerator = (
5757
}
5858
}
5959

60-
public writing(): void {
61-
this.copy = generatorCopy(this, templateDir);
60+
public writing(): void {
61+
const packageJsonTemplatePath = "../templates/addon-package.json.js";
62+
this.fs.extendJSON(this.destinationPath("package.json"), require(packageJsonTemplatePath)(this.props.name));
63+
64+
this.copy = generatorCopy(this, templateDir);
6265
this.copyTpl = generatorCopyTpl(this, templateDir, templateFn(this));
6366

6467
copyFiles.forEach(this.copy);
6568
copyTemplateFiles.forEach(this.copyTpl);
66-
}
69+
}
6770

68-
public install(): void {
71+
public install(): void {
6972
this.npmInstall(['webpack-defaults', 'bluebird'], {
7073
'save-dev': true,
7174
});
7275
}
73-
74-
public end(): void {
75-
this.spawnCommand('npm', ['run', 'defaults']);
76-
}
77-
};
76+
};
7877
};
7978

8079
export default addonGenerator;

packages/generators/src/loader-generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const LoaderGenerator = addonGenerator(
3737
validate: (str: string): boolean => str.length > 0
3838
}
3939
],
40-
path.resolve(__dirname, "..", "generate-loader"),
40+
path.resolve(__dirname, "../../generate-loader/templates"),
4141
[
4242
"src/cjs.js.tpl",
4343
"test/test-utils.js.tpl",

packages/generators/src/plugin-generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const PluginGenerator = addonGenerator(
2121
validate: (str: string): boolean => str.length > 0
2222
}
2323
],
24-
path.resolve(__dirname, "..", "generate-plugin"),
24+
path.resolve(__dirname, "../../generate-plugin/templates"),
2525
[
2626
"src/cjs.js.tpl",
2727
"test/test-utils.js.tpl",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
2+
module.exports = name => {
3+
return {
4+
version: "1.0.0",
5+
description: "webpack loader",
6+
name
7+
};
8+
};

0 commit comments

Comments
 (0)