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

Fails to automatically load fixture files if more than one . is in file name #1402

Closed
gautejohan opened this issue Mar 2, 2018 · 7 comments
Closed

Comments

@gautejohan
Copy link

@gautejohan gautejohan commented Mar 2, 2018

  • Operating System: OSX High Sierra 10.13.3
  • Cypress Version: 2.1.0

Is this a Feature or Bug?

Bug

Current behavior:

cy.fixture function does not look up files without extension correctly if they have several dots. Is probably looking for first element after first ".", but should look for element after last "."

How to reproduce:

Check this code: tiny test fork
Create a fixture file with several dots in name (e.g. parent.child.info.json, and use cy.fixture('parent.child.info') in your test.

@jennifer-shehane
Copy link
Member

@jennifer-shehane jennifer-shehane commented Mar 6, 2018

This is a bug.

You can workaround this issue currently by providing the file extension in the argument to cy.fixture().

Example:

describe('Cypress fixture', () => {
  it('several dots in fixture name', () => {
    cy.fixture('user.test.1')  // Error: No fixture exists at:...cypress/fixtures/user.test.1
  })

  it('several dots in fixture name with file ext', () => {
    cy.fixture('user.test.1.json')  // Works fine
  })
})

The issue is likely where the fileName for fixtures are handled here: https://github.com/corneliutusnea/cypress/blob/issue-1335/packages/server/lib/fixture.coffee#L24 Feel free to open a PR for a fix.

@gautejohan
Copy link
Author

@gautejohan gautejohan commented Mar 9, 2018

Hi!

I'm new to coffee script, but I have looked at the code.
The problem is on line 31, if ext = path.extname(p)

The extname function returns what it thinks is the extension, but when you provide a fixture name with two or more "." (but without the actual extension), parseFile will fail, and the code doesn't try to parse the file with the list of extensions.

Not sure how to best rewrite it but I guess the code should check if the file exists or not (before checking extension), and if it doesn't exist, check each extension like today.

I don't know how much of an impact in speed this will be, checking if a file exists is rather expensive(?)

@Ancinek
Copy link

@Ancinek Ancinek commented Jul 8, 2018

Hey, I didn't want to create a new issue so I will write here as this is kind of related.

Is there a way in cypress to see if a file exists?
For example, I would like to know if a fixture file exists, and if not I would like to request it from the server and save it. If I do cy.readFile for a file that does not exist, the test execution fails and stops.

@bahmutov
Copy link
Contributor

@bahmutov bahmutov commented Jul 8, 2018

@Ancinek
Copy link

@Ancinek Ancinek commented Jul 8, 2018

@bahmutov That's what I was looking for! For anyone that also needs something like this - from within the plugin/index.js file you invoke an on method, so the file looks like this:

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  on('task', {
    checkFixtureExistance: (fixtureName) => {
      const rootPath = appRootDir.get();
      const filePath = `${rootPath}/cypress/fixtures/${fixtureName}.json`;
      return fs.existsSync(filePath);
    },
  });
};

So in the app you can then simply do:

const provideFixture = (fixtureName, endpointPath) => {
  cy.server();
  const filePath = `cypress/fixtures/${fixtureName}.json`;
  cy.task('checkFixtureExistance', fixtureName).then((exists) => {
    if (exists) return;
    cy.request(endpointPath).then(({ body }) => {
      cy.writeFile(filePath, body);
    });
  });
  cy.route(path, `fixture:${fixtureName}.json`);
}
@cypress-bot
Copy link

@cypress-bot cypress-bot bot commented Mar 6, 2019

The code for this is done in cypress-io/cypress#3606, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

flotwig added a commit that referenced this issue Mar 6, 2019
…3606)

Fixes #1402

Also fixes a few TODO comments in the fixtures.coffee file along the way
Bkucera added a commit that referenced this issue Mar 12, 2019
…3606)

Fixes #1402

Also fixes a few TODO comments in the fixtures.coffee file along the way
@cypress-bot
Copy link

@cypress-bot cypress-bot bot commented Mar 15, 2019

Released in 3.2.0.

Bkucera added a commit to Bkucera/cypress that referenced this issue Apr 23, 2019
…ypress-io#3606)

Fixes cypress-io#1402

Also fixes a few TODO comments in the fixtures.coffee file along the way
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants