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 and migrate to JHipster helpers #27426

Merged
merged 14 commits into from
Sep 27, 2024
Merged
2 changes: 1 addition & 1 deletion cli/cli.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const cliSharedBlueprintFiles = {
describe('cli', () => {
const __filename = fileURLToPath(import.meta.url);
const jhipsterCli = join(dirname(__filename), '..', 'bin', 'jhipster.cjs');
const logger = { verboseInfo: esmocha.fn(), fatal: esmocha.fn(), debug: esmocha.fn() };
const logger = { verboseInfo: esmocha.fn(), warn: esmocha.fn(), fatal: esmocha.fn(), debug: esmocha.fn() };
const getCommand = esmocha.fn();
let mockCli;
let argv;
Expand Down
2 changes: 1 addition & 1 deletion cli/environment-builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,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
2 changes: 1 addition & 1 deletion cli/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import chalk from 'chalk';

import { CLI_LOGGER, createJHipsterLogger } from '../generators/base/support/index.js';
import { CLI_LOGGER, createJHipsterLogger } from '../lib/utils/logger.js';

export const CLI_NAME = 'jhipster';
export const GENERATOR_NAME = 'generator-jhipster';
Expand Down
5 changes: 2 additions & 3 deletions generators/angular/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, dirname, join } from 'path';
import { basename, dirname } from 'path';
import { fileURLToPath } from 'url';
import { before, describe, expect, it } from 'esmocha';
import { snakeCase } from 'lodash-es';
Expand All @@ -14,7 +14,6 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const generator = basename(__dirname);
const generatorFile = join(__dirname, 'index.ts');

const { ANGULAR: clientFramework } = clientFrameworkTypes;
const commonConfig = { clientFramework, nativeLanguage: 'en', languages: ['fr', 'en'] };
Expand Down Expand Up @@ -88,7 +87,7 @@ describe(`generator - ${clientFramework}`, () => {
describe(name, () => {
before(async () => {
await helpers
.run(generatorFile)
.runJHipster(generator)
.withJHipsterConfig(sampleConfig, entities)
.withSharedApplication({ gatewayServicesApiAvailable: sampleConfig.applicationType === 'gateway' })
.withControl({ getWebappTranslation: () => 'translations' })
Expand Down
46 changes: 25 additions & 21 deletions generators/base-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,30 @@ describe(`generator - ${generator}`, () => {
}

before(async () => {
await helpers.run(CustomGenerator as any).withJHipsterConfig({}, [
{
name: 'One',
fields: [{ fieldName: 'id', fieldType: 'Long' }],
relationships: [{ relationshipName: 'two', otherEntityName: 'Two', relationshipType: 'many-to-one' }],
},
{
name: 'Two',
fields: [
{ fieldName: 'id', fieldType: 'Long' },
{ fieldName: 'name', fieldType: 'String' },
],
relationships: [
{ relationshipName: 'one', otherEntityName: 'One', relationshipType: 'many-to-one' },
{ relationshipName: 'three', otherEntityName: 'Three', relationshipType: 'many-to-one' },
],
},
{
name: 'Three',
},
]);
await helpers
.run(CustomGenerator as any)
.withJHipsterGenerators({ useDefaultMocks: true })
.withJHipsterConfig({}, [
{
name: 'One',
fields: [{ fieldName: 'id', fieldType: 'Long' }],
relationships: [{ relationshipName: 'two', otherEntityName: 'Two', relationshipType: 'many-to-one' }],
},
{
name: 'Two',
fields: [
{ fieldName: 'id', fieldType: 'Long' },
{ fieldName: 'name', fieldType: 'String' },
],
relationships: [
{ relationshipName: 'one', otherEntityName: 'One', relationshipType: 'many-to-one' },
{ relationshipName: 'three', otherEntityName: 'Three', relationshipType: 'many-to-one' },
],
},
{
name: 'Three',
},
]);
});

it('should call priorities with correct arguments', async () => {
Expand Down Expand Up @@ -397,6 +400,7 @@ describe(`generator - ${generator}`, () => {
before(async () => {
await helpers
.run(CustomGenerator as any)
.withJHipsterGenerators({ useDefaultMocks: true })
.withJHipsterConfig({}, [
{
name: 'One',
Expand Down
10 changes: 1 addition & 9 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,15 +721,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
if (await this.env.get(namespace)) {
generator = namespace;
} else {
// Keep test compatibility were jhipster lookup does not run.
const found = ['/index.js', '/index.cjs', '/index.mjs', '/index.ts', '/index.cts', '/index.mts'].find(extension => {
const pathToLook = join(__dirname, `../${generator}${extension}`);
return existsSync(pathToLook) ? pathToLook : undefined;
});
if (!found) {
throw new Error(`Generator ${generator} was not found`);
}
generator = join(__dirname, `../${generator}${found}`);
throw new Error(`Generator ${generator} was not found`);
}
}

Expand Down
10 changes: 7 additions & 3 deletions generators/base/blueprints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,11 @@ describe('generator - base - local blueprint', () => {

describe('generates application', () => {
before(async () => {
await helpers.run(BLUEPRINT_NS).withFiles({ '.blueprint/app/index.mjs': BLUEPRINT_CONTENTS }).commitFiles().withJHipsterConfig();
await helpers
.runJHipster(BLUEPRINT_NS, { useEnvironmentBuilder: true })
.withFiles({ '.blueprint/app/index.mjs': BLUEPRINT_CONTENTS })
.commitFiles()
.withJHipsterConfig();
});

it('creates expected default files', () => {
Expand Down Expand Up @@ -486,7 +490,7 @@ describe('generator - base-blueprint', () => {
before(() => {
mockedPriorities = createPrioritiesFakes();
mockBlueprintSubGen = createAllBlueprint(mockedPriorities);
return helpers.run(mockBlueprintSubGen);
return helpers.run(mockBlueprintSubGen).withJHipsterGenerators({ useDefaultMocks: true });
});

priorities.forEach((priority, idx) => {
Expand Down Expand Up @@ -541,7 +545,7 @@ describe('generator - base-blueprint', () => {
return super.end;
}
};
return helpers.create(mockBlueprintSubGen).run();
return helpers.run(mockBlueprintSubGen).withJHipsterGenerators({ useDefaultMocks: true });
});

priorities.forEach(priority => {
Expand Down
9 changes: 6 additions & 3 deletions generators/base/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ describe(`generator - ${generator}`, () => {
}

before(async () => {
await helpers.run(CustomGenerator as any).withOptions({
skipPriorities: ['prompting', 'writing', 'postWriting'],
});
await helpers
.run(CustomGenerator as any)
.withJHipsterGenerators({ useDefaultMocks: true })
.withOptions({
skipPriorities: ['prompting', 'writing', 'postWriting'],
});
});

it('should skip priorities', async () => {
Expand Down
13 changes: 6 additions & 7 deletions generators/client/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { basename, dirname, join } from 'path';
import { basename, dirname } from 'path';
import { fileURLToPath } from 'url';
import { snakeCase } from 'lodash-es';
import { before, describe, expect, it } from 'esmocha';
Expand All @@ -32,7 +32,6 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const generator = basename(__dirname);
const generatorFile = join(__dirname, 'index.js');

describe(`generator - ${generator}`, () => {
it('generator-list constant matches folder name', async () => {
Expand All @@ -49,7 +48,7 @@ describe(`generator - ${generator}`, () => {
const options = { enableTranslation: false };
before(async () => {
await helpers
.run(generatorFile)
.runJHipster(generator)
.withControl({ getWebappTranslation: () => 'translations' })
.withJHipsterConfig(options)
.withSkipWritingPriorities()
Expand All @@ -68,7 +67,7 @@ describe(`generator - ${generator}`, () => {
const options = { enableTranslation: true };
before(async () => {
await helpers
.run(generatorFile)
.runJHipster(generator)
.withControl({ getWebappTranslation: () => 'translations' })
.withJHipsterConfig(options)
.withSkipWritingPriorities()
Expand All @@ -87,7 +86,7 @@ describe(`generator - ${generator}`, () => {
const options = { testFrameworks: [] };
before(async () => {
await helpers
.run(generatorFile)
.runJHipster(generator)
.withControl({ getWebappTranslation: () => 'translations' })
.withJHipsterConfig(options)
.withSkipWritingPriorities()
Expand All @@ -109,7 +108,7 @@ describe(`generator - ${generator}`, () => {
const options = { testFrameworks: [CYPRESS] };
before(async () => {
await helpers
.run(generatorFile)
.runJHipster(generator)
.withControl({ getWebappTranslation: () => 'translations' })
.withJHipsterConfig(options)
.withSkipWritingPriorities()
Expand Down Expand Up @@ -139,7 +138,7 @@ describe(`generator - ${generator}`, () => {
];
before(async () => {
await helpers
.run(generatorFile)
.runJHipster(generator)
.withControl({ getWebappTranslation: () => 'translations' })
.withJHipsterConfig({ applicationType: 'microservice' })
.withSkipWritingPriorities()
Expand Down
7 changes: 3 additions & 4 deletions generators/common/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { basename, dirname, join } from 'path';
import { basename, dirname } from 'path';
import { fileURLToPath } from 'url';
import { before, describe, expect, it } from 'esmocha';
import { snakeCase } from 'lodash-es';
Expand All @@ -30,7 +30,6 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const generator = basename(__dirname);
const generatorFile = join(__dirname, 'index.js');

const mockedGenerators = ['jhipster:git'];

Expand All @@ -45,7 +44,7 @@ describe(`generator - ${generator}`, () => {
describe('with', () => {
describe('default config', () => {
before(async () => {
await helpers.run(generatorFile).withJHipsterConfig().withMockedGenerators(mockedGenerators);
await helpers.runJHipster(generator).withJHipsterConfig().withMockedGenerators(mockedGenerators);
});

it('should succeed', () => {
Expand All @@ -59,7 +58,7 @@ describe(`generator - ${generator}`, () => {
describe('Custom prettier', () => {
before(async () => {
await basicHelpers
.run(generatorFile)
.runJHipster(generator)
.withJHipsterConfig({
prettierTabWidth: 10,
})
Expand Down
Loading
Loading