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

Sometimes config.env.key returns incorrect value if use it in plugin/index.js #2824

Closed
viraxslot opened this issue Nov 22, 2018 · 5 comments
Closed

Comments

@viraxslot
Copy link

viraxslot commented Nov 22, 2018

What I want to do:

  1. I'm testing API method which saves context into redis
  2. After request I want to check values in redis, so I've added task into plugin/index.js
  3. I call cy.task() in my test and everything works fine, but not always (see below)

Code in plugin/index.js:

var Redis = require('ioredis');
const path = require('path');
const fs = require('fs-extra');
decodeBase64Token = require('../../helpers/decode-token');

function getConfigurationByFile(file) {
  const pathToConfigFile = path.resolve('.\\cypress\\config', `${file}.json`);
  return fs.readJson(pathToConfigFile);
}

function redisValue(token, config) {
  return new Promise((resolve, reject) => {		
    const redis = new Redis(config.env.redisPort, config.env.redisHost);
    // log host in ioredis instance
    console.log(redis.options.host);
    const decodedToken = decodeBase64Token(token);		
    const fullKey = `TEST:${decodedToken.payload.jti}`;
    
    redis.hget(fullKey, 'data').then((res) => {
       try {							
        redis.disconnect();				
        const data = JSON.parse(res);
        resolve(data);
      }
      catch (e) {
        console.log('Error to parse token:', e.message);
      }
    }).catch(error => reject(error));
  });
}

module.exports = (on, config) => {
  on('task', {
    getRedisValueByToken(token) {
      return redisValue(token, config);
    }
  });
  const file = config.env.configFile || 'test';
  return getConfigurationByFile(file);
};

Steps to reproduce / Current behavior:

I open cypress using command

npx cypress open --env configFile=staging

When I run tests, which use task "getRedisValueByToken" in browser almost always first run fails and code console.log(redis.options.host); prints:

GET /__/ 200 25.320 ms - -
GET /__cypress/runner/cypress_runner.css 200 65.206 ms - -
GET /__cypress/runner/cypress_runner.js 200 464.325 ms - -
GET /__cypress/runner/fonts/fontawesome-webfont.woff2?v=4.6.3 200 3.532 ms - 71896
GET /__cypress/iframes/integration/user-post.spec.js 200 476.202 ms - 735
GET /__cypress/tests?p=cypress%5Csupport%5Cindex.js-539 200 1491.571 ms - -
GET /__cypress/tests?p=cypress%5Cintegration%5Cuser-post.spec.js-868 200 3552.497 ms - -
localhost
localhost
localhost
HEAD / 500 4559.627 ms - -
HEAD / 500 4557.186 ms - -
HEAD / 500 4556.445 ms - -

If rerun test cases - nothing changes.
But if add console.log(123); after console.log(redis.options.host); and save plugin/index.js then tests passes and log looks like:

HEAD / 500 4560.082 ms - -
HEAD / 500 4581.335 ms - -
HEAD / 500 4585.360 ms - -
GET /__cypress/iframes/integration/user-post.spec.js 200 12.456 ms - 735
GET /__cypress/tests?p=cypress%5Csupport%5Cindex.js-842 200 13.413 ms - -
GET /__cypress/tests?p=cypress%5Cintegration%5Cuser-post.spec.js-869 200 87.054 ms - -
need.host.net
123
need.host.net
123
need.host.net
123

After that test cases always passes.

Desired behavior:

I want to understand is it correct to pass config object to a function "redisValue" in this way?
If not then how to use environment variables in tasks?

Versions

Cypress package version: 3.1.2
Windows 10
Run with Chrome 70 / Electron 59

Thank you.

@chrisbreiding chrisbreiding self-assigned this Nov 26, 2018
@chrisbreiding chrisbreiding added the stage: needs investigating Someone from Cypress needs to look at this label Nov 26, 2018
@christophechevalier
Copy link

christophechevalier commented Dec 5, 2018

Hi @viraxslot !
Does it do the same thing ?

const pathToConfigFile = path.resolve('.\\cypress\\config', `${file}.json`);

and

const pathToConfigFile = path.resolve('cypress', 'config', `${file}.json`);

Thanks.

@viraxslot
Copy link
Author

@christophechevalier
Here is a simple example:
image

You also could check it in node.js documentation
I suppose the answer is "yes" :)

@brian-mann
Copy link
Member

brian-mann commented Dec 24, 2018

@viraxslot It doesn't look like you're ever mutating the config object, so I'm not sure how config.env would ever get mutated to have the redisHost and redisPort values - unless those are passed in somewhere else.

The --configFile option does not appear to be doing anything that would alter those values. You're returning the object, which I'm guessing does have those properties, but you're never modifying the actual config closure, which is why those values are not being applied.

If this isn't the case you'll need to provide a reproducible repo that shows the problem.

@jennifer-shehane jennifer-shehane added stage: awaiting response Potential fix was proposed; awaiting response and removed stage: needs investigating Someone from Cypress needs to look at this labels Jan 8, 2019
@jennifer-shehane
Copy link
Member

Unfortunately we'll have to close this issue if no reproducible example is provided. Can anyone provide a way to reproduce this?

@jennifer-shehane
Copy link
Member

Unfortunately we have to close this issue as there is not enough information to reproduce the problem. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.

Please comment in this issue with a reproducible example and we will reopen the issue. 🙏

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

5 participants