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

Open
gautejohan opened this Issue Mar 2, 2018 · 5 comments

Comments

5 participants
@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

This comment has been minimized.

Member

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Collaborator

bahmutov commented Jul 8, 2018

@Ancinek

This comment has been minimized.

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`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment