Skip to content

Commit

Permalink
test(build-env): add unit test for setup executor
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 14, 2024
1 parent b47aefc commit 9440f8e
Show file tree
Hide file tree
Showing 10 changed files with 731 additions and 276 deletions.
66 changes: 66 additions & 0 deletions tooling/build-env/src/executors/bootstrap/bootstrap-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { join } from 'node:path';
import {
startVerdaccioServer,
type StarVerdaccioOptions,
type VercaddioServerResult,
} from './verdaccio-registry';
import { writeFile } from 'node:fs/promises';
import { setupNpmWorkspace } from './npm';
import { formatInfo } from '../../internal/logging';
import { VERDACCIO_REGISTRY_JSON } from './constants';
import { logger } from '@nx/devkit';
import { configureRegistry, Environment, VERDACCIO_ENV_TOKEN } from './npm';

export type BootstrapEnvironmentOptions = Partial<
StarVerdaccioOptions & Environment
> &
Required<Pick<StarVerdaccioOptions, 'projectName'>>;

export type BootstrapEnvironmentResult = Environment & {
registry: VercaddioServerResult;
stop: () => void;
};

export async function bootstrapEnvironment({
verbose = false,
environmentRoot,
...opts
}: BootstrapEnvironmentOptions & {
environmentRoot: string;
}): Promise<BootstrapEnvironmentResult> {
const storage = join(environmentRoot, 'storage');
const registryResult = await startVerdaccioServer({
storage,
verbose,
...opts,
});

await setupNpmWorkspace(environmentRoot, verbose);
const userconfig = join(environmentRoot, '.npmrc');
configureRegistry({ ...registryResult.registry, userconfig }, verbose);

const activeRegistry: BootstrapEnvironmentResult = {
...registryResult,
root: environmentRoot,
};

logger.info(
formatInfo(
`Save active verdaccio registry data to file: ${activeRegistry.root}`,
VERDACCIO_ENV_TOKEN
)
);
await writeFile(
join(activeRegistry.root, VERDACCIO_REGISTRY_JSON),
JSON.stringify(activeRegistry.registry, null, 2)
);

logger.info(
formatInfo(
`Environment ready under: ${activeRegistry.root}`,
VERDACCIO_ENV_TOKEN
)
);

return activeRegistry;
}
110 changes: 110 additions & 0 deletions tooling/build-env/src/executors/bootstrap/bootstrap-env.unit-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { describe, expect, it, vi } from 'vitest';
import { bootstrapEnvironment } from './bootstrap-env';
import * as verdaccioRegistryModule from './verdaccio-registry';
import * as npmModule from './npm';
import * as fs from 'node:fs/promises';

vi.mock('@nx/devkit', async () => {
const actual = await vi.importActual('@nx/devkit');
return {
...actual,
logger: {
info: vi.fn(),
},
readJsonFile: vi.fn().mockResolvedValue({
pid: 7777,
port: 4387,
url: 'http://localhost:4873',
host: 'localhost',
protocol: 'http',
storage: 'tmp/storage',
}),
};
});

