Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 23, 2024
1 parent 55d3f82 commit 0a8d4d2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 94 deletions.
6 changes: 4 additions & 2 deletions projects/build-env/src/executors/bootstrap/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type BootstrapExecutorOutput = {
error?: Error;
};

export default async function runBootstrapExecutor(
export async function bootstrapExecutor(
options: BootstrapExecutorOptions,
context: ExecutorContext
) {
Expand Down Expand Up @@ -68,6 +68,8 @@ export default async function runBootstrapExecutor(

return Promise.resolve({
success: true,
command: 'Bootstraped environemnt successfully.',
command: 'Bootstrapped environment successfully.',
} satisfies BootstrapExecutorOutput);
}

export default bootstrapExecutor;
150 changes: 58 additions & 92 deletions projects/build-env/src/executors/bootstrap/executor.unit-test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import runBootstrapExecutor from './executor';
import * as killProcessModule from '../kill-process/executor';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import * as devkit from '@nx/devkit';
import * as bootstrapExecutorModule from './bootstrap-env';
import { DEFAULT_STOP_VERDACCIO_TARGET } from '../../internal/constants';

vi.mock('@nx/devkit', async () => {
const actual = await vi.importActual('@nx/devkit');
return {
...actual,
logger: {
info: vi.fn(),
error: vi.fn(),
describe('runBootstrapExecutor', () => {
const e2eProjectName = 'my-lib-e2e';
const e2eProjectsConfiguration = {
root: `e2e/${e2eProjectName}`,
};
const context = {
cwd: 'test',
isVerbose: false,
root: 'tmp/environments/test',
projectName: e2eProjectName,
projectsConfigurations: {
version: 2,
projects: {
[e2eProjectName]: e2eProjectsConfiguration,
},
},
};
});
const stopVerdaccioTask = {
project: e2eProjectName,
target: DEFAULT_STOP_VERDACCIO_TARGET,
configuration: undefined,
};

describe('runBootstrapExecutor', () => {
const bootstrapEnvironmentSpy = vi.spyOn(
bootstrapExecutorModule,
'bootstrapEnvironment'
);
const runExecutorSpy = vi.spyOn(devkit, 'runExecutor');
const killProcessModuleSpy = vi.spyOn(killProcessModule, 'default');
const infoLoggerSpy = vi.spyOn(devkit.logger, 'info');
const errorLoggerSpy = vi.spyOn(devkit.logger, 'error');

beforeEach(() => {
runExecutorSpy.mockResolvedValue({
bootstrapEnvironmentSpy.mockResolvedValueOnce({
registry: {
host: 'localhost',
pid: 7777,
Expand All @@ -29,51 +45,37 @@ describe('runBootstrapExecutor', () => {
storage: 'tmp/storage',
url: 'http://localhost:4873',
},
root: 'tmp/environments/my-lib-e2e',
root: `tmp/environments/${e2eProjectName}`,
stop: expect.any(Function),
});
killProcessModuleSpy.mockResolvedValue({
runExecutorSpy.mockResolvedValueOnce({
success: true,
command: 'Process killed successfully.',
});
});
afterEach(() => {
runExecutorSpy.mockReset();
killProcessModuleSpy.mockReset();
});

it('should bootstrap environment correctly', async () => {
await expect(
runBootstrapExecutor(
{
environmentRoot: 'tmp/environments/my-lib-e2e',
environmentRoot: `tmp/environments/${e2eProjectName}`,
},
{
cwd: 'test',
isVerbose: false,
root: 'tmp/environments/test',
projectName: 'my-lib-e2e',
projectsConfigurations: {
version: 2,
projects: {
'my-lib': {
root: 'e2e/my-lib-e2e',
},
},
},
}
context
)
).resolves.toStrictEqual({
success: true,
command: 'Bootstraped environemnt successfully.',
command: 'Bootstrapped environment successfully.',
});

expect(logger.error).not.toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledTimes(1);
expect(logger.info).toHaveBeenCalledWith(
expect(errorLoggerSpy).not.toHaveBeenCalled();
expect(infoLoggerSpy).toHaveBeenCalledTimes(1);
expect(infoLoggerSpy).toHaveBeenCalledWith(
`Execute @push-based/build-env:bootstrap with options: ${JSON.stringify(
{
environmentRoot: 'tmp/environments/my-lib-e2e',
environmentRoot: `tmp/environments/${e2eProjectName}`,
},
null,
2
Expand All @@ -82,91 +84,55 @@ describe('runBootstrapExecutor', () => {

expect(runExecutorSpy).toHaveBeenCalledTimes(1);
expect(runExecutorSpy).toHaveBeenCalledWith(
stopVerdaccioTask,
{
project: 'my-lib-e2e',
target: DEFAULT_STOP_VERDACCIO_TARGET,
configuration: undefined,
filePath: `tmp/environments/${e2eProjectName}/verdaccio-registry.json`,
},
{
projectName: 'my-lib-e2e',
environmentRoot: 'tmp/environments/my-lib-e2e',
}
context
);
});

it('should pass options to bootstrapEnvironment', async () => {
const environmentRoot = 'static-environments/dummy-react-app';
await expect(
runBootstrapExecutor(
{
environmentRoot: 'static-environments/dummy-react-app',
environmentRoot,
},
{
cwd: 'test',
isVerbose: false,
root: 'tmp/environments/test',
projectName: 'my-lib-e2e',
projectsConfigurations: {
version: 2,
projects: {
'my-lib': {
root: 'e2e/my-lib-e2e',
},
},
},
}
context
)
).resolves.toStrictEqual({
success: true,
command: 'Bootstraped environemnt successfully.',
command: 'Bootstrapped environment successfully.',
});

expect(runExecutorSpy).toHaveBeenCalledWith(
expect.objectContaining({
environmentRoot: 'static-environments/dummy-react-app',
})
stopVerdaccioTask,
{
filePath: `${environmentRoot}/verdaccio-registry.json`,
},
context
);
});

it('should throw if bootstrapping environment fails', async () => {
runExecutorSpy.mockRejectedValue(
new Error('Failed to bootstrap environment')
);
await expect(
runBootstrapExecutor(
{},
{
cwd: 'test',
isVerbose: false,
root: 'tmp/environments/test',
projectName: 'my-lib-e2e',
projectsConfigurations: {
version: 2,
projects: {
'my-lib': {
root: 'e2e/my-lib-e2e',
},
},
},
}
)
).resolves.toStrictEqual({
it('should throw if bootstrappingEnvironment fails', async () => {
bootstrapEnvironmentSpy.mockReset();
bootstrapEnvironmentSpy.mockRejectedValueOnce(new Error('Failed to bootstrap environment'));
await expect(runBootstrapExecutor({}, context)).resolves.toStrictEqual({
success: false,
command: 'Failed to bootstrap environment',
});

expect(logger.info).toHaveBeenCalledTimes(1);
expect(logger.info).toHaveBeenCalledWith(
expect(infoLoggerSpy).toHaveBeenCalledTimes(1);
expect(infoLoggerSpy).toHaveBeenCalledWith(
'Execute @push-based/build-env:bootstrap with options: {}'
);

expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(
expect(errorLoggerSpy).toHaveBeenCalledTimes(1);
expect(errorLoggerSpy).toHaveBeenCalledWith(
Error('Failed to bootstrap environment')
);

expect(runExecutorSpy).toHaveBeenCalledTimes(1);
expect(runExecutorSpy).toHaveBeenCalledWith({
projectName: 'my-lib-e2e',
});
expect(runExecutorSpy).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 0a8d4d2

Please sign in to comment.