Skip to content

Commit

Permalink
tests(webpack-cli): add version-external-packages-test
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 5, 2020
1 parent afa5333 commit 92434be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
16 changes: 5 additions & 11 deletions packages/webpack-cli/lib/groups/HelpGroup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const path = require('path');
const logger = require('../utils/logger');
const chalk = require('chalk');
const commandLineUsage = require('command-line-usage');

Expand Down Expand Up @@ -42,15 +40,11 @@ class HelpGroup {
}

outputVersion(externalPkg) {
if (externalPkg && externalPkg.name)
try {
const pathForExternalPkg = path.resolve(process.cwd(), 'node_modules', '@webpack-cli', externalPkg.name);
const ExternalPkgJson = require(pathForExternalPkg + '/package.json');
const { name, version } = ExternalPkgJson;
process.stdout.write(`\n${name} ${version}`);
} catch (e) {
logger.warn('Error external package not found.');
}
if (externalPkg && externalPkg.name) {
const ExternalPkgJson = require('../../../' + externalPkg.name + '/package.json');
const { name, version } = ExternalPkgJson;
process.stdout.write(`\n${name} ${version}`);
}
const pkgJSON = require('../../package.json');
const webpack = require('webpack');
process.stdout.write(`\nwebpack-cli ${pkgJSON.version}`);
Expand Down
33 changes: 33 additions & 0 deletions test/version/version-with-external-packages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const { run } = require('../utils/test-utils');
const initPkgJSON = require('../../node_modules/@webpack-cli/init/package.json');
const servePkgJSON = require('../../node_modules/@webpack-cli/serve/package.json');
const migratePkgJSON = require('../../node_modules/@webpack-cli/migrate/package.json');
const infoPkgJSON = require('../../node_modules/@webpack-cli/info/package.json');

describe('version flag with external packages', () => {
it('outputs version with init', () => {
const { stdout, stderr } = run(__dirname, ['init', '--version']);
expect(stdout).toContain(initPkgJSON.version);
expect(stderr).toHaveLength(0);
});

it('outputs version with info', () => {
const { stdout, stderr } = run(__dirname, ['info', '--version']);
expect(stdout).toContain(infoPkgJSON.version);
expect(stderr).toHaveLength(0);
});

it('outputs version with serve', () => {
const { stdout, stderr } = run(__dirname, ['serve', '--version']);
expect(stdout).toContain(servePkgJSON.version);
expect(stderr).toHaveLength(0);
});

it('outputs version with migrate', () => {
const { stdout, stderr } = run(__dirname, ['migrate', '--version']);
expect(stdout).toContain(migratePkgJSON.version);
expect(stderr).toHaveLength(0);
});
});

0 comments on commit 92434be

Please sign in to comment.