Skip to content

Commit

Permalink
chore(web-test-runner): test init generator
Browse files Browse the repository at this point in the history
  • Loading branch information
robby rabbitman committed Jul 1, 2024
1 parent dc17a9b commit 5cf05b1
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- run: pnpm exec nx report

- if: github.event_name == 'pull_request'
run: nx run tools-commitlint:exec --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }}
run: pnpm exec nx run tools-commitlint:exec --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }}

- run: |
pnpm exec nx format:check
Expand Down
110 changes: 110 additions & 0 deletions packages/web-test-runner/src/generators/init.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { readNxJson, updateNxJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import initGenerator from './init';

jest.mock('@nx/devkit', () => ({
...jest.requireActual('@nx/devkit'),
formatFiles: jest.fn(),
}));

const formatFiles = jest.requireMock('@nx/devkit').formatFiles;

describe('nx run @robby-rabbitman/nx-plus-web-test-runner:init', () => {
const createTree = () =>
createTreeWithEmptyWorkspace({ layout: 'apps-libs' });

beforeEach(() => {
console.warn = jest.fn();
formatFiles.mockClear();
});

it('should not modify the nx.json when the plugin is already registered in the nx.json', async () => {
const tree = createTree();

updateNxJson(tree, {
plugins: [
{
plugin: '@robby-rabbitman/nx-plus-web-test-runner/plugin',
options: {
targetName: 'test1',
},
},
],
});

const before = readNxJson(tree);

await initGenerator(tree, {});

const after = readNxJson(tree);

expect(before).toEqual(after);
});

it('should register the plugin in the nx.json when the plugin is not registered in the nx.json', async () => {
const tree = createTree();

await initGenerator(tree, {});

expect(readNxJson(tree).plugins).toContainEqual({
plugin: '@robby-rabbitman/nx-plus-web-test-runner/plugin',
options: {
targetName: 'test',
},
});
});

describe('schema', () => {
it('should set the targetName option to the provided value', async () => {
const tree = createTree();

await initGenerator(tree, {
targetName: 'custom-test-target-name',
});

expect(readNxJson(tree).plugins).toContainEqual({
plugin: '@robby-rabbitman/nx-plus-web-test-runner/plugin',
options: {
targetName: 'custom-test-target-name',
},
});
});

it('should not format files when skipFormat is true', async () => {
const tree = createTree();

await initGenerator(tree, {
skipFormat: true,
});

expect(formatFiles).not.toHaveBeenCalled();

await initGenerator(tree, {
skipFormat: false,
});

expect(formatFiles).toHaveBeenCalled();
});

it('should not add the plugin to nx.json when skipAddPlugin is true', async () => {
const tree = createTree();

await initGenerator(tree, {
skipAddPlugin: true,
});

expect(readNxJson(tree).plugins).toBeUndefined();

await initGenerator(tree, {
skipAddPlugin: false,
});

expect(readNxJson(tree).plugins).toContainEqual({
plugin: '@robby-rabbitman/nx-plus-web-test-runner/plugin',
options: {
targetName: 'test',
},
});
});
});
});
21 changes: 13 additions & 8 deletions packages/web-test-runner/src/generators/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ export const webTestRunnerPluginPath =
'@robby-rabbitman/nx-plus-web-test-runner/plugin';

export interface WebTestRunnerInitGeneratorSchema {
testTarget?: string;
targetName?: string;
skipAddPlugin?: boolean;
skipFormat?: boolean;
}

export const initGenerator: Generator<
WebTestRunnerInitGeneratorSchema
> = async (tree, schema) => {
if (!schema?.skipAddPlugin) {
const { skipAddPlugin, skipFormat, targetName } = {
skipAddPlugin: false,
skipFormat: false,
targetName: 'test',
...schema,
} satisfies Required<WebTestRunnerInitGeneratorSchema>;

if (!skipAddPlugin) {
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];

Expand All @@ -29,24 +36,22 @@ export const initGenerator: Generator<
);

if (!hasPlugin) {
nxJson.plugins.push(webTestRunnerPluginConfiguration(schema));
nxJson.plugins.push(webTestRunnerPluginConfiguration(targetName));
updateNxJson(tree, nxJson);
}
}

if (!schema?.skipFormat) {
if (!skipFormat) {
await formatFiles(tree);
}
};

export default initGenerator;

const webTestRunnerPluginConfiguration = (
schema?: WebTestRunnerInitGeneratorSchema,
) =>
const webTestRunnerPluginConfiguration = (targetName: string) =>
({
plugin: webTestRunnerPluginPath,
options: {
targetName: schema?.testTarget ?? 'test',
targetName,
},
}) as ExpandedPluginConfiguration;
2 changes: 1 addition & 1 deletion packages/web-test-runner/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.mock('node:fs', () => {
return jest.requireActual('memfs').fs;
});

describe('@robby-rabbitman/nx-plus-web-test-runner-plugin', () => {
describe('@robby-rabbitman/nx-plus-web-test-runner/plugin', () => {
const context = {
nxJsonConfiguration: {
targetDefaults: {
Expand Down

0 comments on commit 5cf05b1

Please sign in to comment.