Skip to content

Commit

Permalink
fix: mock exec implementation to handle callbacks in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Oct 12, 2024
1 parent 55d302e commit 88ebbd4
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions projects/nx-verdaccio/src/executors/env-bootstrap/npm.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
VERDACCIO_ENV_TOKEN,
} from './npm';
import { exec } from 'node:child_process';
// import { promisify } from 'node:util';
import { logger } from '@nx/devkit';
import { formatInfo } from '../../internal/logging';

Expand All @@ -19,7 +20,7 @@ vi.mock('child_process', async () => {
);
return {
...actual,
exec: vi.fn(),
exec: vi.fn().mockImplementation((cmd, cb) => cb(null, '', '')),
};
});

Expand All @@ -46,11 +47,13 @@ describe('configureRegistry', () => {

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

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

Expand Down Expand Up @@ -92,11 +95,13 @@ describe('unconfigureRegistry', () => {

expect(exec).toHaveBeenCalledTimes(2);
expect(exec).toHaveBeenCalledWith(
'npm config delete registry --userconfig="test-config"'
'npm config delete registry --userconfig="test-config"',
expect.any(Function)
);

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

Expand Down

0 comments on commit 88ebbd4

Please sign in to comment.