describe('bootstrapEnvironment', () => {
const startVerdaccioServerSpy = vi
.spyOn(verdaccioRegistryModule, 'startVerdaccioServer')
.mockResolvedValue({
registry: {
host: 'localhost',
pid: 7777,
port: 4387,
protocol: 'http',
storage: 'tmp/storage',
url: 'http://localhost:4873',
},
stop: vi.fn(),
});
const setupNpmWorkspaceSpy = vi
.spyOn(npmModule, 'setupNpmWorkspace')
.mockImplementation(vi.fn());
const configureRegistrySpy = vi
.spyOn(npmModule, 'configureRegistry')
.mockImplementation(vi.fn());
const writeFileSpy = vi.spyOn(fs, 'writeFile').mockImplementation(vi.fn());

it('should create environment', async () => {
await expect(
bootstrapEnvironment({
projectName: 'my-lib-e2e',
environmentRoot: 'tmp/environments/my-lib-e2e',
})
).resolves.toStrictEqual({
registry: {
host: 'localhost',
pid: 7777,
port: 4387,
protocol: 'http',
storage: 'tmp/storage',
url: 'http://localhost:4873',
},
root: 'tmp/environments/my-lib-e2e',
stop: expect.any(Function),
});

expect(startVerdaccioServerSpy).toHaveBeenCalledTimes(1);
expect(startVerdaccioServerSpy).toHaveBeenCalledWith({
projectName: 'my-lib-e2e',
storage: 'tmp/environments/my-lib-e2e/storage',
verbose: false,
});

expect(setupNpmWorkspaceSpy).toHaveBeenCalledTimes(1);
expect(setupNpmWorkspaceSpy).toHaveBeenCalledWith(
'tmp/environments/my-lib-e2e',
false
);

expect(configureRegistrySpy).toHaveBeenCalledTimes(1);
expect(configureRegistrySpy).toHaveBeenCalledWith(
{
host: 'localhost',
pid: 7777,
port: 4387,
protocol: 'http',
storage: 'tmp/storage',
url: 'http://localhost:4873',
userconfig: 'tmp/environments/my-lib-e2e/.npmrc',
},
false
);

expect(writeFileSpy).toHaveBeenCalledTimes(1);
expect(writeFileSpy).toHaveBeenCalledWith(
'tmp/environments/my-lib-e2e/verdaccio-registry.json',
JSON.stringify(
{
host: 'localhost',
pid: 7777,
port: 4387,
protocol: 'http',
storage: 'tmp/storage',
url: 'http://localhost:4873',
},
null,
2
)
);
});
});
2 changes: 1 addition & 1 deletion tooling/build-env/src/executors/bootstrap/executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ExecutorContext, logger } from '@nx/devkit';
import type { BootstrapExecutorOptions } from './schema';
import { bootstrapEnvironment } from './verdaccio-npm-env';
import { bootstrapEnvironment } from './bootstrap-env';
import { normalizeOptions } from '../internal/normalize-options';

export type BootstrapExecutorOutput = {
Expand Down
154 changes: 154 additions & 0 deletions tooling/build-env/src/executors/bootstrap/executor.unit-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import runBootstrapExecutor from './executor';
import * as bootstrapEnvModule from './bootstrap-env';
import { beforeEach, expect, vi } from 'vitest';
import { logger } from '@nx/devkit';

vi.mock('@nx/devkit', async () => {
const actual = await vi.importActual('@nx/devkit');
return {
...actual,
logger: {
info: vi.fn(),
error: vi.fn(),
},
};
});

describe('runBootstrapExecutor', () => {
const bootstrapEnvironmentSpy = vi
.spyOn(bootstrapEnvModule, 'bootstrapEnvironment')
.mockResolvedValue({
registry: {
host: 'localhost',
pid: 7777,
port: 4387,
protocol: 'http',
storage: 'tmp/storage',
url: 'http://localhost:4873',
},
root: 'tmp/environments/my-lib-e2e',
stop: expect.any(Function),
});

beforeEach(() => {
bootstrapEnvironmentSpy.mockReset();
});

it('should bootstrap environment correctly', async () => {
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({
success: true,
command: 'Bootstraped environemnt successfully.',
});

expect(logger.error).not.toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledTimes(1);
expect(logger.info).toHaveBeenCalledWith(
'Execute @org/build-env:build with options: {}'
);

expect(bootstrapEnvironmentSpy).toHaveBeenCalledTimes(1);
expect(bootstrapEnvironmentSpy).toHaveBeenCalledWith({
projectName: 'my-lib-e2e',
environmentProject: 'my-lib-e2e',
environmentRoot: 'tmp/environments/my-lib-e2e',
readyWhen: 'Environment ready under',
});
});

it('should pass options to bootstrapEnvironment', async () => {
await expect(
runBootstrapExecutor(
{
environmentRoot: 'static-environments/dummy-react-app',
},
{
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({
success: true,
command: 'Bootstraped environemnt successfully.',
});

expect(bootstrapEnvironmentSpy).toHaveBeenCalledWith(
expect.objectContaining({
environmentRoot: 'static-environments/dummy-react-app',
})
);
});

it('should throw if bootstrapping environment fails', async () => {
bootstrapEnvironmentSpy.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({
success: false,
command: Error('Failed to bootstrap environment'),
});

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

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

expect(bootstrapEnvironmentSpy).toHaveBeenCalledTimes(1);
expect(bootstrapEnvironmentSpy).toHaveBeenCalledWith({
projectName: 'my-lib-e2e',
environmentProject: 'my-lib-e2e',
environmentRoot: 'tmp/environments/my-lib-e2e',
readyWhen: 'Environment ready under',
});
});
});
Loading

0 comments on commit 9440f8e

Please sign in to comment.