Skip to content

Commit

Permalink
chore: fix some indexed access
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Jan 28, 2025
1 parent d3d6c42 commit c83df22
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/generate/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export function indent(value: string) {
export function extractPickleIdFromLine(line: string) {
const match = line.match(/\/\/ test: ([0-9a-f-]+)$/);
if (match) {
return { pickleId: match[1], index: match.index };
return { pickleId: match[1]!, index: match.index };
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/generate/importTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export class ImportTestGuesser {
this.fillCustomTestsFromRegularSteps();
this.fillCustomTestsFromDecoratorSteps();
if (!this.hasCustomTests()) return;
if (!getExportedTestsCount()) this.throwCantGuessError();
const topmostTest = this.findTopmostTest();
if (getExportedTestsCount() === 0) this.throwCantGuessError();
// topmostTest must exist as we checked tests count above
const topmostTest = this.findTopmostTest()!;
const { file, varName } = this.getExportedTestInfo(topmostTest);
return { file, varName };
}
Expand Down Expand Up @@ -66,7 +67,13 @@ export class ImportTestGuesser {
let topmostTest = customTests[0];

for (let i = 1; i < customTests.length; i++) {
const higherTest = selectHigherTestInstance(topmostTest, customTests[i]);
const customTest = customTests[i];
const higherTest = selectHigherTestInstance(topmostTest, customTest);
// todo: uncomment for noUncheckedIndexedAccess
// const higherTest =
// topmostTest && customTest
// ? selectHigherTestInstance(topmostTest, customTest)
// : topmostTest || customTest;
if (!higherTest) {
exit(
`Can't guess test instance for: ${this.featureUri}.`,
Expand Down Expand Up @@ -101,5 +108,5 @@ export class ImportTestGuesser {
function selectHigherTestInstance(test1: TestTypeCommon, test2: TestTypeCommon) {
if (isTestContainsSubtest(test1, test2)) return test1;
if (isTestContainsSubtest(test2, test1)) return test2;
// Provided tests are no in parent-child relation
// Provided tests are not in parent-child relation
}
2 changes: 1 addition & 1 deletion src/generate/specialTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class SpecialTags {
for (const tag of this.ownTags.reverse()) {
const match = tag.match(/@timeout:([\d_]+)/i);
if (match) {
this.timeout = Number(match[1].replace(/_/g, ''));
this.timeout = Number(match[1]!.replace(/_/g, ''));
return;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/generate/test/decoratorFixtureResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export class DecoratorFixtureResolver {
private getFixtureName(pomNode: PomNode, stepData: StepData) {
const resolvedFixtures = this.testPoms.getResolvedFixtures(pomNode);

if (resolvedFixtures.length === 0) {
return exitEmptyDecoratorFixture(stepData);
}
const firstFixture = resolvedFixtures[0];

if (resolvedFixtures.length > 1) {
if (this.config.statefulPoms) {
Expand All @@ -50,12 +48,16 @@ export class DecoratorFixtureResolver {
// tagged fixture has priority
const firstTaggedFixture = resolvedFixtures.find((f) => f.byTag);
const firstFixtureWithName =
firstTaggedFixture?.name || pomNode.fixtureName || resolvedFixtures[0].name;
firstTaggedFixture?.name || pomNode.fixtureName || firstFixture?.name;
return firstFixtureWithName || exitEmptyDecoratorFixture(stepData);
}
}

return resolvedFixtures[0].name;
if (!firstFixture) {
return exitEmptyDecoratorFixture(stepData);
}

return firstFixture.name;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gherkin/featuresLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FeaturesLoader {
}

private getPickleWithLocation(pickle: Pickle) {
const lastAstNodeId = pickle.astNodeIds[pickle.astNodeIds.length - 1];
const lastAstNodeId = pickle.astNodeIds[pickle.astNodeIds.length - 1]!;
const location = this.gherkinQuery.getLocation(lastAstNodeId);
return { ...pickle, location };
}
Expand Down
2 changes: 1 addition & 1 deletion src/reporter/cucumber/messagesBuilder/AttachmentMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class AttachmentMapper {
private assignAttachment(index: number, pwStep: pw.TestStep) {
// pick attachment from result.attachments array
const [foundAttachment] = this.allAttachments.splice(index, 1);
this.stepAttachments.getOrCreate(pwStep, () => []).push(foundAttachment);
this.stepAttachments.getOrCreate(pwStep, () => []).push(foundAttachment!);
}

// private mapUnusedAttachments() {
Expand Down
6 changes: 4 additions & 2 deletions src/reporter/cucumber/messagesBuilder/TestCaseRunHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ export class TestCaseRunHooks {
}

function buildFallbackError(unprocessedErrors: pw.TestError[]) {
return unprocessedErrors.length === 1
? unprocessedErrors[0]
const firstError = unprocessedErrors[0];

return firstError && unprocessedErrors.length === 1
? firstError
: buildConcatenatedError(unprocessedErrors);
}

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/bddStepInvoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export class BddStepInvoker {
this.bddContext.tags,
);

if (stepDefinitions.length === 1) return stepDefinitions[0];
const firstFoundDefinition = stepDefinitions[0];
if (firstFoundDefinition && stepDefinitions.length === 1) return firstFoundDefinition;

const fullStepLocation = `${this.bddContext.featureUri}:${gherkinStepLine}`;

Expand Down
9 changes: 8 additions & 1 deletion src/snippets/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ export class Snippet {
const cucumberExpressionGenerator = new CucumberExpressionGenerator(
() => parameterTypeRegistry.parameterTypes,
);

const generatedExpressions = cucumberExpressionGenerator.generateExpressions(
this.pickleStep.text,
);

// Always take only first generatedExpression
// Other expressions are for int/float combinations
return generatedExpressions[0];
const firstExpression = generatedExpressions[0];
if (!firstExpression) {
throw new Error(`Could not generate expression for step: ${this.pickleStep.text}`);
}

return firstExpression;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/steps/decorators/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ function getFirstNonAutoInjectFixture(
(fixtureName) => !isBddAutoInjectFixture(fixtureName),
);

if (fixtureNames.length === 0) {
const firstFixtureName = fixtureNames[0];
if (!firstFixtureName) {
throw new Error(`No suitable fixtures found for decorator step "${pattern}"`);
}

if (fixtureNames.length > 1) {
throw new Error(`Several suitable fixtures found for decorator step "${pattern}"`);
}

return fixturesArg[fixtureNames[0]];
return fixturesArg[firstFixtureName];
}

function saveStepConfigToMethod(
Expand Down
2 changes: 1 addition & 1 deletion test/bdd-syntax/steps-cucumber-style/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Then('Passed doc string to contain {string}', async function (arg: string, docSt
Then(
'Passed data table to have in row {int} col {string} value {string}',
async function (row: number, col: string, value: string, table: DataTable) {
expect(table.hashes()[row][col]).toEqual(value);
expect(table.hashes()[row]?.[col]).toEqual(value);
},
);

Expand Down
2 changes: 1 addition & 1 deletion test/bdd-syntax/steps-pw-style/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Then('Passed doc string to contain {string}', async ({}, arg: string, docString:
Then(
'Passed data table to have in row {int} col {string} value {string}',
async ({}, row: number, col: string, value: string, table: DataTable) => {
expect(table.hashes()[row][col]).toEqual(value);
expect(table.hashes()[row]?.[col]).toEqual(value);
},
);

Expand Down
2 changes: 1 addition & 1 deletion test/quotes/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Given('step with docString', ({}, docString: string) => {
});

Given('step with dataTable', ({}, table: DataTable) => {
const { value1, value2 } = table.hashes()[0];
const { value1, value2 } = table.hashes()[0] || {};
expect(value1).toEqual('value with \'`" quotes');
expect(value2).toEqual('value with\n newline');
});

0 comments on commit c83df22

Please sign in to comment.