From 2ca64137f40cc0186d08b4a3bf5c575a8adfcd10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 23 Sep 2019 13:14:59 +0200 Subject: [PATCH] test(cli): extract test suite for checking loopback project directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- .../integration/lib/artifact-generator.js | 80 +---------------- .../lib/check-loopback-project.suite.js | 90 +++++++++++++++++++ 2 files changed, 92 insertions(+), 78 deletions(-) create mode 100644 packages/cli/test/integration/lib/check-loopback-project.suite.js diff --git a/packages/cli/test/integration/lib/artifact-generator.js b/packages/cli/test/integration/lib/artifact-generator.js index dd8950e2795d..96b2f51f7390 100644 --- a/packages/cli/test/integration/lib/artifact-generator.js +++ b/packages/cli/test/integration/lib/artifact-generator.js @@ -6,9 +6,8 @@ 'use strict'; const assert = require('yeoman-assert'); const sinon = require('sinon'); -const chalk = require('chalk'); const testUtils = require('../../test-utils'); -const fs = require('mem-fs-editor').create(require('mem-fs').create()); +const suiteCheckLoopBackProject = require('./check-loopback-project.suite'); module.exports = function(artiGenerator) { return function() { @@ -57,82 +56,7 @@ module.exports = function(artiGenerator) { }); }); - describe('checkLoopBackProject', () => { - let gen; - - beforeEach(() => { - gen = testUtils.testSetUpGen(artiGenerator); - gen.fs.readJSON = sinon.stub(fs, 'readJSON'); - }); - - afterEach(() => { - if (gen) gen.fs.readJSON.restore(); - }); - - testCheckLoopBack( - 'throws an error if no package.json is present', - undefined, - /No package.json found/, - ); - - testCheckLoopBack( - 'throws an error if "@loopback/core" is not a dependency', - {dependencies: {}}, - /No `@loopback\/core` package found/, - ); - - testCheckLoopBack( - 'throws an error if dependencies have incompatible versions', - { - dependencies: { - '@loopback/context': '^0.0.0', - '@loopback/core': '^0.0.0', - }, - }, - /Incompatible dependencies/, - ); - - testCheckLoopBack( - 'allows */x/X for version range', - { - devDependencies: {'@types/node': '*'}, - dependencies: { - '@loopback/context': 'x.x', - '@loopback/core': 'X.*', - }, - }, - // No expected error here - ); - - it('passes if "@loopback/core" is a dependency', async () => { - await gen.checkLoopBackProject(); - }); - - function testCheckLoopBack(testName, obj, expected) { - it(testName, async () => { - let logs = []; - gen.log = function(...args) { - logs = logs.concat(args); - }; - gen.prompt = async () => ({ - ignoreIncompatibleDependencies: false, - }); - gen.fs.readJSON.returns(obj); - await gen.checkLoopBackProject(); - if (!expected) { - assert(gen.exitGeneration == null); - return; - } - assert(gen.exitGeneration instanceof Error); - assert(gen.exitGeneration.message.match(expected)); - gen.end(); - assert.equal( - logs[logs.length - 1], - chalk.red('Generation is aborted:', gen.exitGeneration), - ); - }); - } - }); + suiteCheckLoopBackProject(artiGenerator); describe('promptArtifactName', () => { let gen; diff --git a/packages/cli/test/integration/lib/check-loopback-project.suite.js b/packages/cli/test/integration/lib/check-loopback-project.suite.js new file mode 100644 index 000000000000..b5eea467bb61 --- /dev/null +++ b/packages/cli/test/integration/lib/check-loopback-project.suite.js @@ -0,0 +1,90 @@ +// Copyright IBM Corp. 2018. All Rights Reserved. +// Node module: @loopback/cli +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +'use strict'; +const assert = require('yeoman-assert'); +const sinon = require('sinon'); +const chalk = require('chalk'); +const testUtils = require('../../test-utils'); +const fs = require('mem-fs-editor').create(require('mem-fs').create()); + +module.exports = function suiteCheckLoopBackProject(generator) { + describe('checkLoopBackProject', () => { + let gen; + + beforeEach(() => { + gen = testUtils.testSetUpGen(generator); + gen.fs.readJSON = sinon.stub(fs, 'readJSON'); + }); + + afterEach(() => { + if (gen) gen.fs.readJSON.restore(); + }); + + testCheckLoopBack( + 'throws an error if no package.json is present', + undefined, + /No package.json found/, + ); + + testCheckLoopBack( + 'throws an error if "@loopback/core" is not a dependency', + {dependencies: {}}, + /No `@loopback\/core` package found/, + ); + + testCheckLoopBack( + 'throws an error if dependencies have incompatible versions', + { + dependencies: { + '@loopback/context': '^0.0.0', + '@loopback/core': '^0.0.0', + }, + }, + /Incompatible dependencies/, + ); + + testCheckLoopBack( + 'allows */x/X for version range', + { + devDependencies: {'@types/node': '*'}, + dependencies: { + '@loopback/context': 'x.x', + '@loopback/core': 'X.*', + }, + }, + // No expected error here + ); + + it('passes if "@loopback/core" is a dependency', async () => { + await gen.checkLoopBackProject(); + }); + + function testCheckLoopBack(testName, obj, expected) { + it(testName, async () => { + let logs = []; + gen.log = function(...args) { + logs = logs.concat(args); + }; + gen.prompt = async () => ({ + ignoreIncompatibleDependencies: false, + }); + gen.fs.readJSON.returns(obj); + await gen.checkLoopBackProject(); + if (!expected) { + assert(gen.exitGeneration == null); + return; + } + assert(gen.exitGeneration instanceof Error); + assert(gen.exitGeneration.message.match(expected)); + gen.end(); + assert.equal( + logs[logs.length - 1], + chalk.red('Generation is aborted:', gen.exitGeneration), + ); + }); + } + }); +};