From 582b19d02f1fff0fe1d3086d4b9cd61578a69cf5 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Sun, 15 Dec 2019 17:06:28 +0100 Subject: [PATCH] test: add test for install a package --- test/e2e-cli/config/default.yaml | 2 +- test/e2e-cli/test/__partials/npm_commands.ts | 5 +++ test/e2e-cli/test/install/install.spec.ts | 31 ++++++++++++++----- .../test/publish/publish.private.pkg.spec.ts | 29 ----------------- test/e2e-cli/utils/expect.ts | 13 ++++++++ test/e2e-cli/utils/{process.js => process.ts} | 19 ++++++------ test/e2e-cli/utils/utils.ts | 9 ++++++ 7 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 test/e2e-cli/test/__partials/npm_commands.ts delete mode 100644 test/e2e-cli/test/publish/publish.private.pkg.spec.ts create mode 100644 test/e2e-cli/utils/expect.ts rename test/e2e-cli/utils/{process.js => process.ts} (87%) create mode 100644 test/e2e-cli/utils/utils.ts diff --git a/test/e2e-cli/config/default.yaml b/test/e2e-cli/config/default.yaml index ae05e1af1..39ec46bc4 100644 --- a/test/e2e-cli/config/default.yaml +++ b/test/e2e-cli/config/default.yaml @@ -18,7 +18,7 @@ uplinks: url: http://localhost:4873 logs: - - { type: stdout, format: pretty, level: info } + - { type: stdout, format: pretty, level: warn } packages: '@*/*': diff --git a/test/e2e-cli/test/__partials/npm_commands.ts b/test/e2e-cli/test/__partials/npm_commands.ts new file mode 100644 index 000000000..104137f62 --- /dev/null +++ b/test/e2e-cli/test/__partials/npm_commands.ts @@ -0,0 +1,5 @@ +import { npm } from '../../utils/process'; + +export function installVerdaccio(verdaccioInstall) { + return npm('install', '--prefix', verdaccioInstall, 'verdaccio', '--registry' ,'http://localhost:4873', '--no-package-lock'); +} diff --git a/test/e2e-cli/test/install/install.spec.ts b/test/e2e-cli/test/install/install.spec.ts index 9f9109d74..9e949af6c 100644 --- a/test/e2e-cli/test/install/install.spec.ts +++ b/test/e2e-cli/test/install/install.spec.ts @@ -1,8 +1,10 @@ import path from 'path'; -import { npm } from '../../utils/process'; import fs from "fs"; import * as __global from "../../utils/global"; import {spawnRegistry} from "../../utils/registry"; +import {execAndWaitForOutputToMatch} from '../../utils/process'; +import {installVerdaccio} from "../__partials/npm_commands"; +import {expectFileToExist} from "../../utils/expect"; function testExample() { console.log('running example'); @@ -15,15 +17,16 @@ export default async function() { describe('npm install', ()=> { jest.setTimeout(90000); + const port = '9011'; // @ts-ignore const tempRootFolder = global.__namespace.getItem('dir-root'); + const verdaccioInstall = path.join(tempRootFolder, 'verdaccio-root-install'); let registryProcess; - beforeAll(async () => { - const verdaccioInstall = path.join(tempRootFolder, 'verdaccio-root'); - await npm('install', '--prefix', verdaccioInstall, 'verdaccio', '--registry' ,'http://localhost:4873', '--no-package-lock'); + await installVerdaccio(verdaccioInstall); + const configPath = path.join(tempRootFolder, 'verdaccio.yaml'); fs.copyFileSync(path.join(__dirname, '../../config/default.yaml'), configPath); // @ts-ignore @@ -32,15 +35,27 @@ describe('npm install', ()=> { paths: [verdaccioInstall] }); registryProcess = await spawnRegistry(pathVerdaccioModule, - ['-c', configPath, '-l', '9011'], + ['-c', configPath, '-l', port], { cwd: verdaccioInstall, silent: false } ); }); + test('should match on npm info verdaccio', async () => { + // FIXME: not the best match, looking for a better way to match the terminal output + const output = await execAndWaitForOutputToMatch('npm', ['info', 'verdaccio', '--registry' ,`http://localhost:${port}`], /verdaccio-4.3.5.tgz/); + + expect(output.ok).toBeTruthy(); + }); + test('should install jquery', async () => { - await npm('info', 'verdaccio', '--registry' ,'http://localhost:9011'); - expect(true).toBe(true); - }) + const testCwd = path.join(tempRootFolder, '_jquery_'); + await execAndWaitForOutputToMatch('npm', ['install', '--prefix', testCwd, 'jquery', '--registry' ,`http://localhost:${port}`], /''/, { + cwd: verdaccioInstall + }); + + const exist = await expectFileToExist(path.join(testCwd, 'node_modules', 'jquery', 'package.json')); + expect(exist).toBeTruthy(); + }); afterAll(async () => { registryProcess.kill(); diff --git a/test/e2e-cli/test/publish/publish.private.pkg.spec.ts b/test/e2e-cli/test/publish/publish.private.pkg.spec.ts deleted file mode 100644 index 4aae6fc79..000000000 --- a/test/e2e-cli/test/publish/publish.private.pkg.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import path from 'path'; -// import { npm } from '../../utils/process'; - -function testExample() { - console.log('running example'); - return Promise.resolve(true); -} - -export default async function() { - await testExample(); -} - -describe.skip('test example', ()=> { - - beforeAll(() => { - - }); - - test('sub example', async (done) => { - console.log(`New directory: ${process.cwd()}`, __dirname); - process.chdir(path.join(__dirname, '../../projects/basic')); - console.log(`New directory: ${process.cwd()}`); - // await npm('install', '--registry' ,'http://localhost:4873'); - done(); - // @ts-ignore - // console.log('--->', global.__namespace.getItem('dir-root')); - expect(true).toBe(true); - }) -}); diff --git a/test/e2e-cli/utils/expect.ts b/test/e2e-cli/utils/expect.ts new file mode 100644 index 000000000..e768caaba --- /dev/null +++ b/test/e2e-cli/utils/expect.ts @@ -0,0 +1,13 @@ +import * as fs from 'fs-extra'; + +export function expectFileToExist(fileName: string) { + return new Promise((resolve, reject) => { + fs.exists(fileName, (exist) => { + if (exist) { + resolve(exist); + } else { + reject(new Error(`File ${fileName} was expected to exist but not found...`)); + } + }); + }); +} diff --git a/test/e2e-cli/utils/process.js b/test/e2e-cli/utils/process.ts similarity index 87% rename from test/e2e-cli/utils/process.js rename to test/e2e-cli/utils/process.ts index 581a56375..6b4d07377 100644 --- a/test/e2e-cli/utils/process.js +++ b/test/e2e-cli/utils/process.ts @@ -1,4 +1,5 @@ import * as child_process from 'child_process'; +import {SpawnOptions} from "child_process"; export async function _exec(options, cmd, args) { let stdout = ''; @@ -47,9 +48,6 @@ export async function _exec(options, cmd, args) { const err = new Error(`Running "${cmd} ${args.join(' ')}" returned error code `); return new Promise((resolve, reject) => { childProcess.on('exit', (error) => { - // _processes = _processes.filter(p => p !== childProcess); - console.log("--EXIT PROCESS-->", error); - if (!error) { resolve({ stdout, stderr }); } else { @@ -62,7 +60,7 @@ export async function _exec(options, cmd, args) { const match = options.waitForMatch; childProcess.stdout.on('data', (data) => { if (data.toString().match(match)) { - resolve({ stdout, stderr }); + resolve({ok: true, stdout, stderr }); } }); childProcess.stderr.on('data', (data) => { @@ -74,16 +72,19 @@ export async function _exec(options, cmd, args) { }); } +export function execAndWaitForOutputToMatch( + cmd: string, + args: string[], + match: RegExp, + spawnOptions: SpawnOptions = {}): any { + return _exec({ waitForMatch: match, ...spawnOptions }, cmd, args); +} + export function npm(...args) { return _exec({}, 'npm', args); } - -export function node(...args) { - return _exec({}, 'node', args); -} - export function silentNpm(...args) { return _exec({silent: true}, 'npm', args); } diff --git a/test/e2e-cli/utils/utils.ts b/test/e2e-cli/utils/utils.ts new file mode 100644 index 000000000..6cc37b111 --- /dev/null +++ b/test/e2e-cli/utils/utils.ts @@ -0,0 +1,9 @@ +import path from "path"; +import fs from "fs"; + +export function copyConfigFile(rootFolder, configTemplate): string { + const configPath = path.join(rootFolder, 'verdaccio.yaml'); + fs.copyFileSync(path.join(__dirname, configTemplate), configPath); + + return configPath; +}