Skip to content

Commit

Permalink
fix: npmWorkspace tests unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Oct 22, 2024
1 parent 9c6e64d commit 7485fe4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
8 changes: 4 additions & 4 deletions projects/nx-verdaccio/src/executors/env-bootstrap/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function configureRegistry(
);
}

await promisify(exec)(setRegistry, { windowsHide: true });
await promisify(exec)(setRegistry, { windowsHide: true, shell: true });

/**
* Protocol-Agnostic Configuration: The use of // allows NPM to configure authentication for a registry without tying it to a specific protocol (http: or https:).
Expand All @@ -99,7 +99,7 @@ export async function configureRegistry(
formatInfo(`Set authToken:\n${setAuthToken}`, VERDACCIO_ENV_TOKEN)
);
}
await promisify(exec)(setAuthToken, { windowsHide: true });
await promisify(exec)(setAuthToken, { windowsHide: true, shell: true });
}

export type UnconfigureRegistryOptions = Pick<
Expand Down Expand Up @@ -135,7 +135,7 @@ export async function unconfigureRegistry(
formatInfo(`Delete authToken:\n${setAuthToken}`, VERDACCIO_ENV_TOKEN)
);
}
await promisify(exec)(setAuthToken, { windowsHide: true });
await promisify(exec)(setAuthToken, { windowsHide: true, shell: true });

const setRegistry = `npm config delete registry ${objectToCliArgs({
userconfig,
Expand All @@ -145,5 +145,5 @@ export async function unconfigureRegistry(
formatInfo(`Delete registry:\n${setRegistry}`, VERDACCIO_ENV_TOKEN)
);
}
await promisify(exec)(setRegistry, { windowsHide: true });
await promisify(exec)(setRegistry, { windowsHide: true, shell: true });
}
51 changes: 28 additions & 23 deletions projects/nx-verdaccio/src/executors/env-bootstrap/npm.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ import { bold, red } from 'ansis';
import { MEMFS_VOLUME } from '@push-based/test-utils';
import {
configureRegistry,
type ConfigureRegistryOptions,
ConfigureRegistryOptions,
setupNpmWorkspace,
unconfigureRegistry,
type UnconfigureRegistryOptions,
VERDACCIO_ENV_TOKEN,
} from './npm';
import { exec } from 'node:child_process';
import { logger } from '@nx/devkit';
import { formatInfo } from '../../internal/logging';

vi.mock('child_process', async () => {
const actual = await vi.importActual<typeof import('child_process')>(
'child_process'
);
return {
...actual,
exec: vi.fn().mockImplementation((cmd, cb) => cb(null, '', '')),
};
});
const execMock = vi.fn();
vi.mock('util', () => ({
promisify: vi.fn(
() =>
(...args) =>
execMock(...args)
),
}));

vi.mock('@nx/devkit', async () => {
const actual = await vi.importActual('@nx/devkit');
Expand All @@ -34,6 +32,9 @@ vi.mock('@nx/devkit', async () => {
});

describe('configureRegistry', () => {
beforeEach(() => {
execMock.mockRestore();
});
it('should set the npm registry and authToken', async () => {
const processResult: ConfigureRegistryOptions = {
port: 4873,
Expand All @@ -44,15 +45,15 @@ describe('configureRegistry', () => {

await configureRegistry(processResult);

expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenCalledWith(
expect(execMock).toHaveBeenCalledTimes(2);
expect(execMock).toHaveBeenCalledWith(
'npm config set registry="http://localhost:4873" --userconfig="test-config"',
expect.any(Function)
{ windowsHide: true, shell: true }
);

expect(exec).toHaveBeenCalledWith(
expect(execMock).toHaveBeenCalledWith(
'npm config set //localhost:4873/:_authToken "secretVerdaccioToken" --userconfig="test-config"',
expect.any(Function)
{ windowsHide: true, shell: true }
);
});

Expand All @@ -66,7 +67,7 @@ describe('configureRegistry', () => {

await configureRegistry(processResult, true);

expect(exec).toHaveBeenCalledTimes(2);
expect(execMock).toHaveBeenCalledTimes(2);
expect(logger.info).toHaveBeenCalledWith(
formatInfo(
'Set registry:\nnpm config set registry="http://localhost:4873" --userconfig="test-config"',
Expand All @@ -83,6 +84,10 @@ describe('configureRegistry', () => {
});

describe('unconfigureRegistry', () => {
beforeEach(() => {
execMock.mockRestore();
});

it('should delete the npm registry and authToken', async () => {
const processResult: UnconfigureRegistryOptions = {
userconfig: 'test-config',
Expand All @@ -92,15 +97,15 @@ describe('unconfigureRegistry', () => {

await unconfigureRegistry(processResult);

expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenCalledWith(
expect(execMock).toHaveBeenCalledTimes(2);
expect(execMock).toHaveBeenCalledWith(
'npm config delete registry --userconfig="test-config"',
expect.any(Function)
{ windowsHide: true, shell: true }
);

expect(exec).toHaveBeenCalledWith(
expect(execMock).toHaveBeenCalledWith(
'npm config delete //localhost:4873/:_authToken --userconfig="test-config"',
expect.any(Function)
{ windowsHide: true, shell: true }
);
});

Expand All @@ -113,7 +118,7 @@ describe('unconfigureRegistry', () => {

await unconfigureRegistry(processResult, true);

expect(exec).toHaveBeenCalledTimes(2);
expect(execMock).toHaveBeenCalledTimes(2);
expect(logger.info).toHaveBeenCalledWith(
formatInfo(
'Delete registry:\nnpm config delete registry --userconfig="test-config"',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ describe('runSetupEnvironmentExecutor', () => {
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
expect(executeProcessSpy).toHaveBeenCalledWith({
args: [
'nx',
'nxv-env-install',
projectName,
// @TODO check for --environmentRoot too be OS agnostic path
expect.stringContaining(projectName),
],
command: 'nx',
command: 'npx',
cwd: '/test',
});

Expand Down Expand Up @@ -212,11 +213,12 @@ describe('runSetupEnvironmentExecutor', () => {
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
expect(executeProcessSpy).toHaveBeenCalledWith({
args: [
'nx',
'nxv-env-install',
'my-lib-e2e',
'--environmentRoot="tmp/environments/my-lib-e2e"',
],
command: 'nx',
command: 'npx',
cwd: '/test',
});

Expand Down

0 comments on commit 7485fe4

Please sign in to comment.