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

[fix] Multi variable support in check values. #82

Merged
merged 4 commits into from
Jun 2, 2021

Conversation

legander
Copy link
Collaborator

@legander legander commented May 31, 2021

FIX check value referencing multiple variables only resolving first variable.

HAR fragment / desired outcome / before PR result

// HAR fragment
"entries": [
  {
    "variables": [
      {
        "type": JSONPath,
        "name": "firstName",
        "expression": "$.first_name"
      },
      {
        "type": JSONPath,
        "name": "lastName",
        "expression": "$.last_name"
      }
    ],
  },
  {
    "checks": [
      {
        "type": JSONPathValue,
        "expression": "$.full_name",
        "condition": equals,
        "value": "${firstName} ${lastName}"
      }
    ],
  }
]
    
// Desired outcome
check(response, {
  [`\$.full_name equals ${vars["firstName"]} ${vars["lastName"]}`]: response =>
    jsonpath
      .query(response.json(), "$.full_name")
      .some(value => value === `${vars["firstName"]} ${vars["lastName"]}`),
});    

// Before PR
check(response, {
  [`\$.full_name equals ${vars["firstName"]} ${vars["lastName"]}`]: response =>
    jsonpath
      .query(response.json(), "$.full_name")
      .some(value => value === vars["firstName"]),
});

NOTE: Only multi variable usage generate TemplateLiteral while single variable usage still generates MemberExpression.

Multi: `${vars["a"]} ${vars["b"]}`
Single: vars["a"]

@legander legander marked this pull request as draft June 1, 2021 06:43
@legander legander marked this pull request as ready for review June 1, 2021 07:03
@legander legander force-pushed the fix/check-value-multi-variable branch from dd35718 to 811c22e Compare June 1, 2021 07:13
@legander legander requested review from allansson and w1kman June 1, 2021 09:17
@allansson
Copy link
Contributor

allansson commented Jun 1, 2021

// Desired outcome
check(response, {
  [`\$.full_name equals ${vars["firstName"]} ${vars["lastName"]}`]: response =>
    jsonpath
      .query(response.json(), "$.full_name")
      .some(value => value === `${vars["firstName"]} ${vars["lastName"]}`),
});

Isn't it pretty weird that the name of the check would be e.g. $.full_name equals John Doe? I don't think that the name of the check should depend on what the API is returning because it would mean that two runs of the same test could produce different checks. In my mind the name should be something like $.full_name equals "<firstName> <lastName>"

Copy link
Contributor

@allansson allansson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of questions. Nothing big.

const getVariableName = (expr) => (expressions.variable.exec(expr) || [])[1]

const isMultiVariableString = (expr) => {
const matches = [...expr.matchAll(expressions.variables)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a polyfill for the matchAll-function?

https://caniuse.com/?search=matchAll

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For node usage we're good (node 12). For cloud app usage, It looks like core-js covers that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just FYI there is no more core-js in the cloud/k6, but k6 has support for matchAll from goja

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mstoykov The matchAll is run in node/browser when converting the har file to a script. So it doesn't run in k6.

@legander
Copy link
Collaborator Author

legander commented Jun 1, 2021

// Desired outcome
check(response, {
  [`\$.full_name equals ${vars["firstName"]} ${vars["lastName"]}`]: response =>
    jsonpath
      .query(response.json(), "$.full_name")
      .some(value => value === `${vars["firstName"]} ${vars["lastName"]}`),
});

Isn't it pretty weird that the name of the check would be e.g. $.full_name equals John Doe? I don't think that the name of the check should depend on what the API is returning because it would mean that two runs of the same test could produce different checks. In my mind the name should be something like $.full_name equals "<firstName> <lastName>"

Yes I agree with that. That behaviour is not introduced by this PR thought.. But I'll change it 👍

@allansson
Copy link
Contributor

LGTM. You can merge when you feel like it.

Copy link
Contributor

@w1kman w1kman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@legander legander merged commit 584abd4 into master Jun 2, 2021
@legander legander deleted the fix/check-value-multi-variable branch June 2, 2021 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants