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

Random data (from fake-data) doesn't work in before section #2756

Open
RemiBardon opened this issue May 15, 2024 · 3 comments
Open

Random data (from fake-data) doesn't work in before section #2756

RemiBardon opened this issue May 15, 2024 · 3 comments

Comments

@RemiBardon
Copy link

RemiBardon commented May 15, 2024

Version info:

Artillery: 2.0.12
Node.js: v22.1.0
OS: darwin

Running this command:

DEBUG=http* artillery run init.yaml --solo

I expected to see this happen:

JSON body has "name" key.

Instead, this happened:

JSON body is empty (fake data not generated):

  http request: {
    "url": "http://127.0.0.1:8000/v1/workspace",
    "method": "PUT",
    "headers": {
      "user-agent": "Artillery (https://artillery.io)"
    },
-   "json": {
-     "name": "…"
-   }
    "json": {}
  }

I suspect it's because

script.scenarios = script.scenarios.map((scenario) => {
scenario.beforeScenario = [].concat(scenario.beforeScenario || []);
scenario.beforeScenario.push('falsoHandler');
return scenario;
});

only maps scenarios in the scenarios section and not the ones in [before](https://www.artillery.io/docs/reference/test-script#before-and-after-sections) and [after](https://www.artillery.io/docs/reference/test-script#before-and-after-sections) sections.

Files being used:

config:
  target: http://127.0.0.1:8000
  plugins:
    fake-data: {}
  
before:
  flow:
    - put:
        url: "/v1/workspace"
        json:
          name: "{{ $randBrand() }}"
  
scenarios: []
@hassy
Copy link
Member

hassy commented May 16, 2024

thanks @RemiBardon! definitely looks like a bug /cc @bernardobridge

@bernardobridge
Copy link
Contributor

👋 Thanks for the bug report @RemiBardon. I will take a look to see what can be done here

@bernardobridge
Copy link
Contributor

Hey @RemiBardon 👋 ,

Apologies for the wait. This is a current limitation of before/after hooks, which we'll update the docs with. Unfortunately, it won't work with certain plugins that require adding functions to the scenario (e.g. fake-data or expect). Changing this behaviour is a little bit involved, as it requires refactoring how these hooks work, so we won't be able to tackle this just yet.

As a workaround, I would suggest using a javascript function from the before hook to accomplish the same. Something like:

const { randBrand } = require('@ngneat/falso');

function setRandomBrand(context, ee, next) {
    context.vars.randomBrand = randBrand();
    next();
};

module.exports = {
    setRandomBrand
}
config:
  target: http://127.0.0.1:8000
  processor: myProcessor.js
  
before:
  flow:
    - function: setRandomBrand
    - put:
        url: "/v1/workspace"
        json:
          name: "{{ randomBrand }}"
  
scenarios: []

Thanks again!

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

No branches or pull requests

3 participants