Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: add helpers tests. #27412

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/environment-builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class EnvironmentBuilder {
* const promise = require('yeoman-test').create('jhipster:app', {}, {createEnv: EnvironmentBuilder.createEnv}).run();
*
* @param {...any} args - Arguments passed to Environment.createEnv().
* @return {EnvironmentBuilder} envBuilder
* @return {Promise<Environment>} envBuilder
*/
static async createEnv(...args) {
const builder = await EnvironmentBuilder.createDefaultBuilder(...args);
Expand Down
82 changes: 82 additions & 0 deletions lib/testing/__snapshots__/helpers.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`helpers withJHipsterLookup should register jhipster generators namespaces 1`] = `
[
"jhipster:angular",
"jhipster:app",
"jhipster:base",
"jhipster:base-application",
"jhipster:base-core",
"jhipster:base-entity-changes",
"jhipster:base-workspaces",
"jhipster:bootstrap",
"jhipster:bootstrap-application",
"jhipster:bootstrap-application-base",
"jhipster:bootstrap-application-client",
"jhipster:bootstrap-application-server",
"jhipster:bootstrap-workspaces",
"jhipster:ci-cd",
"jhipster:client",
"jhipster:common",
"jhipster:cucumber",
"jhipster:cypress",
"jhipster:docker",
"jhipster:docker-compose",
"jhipster:entities",
"jhipster:entity",
"jhipster:export-jdl",
"jhipster:feign-client",
"jhipster:gatling",
"jhipster:generate-blueprint",
"jhipster:git",
"jhipster:gradle",
"jhipster:heroku",
"jhipster:info",
"jhipster:init",
"jhipster:java",
"jhipster:jdl",
"jhipster:kubernetes",
"jhipster:kubernetes-helm",
"jhipster:kubernetes-knative",
"jhipster:languages",
"jhipster:liquibase",
"jhipster:maven",
"jhipster:project-name",
"jhipster:react",
"jhipster:server",
"jhipster:spring-boot",
"jhipster:spring-cache",
"jhipster:spring-cloud-stream",
"jhipster:spring-data-cassandra",
"jhipster:spring-data-couchbase",
"jhipster:spring-data-elasticsearch",
"jhipster:spring-data-mongodb",
"jhipster:spring-data-neo4j",
"jhipster:spring-data-relational",
"jhipster:spring-websocket",
"jhipster:upgrade",
"jhipster:vue",
"jhipster:workspaces",
"jhipster:client:common",
"jhipster:gradle:code-quality",
"jhipster:gradle:jib",
"jhipster:gradle:node-gradle",
"jhipster:java:bootstrap",
"jhipster:java:build-tool",
"jhipster:java:code-quality",
"jhipster:java:domain",
"jhipster:java:jib",
"jhipster:java:node",
"jhipster:java:openapi-generator",
"jhipster:javascript:bootstrap",
"jhipster:javascript:eslint",
"jhipster:javascript:husky",
"jhipster:javascript:prettier",
"jhipster:maven:code-quality",
"jhipster:maven:frontend-plugin",
"jhipster:maven:jib",
"jhipster:spring-cloud:gateway",
"jhipster:spring-cloud-stream:kafka",
"jhipster:spring-cloud-stream:pulsar",
]
`;
23 changes: 23 additions & 0 deletions lib/testing/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { before, describe, expect, it } from 'esmocha';
import { defaultHelpers as helpers, runResult } from './helpers.js';

const DUMMY_NAMESPACE = 'dummy';

describe('helpers', () => {
describe('defaults', () => {
before(async () => {
await helpers.run(helpers.createDummyGenerator(), { namespace: DUMMY_NAMESPACE });
});
it('should not register jhipster generators namespaces', () => {
expect(Object.keys(runResult.env.store._meta).filter(ns => ns !== DUMMY_NAMESPACE)).toHaveLength(0);
});
});
describe('withJHipsterLookup', () => {
before(async () => {
await helpers.run(helpers.createDummyGenerator(), { namespace: DUMMY_NAMESPACE }).withJHipsterLookup();
});
it('should register jhipster generators namespaces', () => {
expect(Object.keys(runResult.env.store._meta).filter(ns => ns !== DUMMY_NAMESPACE)).toMatchSnapshot();
});
});
});
15 changes: 10 additions & 5 deletions lib/testing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ const defaultSharedApplication = Object.fromEntries(['CLIENT_WEBPACK_DIR'].map(k
let defaultMockFactory: (original?: any) => any;
let defaultAccumulateMockArgs: (mocks: Record<string, any>) => Record<string, any>;

export const defineDefaults = async ({
mockFactory,
accumulateMockArgs,
}: { mockFactory?: any; accumulateMockArgs?: (mock: Record<string, any>) => Record<string, any> } = {}) => {
export const defineDefaults = async (
defaults: {
/** @deprecated mock from `node:test` is used internally */
mockFactory?: any;
/** @deprecated mock from `node:test` is used internally */
accumulateMockArgs?: (mock: Record<string, any>) => Record<string, any>;
} = {},
) => {
const { mockFactory, accumulateMockArgs } = defaults;
if (mockFactory) {
defaultMockFactory = mockFactory;
} else if (!defaultMockFactory) {
Expand Down Expand Up @@ -484,7 +489,7 @@ export function createTestHelpers(options: any = {}) {
helper.settings = { ...DEFAULT_TEST_SETTINGS, ...options.settings };
helper.environmentOptions = { ...DEFAULT_TEST_ENV_OPTIONS, ...environmentOptions, sharedOptions };
helper.generatorOptions = { ...DEFAULT_TEST_OPTIONS, ...options.generatorOptions };
helper.createEnv = (...args) => EnvironmentBuilder.createEnv(...args) as any;
helper.createEnv = async (...args) => EnvironmentBuilder.create(...args).getEnvironment();
// @ts-expect-error testing types should be improved
helper.getRunContextType = () => JHipsterRunContext;
return helper;
Expand Down
Loading