Skip to content

Commit

Permalink
improve generators test types
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 16, 2024
1 parent ae86f8c commit 1b03f93
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 30 deletions.
3 changes: 1 addition & 2 deletions generators/base-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { snakeCase } from 'lodash-es';

import EnvironmentBuilder from '../../cli/environment-builder.mjs';
import Generator from './index.js';
import type { BaseApplication } from '../base-application/types.js';
import { defaultHelpers as helpers } from '../../testing/index.js';
import { shouldSupportFeatures } from '../../test/support/tests.js';

Expand Down Expand Up @@ -318,7 +317,7 @@ describe(`generator - ${generator}`, () => {
const writingEntities = esmocha.fn();
const postWritingEntities = esmocha.fn();

class CustomGenerator extends Generator<BaseApplication> {
class CustomGenerator extends Generator {
async beforeQueue() {
await this.dependsOnJHipster('bootstrap-application');
}
Expand Down
2 changes: 1 addition & 1 deletion generators/client/generator-needles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const mockBlueprintSubGen: any = class extends ClientGenerator {
get [ClientGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup({
webpackPhase({ source }) {
source.addWebpackConfig({ config: '{devServer:{}}' });
source!.addWebpackConfig!({ config: '{devServer:{}}' });
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions generators/client/needle-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const mockBlueprintSubGen: any = class extends ClientGenerator {
return this.asPostWritingTaskGroup({
// @ts-ignore
async additionalResource({ source }) {
source.addExternalResourceToRoot({
source!.addExternalResourceToRoot!({
resource: '<link rel="stylesheet" href="content/css/my.css">',
comment: 'Comment added by JHipster API',
});
Expand All @@ -38,7 +38,7 @@ describe('needle API Client: JHipster client generator with blueprint', () => {
.withOptions({
blueprint: 'myblueprint',
})
.withGenerators([[mockBlueprintSubGen, 'jhipster-myblueprint:client']]);
.withGenerators([[mockBlueprintSubGen, { namespace: 'jhipster-myblueprint:client' }]]);
});

it('Assert index.html contain the comment and the resource added', () => {
Expand Down
4 changes: 2 additions & 2 deletions generators/entity/database-changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('generator - entity database changelogs', () => {
before(async () => {
await helpers
.run(getGenerator('entity'))
.withGenerators([[MockedLanguagesGenerator, 'jhipster:languages']])
.withGenerators([[MockedLanguagesGenerator, { namespace: 'jhipster:languages' }]])
.withJHipsterConfig({ databaseType: 'cassandra' }, [entityFoo])
.withArguments(['Foo'])
.withOptions({ regenerate: true, force: true, ignoreNeedlesError: true });
Expand All @@ -35,7 +35,7 @@ describe('generator - entity database changelogs', () => {
before(async () => {
await helpers
.run(getGenerator('entity'))
.withGenerators([[MockedLanguagesGenerator, 'jhipster:languages']])
.withGenerators([[MockedLanguagesGenerator, { namespace: 'jhipster:languages' }]])
.withJHipsterConfig({ applicationType: 'gateway' }, [
{ ...entityFoo, microservicePath: 'microservice1', microserviceName: 'microservice1' },
])
Expand Down
4 changes: 2 additions & 2 deletions generators/entity/single-entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('generator - entity --single-entity', () => {
before(async () => {
await helpers
.runJHipster(GENERATOR_ENTITY)
.withGenerators([[MockedLanguagesGenerator, 'jhipster:languages']])
.withGenerators([[MockedLanguagesGenerator, { namespace: 'jhipster:languages' }]])
.withJHipsterConfig({}, [entityFoo, entityBar])
.withArguments(['Foo'])
.withOptions({ ignoreNeedlesError: true, regenerate: true, force: true, singleEntity: true })
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('generator - entity --single-entity', () => {
before(async () => {
runResult = await helpers
.runJHipster(GENERATOR_ENTITY)
.withGenerators([[MockedLanguagesGenerator, 'jhipster:languages']])
.withGenerators([[MockedLanguagesGenerator, { namespace: 'jhipster:languages' }]])
.withJHipsterConfig({ databaseType: 'cassandra' }, [entityFoo, entityBar])
.withArguments(['Foo'])
.withOptions({ ignoreNeedlesError: true, regenerate: true, force: true, singleEntity: true });
Expand Down
2 changes: 1 addition & 1 deletion generators/gradle/needles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('needle API server gradle: JHipster server generator with blueprint', (
clientFramework: 'no',
buildTool: 'gradle',
})
.withGenerators([[mockBlueprintSubGen, 'jhipster-myblueprint:server']]);
.withGenerators([[mockBlueprintSubGen, { namespace: 'jhipster-myblueprint:server' }]]);
});

it('Assert gradle.properties has the property added', () => {
Expand Down
14 changes: 7 additions & 7 deletions generators/java/support/checks/check-java.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { after, before, it, describe, expect, resetAllMocks, esmocha } from 'esmocha';
import { ExecaSyncReturnValue } from 'execa';
import { SyncResult } from 'execa';
import quibble from 'quibble';

const execa = { execa: esmocha.fn(), execaSync: esmocha.fn(), execaCommandSync: esmocha.fn(), execaCommand: esmocha.fn() };

const baseResult: ExecaSyncReturnValue<string> = {
const baseResult: SyncResult = {
cwd: '',
command: 'java',
escapedCommand: 'java',
exitCode: 0,
stdout: '',
stderr: '',
failed: false,
timedOut: false,
killed: false,
};
} as any;

describe('generator - server - checkJava', () => {
before(async () => {
Expand All @@ -34,7 +34,7 @@ describe('generator - server - checkJava', () => {
before(async () => {
execa.execaCommandSync.mockReturnValue({ ...baseResult, stderr } as any);
const { default: checkJava } = await import('./check-java.js');
result = checkJava();
result = checkJava([]);
});

it('should return info and javaVersion', async () => {
Expand All @@ -53,7 +53,7 @@ describe('generator - server - checkJava', () => {
before(async () => {
execa.execaCommandSync.mockReturnValue({ ...baseResult, exitCode, stderr } as any);
const { default: checkJava } = await import('./check-java.js');
result = checkJava();
result = checkJava([]);
});

it('should return error', async () => {
Expand All @@ -69,7 +69,7 @@ describe('generator - server - checkJava', () => {
throw new Error('foo');
});
const { default: checkJava } = await import('./check-java.js');
result = checkJava();
result = checkJava([]);
});

it('should return error', async () => {
Expand Down
2 changes: 1 addition & 1 deletion generators/java/support/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const formatDocAsJavaDoc = (text: string, indentSize = 0): string => {
return rows.join('\n');
};

export const formatDocAsApiDescription = (text: string): string => {
export const formatDocAsApiDescription = (text?: string): string | undefined => {
if (!text) {
return text;
}
Expand Down
2 changes: 1 addition & 1 deletion generators/liquibase/needles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('generator - liquibase - needles', () => {
.withOptions({
skipPriorities: ['writing'],
})
.withGenerators([[mockBlueprintSubGen, 'jhipster-myblueprint:liquibase']]);
.withGenerators([[mockBlueprintSubGen, { namespace: 'jhipster-myblueprint:liquibase' }]]);
});

it('Assert changelog is added to master.xml', () => {
Expand Down
2 changes: 1 addition & 1 deletion generators/maven/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe(`generator - ${generator}`, () => {
});
}
},
'jhipster-blueprint:maven',
{ namespace: 'jhipster-blueprint:maven' },
],
]);
});
Expand Down
2 changes: 1 addition & 1 deletion generators/maven/needles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class mockBlueprintSubGen extends BaseApplicationGenerator {
get [BaseApplicationGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup({
mavenStep({ source }) {
const inProfile = this.options.profile;
const inProfile = (this.options as any).profile;
function asItemOrArray<T>(item: T): T | T[] {
return inProfile ? [item] : item;
}
Expand Down
2 changes: 1 addition & 1 deletion generators/server/options/database-migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DATABASE_MIGRATION as optionName } from './index.js';
import optionDefinition from './database-migration.js';

describe(`generators - server - jdl - ${optionName}`, () => {
optionDefinition.knownChoices.forEach(optionValue => {
optionDefinition.knownChoices!.forEach(optionValue => {
describe(`with ${optionValue} value`, () => {
let state: ImportState;

Expand Down
2 changes: 1 addition & 1 deletion generators/server/options/message-broker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MESSAGE_BROKER } from './index.js';
import optionDefinition from './message-broker.js';

describe('generators - server - jdl - messageBroker', () => {
optionDefinition.knownChoices.forEach(optionValue => {
optionDefinition.knownChoices!.forEach(optionValue => {
describe(`with ${optionValue} value`, () => {
let state: ImportState;

Expand Down
8 changes: 4 additions & 4 deletions generators/server/support/needles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
* limitations under the License.
*/
import { before, it, describe, expect } from 'esmocha';
import { defaultHelpers as helpers } from '../../../testing/index.js';
import { defaultHelpers as helpers, runResult } from '../../../testing/index.js';
import { GENERATOR_SPRING_BOOT } from '../../generator-list.js';
import { insertContentIntoApplicationProperties } from './needles.js';
import type { SpringBootApplication } from '../types.js';

describe('generator - server - support - needles', () => {
describe('generated project', () => {
let runResult;
before(async () => {
runResult = await helpers
await helpers
.runJHipster(GENERATOR_SPRING_BOOT)
.withMockedGenerators(['jhipster:common', 'jhipster:languages', 'jhipster:liquibase']);
});
Expand All @@ -36,7 +36,7 @@ describe('generator - server - support - needles', () => {

describe('insertContentIntoApplicationProperties needle', () => {
it('with a non existing needle', () => {
const application = runResult.generator.sharedData.getApplication();
const application: SpringBootApplication = runResult.generator.sharedData.getApplication();
expect(() => insertContentIntoApplicationProperties.call(runResult.generator, application, { foo: 'foo' })).toThrow(
/Missing required jhipster-needle application-properties-foo not found at/,
);
Expand Down
6 changes: 5 additions & 1 deletion generators/upgrade/upgrade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ describe('generator - upgrade', function () {
baseName: 'upgradeTest',
})
.withOptions({ useVersionPlaceholders: false });
await runResult.create(getGenerator(GENERATOR_UPGRADE)).withSpawnMock().withOptions({ useVersionPlaceholders: false }).run();
await runResult
.create(getGenerator(GENERATOR_UPGRADE))
.withSpawnMock()
.withOptions({ useVersionPlaceholders: false } as any)
.run();
});

it('generated git commits to match snapshot', () => {
Expand Down
3 changes: 2 additions & 1 deletion testing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import BaseGenerator from '../generators/base/index.js';
import type { JHipsterGeneratorOptions } from '../generators/base/api.js';
import { getPackageRoot, isDistFolder } from '../lib/index.js';
import type { JSONEntity } from '../jdl/converters/types.js';
import CoreGenerator from '../generators/base-core/generator.js';

type BaseEntity = { name: string } & JSONEntity;
type GeneratorTestType = YeomanGenerator<JHipsterGeneratorOptions>;
type GeneratorTestOptions = JHipsterGeneratorOptions;

type JHipsterRunResult<GeneratorType extends YeomanGenerator = YeomanGenerator> = RunResult<GeneratorType> & {
type JHipsterRunResult<GeneratorType extends CoreGenerator = CoreGenerator> = RunResult<GeneratorType> & {
/**
* First argument of mocked source calls.
*/
Expand Down
11 changes: 10 additions & 1 deletion tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"include": ["cli/**/*", "generators/**/*", "jdl/**/*", "lib/**/*", "testing/**/*"],
"exclude": ["generators/**/*.spec.*"],
"exclude": [
"generators/base-application/*.spec.*",
"generators/base-core/*.spec.*",
"generators/base/**/*.spec.*",
"generators/base-application/*.spec.*",
"generators/bootstrap-application/*.spec.*",
"generators/bootstrap-application-base/*.spec.*",
"generators/java/generators/domain/*.spec.*",
"generators/server/support/needles.spec.ts"
],
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true,
Expand Down

0 comments on commit 1b03f93

Please sign in to comment.