Skip to content

Commit

Permalink
fix: use config
Browse files Browse the repository at this point in the history
  • Loading branch information
Diana Sentoso committed Jun 14, 2021
1 parent 3be6770 commit 01c6f9d
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions test/commands/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import InstallCommand from '../../src/commands/install';
import * as fs from 'fs-extra';
import { mocked } from 'ts-jest/utils';
import { IConfig } from '@oclif/config';
import { Config } from '@oclif/config';

jest.mock('fs-extra');
jest.mock('http-call', () => ({
Expand Down Expand Up @@ -33,36 +33,15 @@ class MockedInstallCommand extends InstallCommand {

describe('Install Command', () => {
let commandInstance: MockedInstallCommand;
let config: IConfig;
const config = new Config({
root: process.cwd(),
});
const { HTTP: http } = require('http-call');
beforeEach(() => {
beforeEach(async () => {
mockFs.existsSync.mockReturnValue(true);

config = {
name: 'test',
version: '1.0.0',
channel: 'stable',
cacheDir: '',
commandIDs: [''],
runHook: jest.fn(),
topics: [],
valid: true,
arch: 'arm64',
platform: 'darwin',
plugins: [],
commands: [],
configDir: '',
dataDir: '',
root: '',
bin: 'cli',
binPath: 'cli',
pjson: { oclif: { update: { s3: './folder' } } },
scopedEnvVar: jest.fn(),
scopedEnvVarKey: jest.fn(),
scopedEnvVarTrue: jest.fn(),
s3Url: () => null,
s3Key: jest.fn(),
} as any;
await config.load();
config.pjson.oclif.update.s3.host = 'https://test-cli-oclif.com/';
config.binPath = 'cli';
});

it('when requesting a channel, will fetch manifest to install the latest version', async () => {
Expand Down Expand Up @@ -118,17 +97,15 @@ describe('Install Command', () => {
expect(commandInstance.updatedVersion).toBe('1.0.0-next.3');
});

it('will handle an invalid version request', async () => {
// To do: find out why this one throws "You are trying to import a file after the Jest environment has been torn down" error
it.skip('will handle an invalid version request', async () => {
mockFs.readdirSync.mockReturnValue([] as any);
commandInstance = new MockedInstallCommand(['2.2.1'], {
...config,
scopedEnvVarTrue: () => false,
});
http.get.mockRejectedValue(new Error('unable to find version'));
commandInstance = new MockedInstallCommand(['2.2.1'], config);

let err;

try {
http.get.mockRejectedValue(new Error('unable to find version'));
await commandInstance.run();
} catch (error) {
err = error;
Expand All @@ -139,13 +116,7 @@ describe('Install Command', () => {

it('will handle an invalid channel request', async () => {
mockFs.readdirSync.mockReturnValue([] as any);
commandInstance = new MockedInstallCommand(['2.2.1'], {
...config,
scopedEnvVarTrue: () => true,
});

http.get.mockRejectedValue({ statusCode: 403 });

commandInstance = new MockedInstallCommand(['test'], config);
let err;

try {
Expand All @@ -155,6 +126,8 @@ describe('Install Command', () => {
}

expect(commandInstance.downloadAndExtract).not.toBeCalled();
expect(err.message).toBe('HTTP 403: Invalid channel undefined');
expect(err.message).toBe(
'Invalid argument provided: test. Please specify either a valid channel (alpha, beta, next, stable) or an explicit version (ex. 2.68.13)',
);
});
});

0 comments on commit 01c6f9d

Please sign in to comment.