-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Multiple files, multiple beforeEach, not all run #1678
Comments
When you run your tests with "run all" or using I'm not exactly clear as to what is happening since there is no code provided for each spec file. Are you wrapping the spec_file1.js describe('File 1 tests', () => {
beforeEach(() => {
// commands here
})
}) spec_file2.js describe('File 2 tests', () => {
beforeEach(() => {
// commands here
})
})
`` |
I have indeed 1 global before each in each file, and before each in describes. I don't see what code could clarify this as it's straightforward but: The global one: beforeEach(() => {
cy.mockAllUnhandledHttpCalls();
cy.mockGetAuthenticationData();
cy.mockGetBusinessUnitData();
cy.mockGetPermissionsData();
cy.mockGetNavigationData();
cy.mockGetActivationPartnersData();
}); In describe: describe('something', () => {
beforeEach(() => {
cy.visit('/activations');
cy.mockGetActivationPartnersData();
cy.mockGetActivationsData();
});
it('should display a list of activations', () => {
cy.get('.activation').should('have.length', 2);
}); |
I am 100% sure there is not a bug in the way this works - but there is likely a misunderstanding of how it works. Hooks come from For us to look at this you'll need to put together a reproducible repo with two test files and the code that you're describing. Just add something like |
I have only one root level beforeEach per file. The issue is apparently when merging multiple files together. For me it's a bug, since it's not merging correctly. |
If you want to run a I'd have to investigate more on whether this is an issue - but this is a suggestion for now. |
Thank you. Every before each the in the second test didn't get touched, making the test fail. The workaround is to copy the code in the multiple beforeEach blocks in every test, which works but provides a lot of code duplication. |
Can you just wrap a larger describe within each file to contain the global file1.js describe('file 1 something', () => {
beforeEach(() => {
cy.mockAllUnhandledHttpCalls();
cy.mockGetAuthenticationData();
cy.mockGetBusinessUnitData();
cy.mockGetPermissionsData();
cy.mockGetNavigationData();
cy.mockGetActivationPartnersData();
});
describe('something', () => {
beforeEach(() => {
cy.visit('/activations');
cy.mockGetActivationPartnersData();
cy.mockGetActivationsData();
});
it('should display a list of activations', () => {
cy.get('.activation').should('have.length', 2);
});
});
}); file2.js describe('file 2 something', () => {
beforeEach(() => {
cy.mockAllUnhandledHttpCalls();
cy.mockGetAuthenticationData();
cy.mockGetBusinessUnitData();
cy.mockGetPermissionsData();
cy.mockGetNavigationData();
cy.mockGetActivationPartnersData();
});
describe('something', () => {
beforeEach(() => {
cy.visit('/activations');
cy.mockGetActivationPartnersData();
cy.mockGetActivationsData();
});
it('should display a list of activations', () => {
cy.get('.activation').should('have.length', 2);
});
});
}); |
Ok, so, I should have taken a closer look to begin with at what the code was doing . You made the assertion that the You will always want to set up I think the reason these routes are not being mocked is because you are defining them after the beforeEach(() => {
cy.visit('/activations');
cy.mockGetActivationPartnersData();
cy.mockGetActivationsData();
}); This should be: beforeEach(() => {
cy.mockGetActivationPartnersData();
cy.mockGetActivationsData();
cy.visit('/activations');
}); I'm actually a bit confused how your last example could be working reliably on every run. If this doesn't help, I think we need to take a step back and have you define what exactly about this is "not working" in the runner exactly. They're not being stubbed? What does the screenshot of the test runner show? |
phew, I didn't think Let us know if you have any other issues - or ask in our community chat https://gitter.im/cypress-io/cypress |
Hm so no resolution? |
I'm not sure why this was closed... @jennifer-shehane ? Here's a screenshot of the dashboard with the failing tests. The routes section correctly shows the xhr requests to mock (even if duplicated), but they're just not getting hit.
Means the mock xhr resource didn't get hit, and thus makes this fail. |
The You generally want to set up |
Indeed, but the request never responds because it's not hit, ie the problem. :D So no matter how long I wait, it'll never hit. Again, this works fine when running the single test file on its own. It fails when running multiple files. |
Yeah, we'll need a reproducible example of this behavior - the I'm unable to figure out why an example like below would not work with the information provided. Cypress.Commands.add('mockGetActivationsData', (fixture = 'getActivations.json') => {
cy.server();
cy.route('GET', '**/**/business-units/*/activations', `fixture:activations/${fixture}`).as("getActivations");
});
describe('something', () => {
beforeEach(() => {
cy.mockGetActivationsData();
cy.visit('/activations');
});
it('should display a list of activations', () => {
cy.wait("@getActivations")
cy.get('.activation').should('have.length', 2);
});
}); |
Here's a simplified version of the pieces you are explaining from our example app. This example is passing. Maybe you could have this reflect what you are doing to get this to fail for me? I'm just unable to recreate this as being a spec1.js beforeEach(() => {
cy.server()
cy.route('GET', 'comments/*', {
'body': 'laudantium enim quasi est quidem magnam voluptate ipsam eos↵tempora quo necessitatibus↵dolor quam autem quasi↵reiciendis et nam sapiente accusantium',
'email': 'Eliseo@gardner.biz',
'id': 1,
'name': 'id labore ex et quam laborum',
'postId': 1,
}).as('getComment')
})
context('Network Requests', () => {
beforeEach(() => {
cy.server()
cy.route('POST', '/comments', 'foo').as('postComment')
cy.visit('https://example.cypress.io/commands/network-requests')
})
it('cy.route() - route responses to matching requests', () => {
let message = 'whoa, this comment does not exist'
cy.get('.network-btn').click()
cy.wait('@getComment').its('status').should('eq', 200)
cy.get('.network-post').click()
cy.wait('@postComment')
})
}) spec2.js beforeEach(() => {
cy.server()
cy.route('POST', '/comments', 'foo').as('postComment')
})
context('Network Requests', () => {
beforeEach(() => {
cy.server()
cy.route('GET', 'comments/*', {
'body': 'laudantium enim quasi est quidem magnam voluptate ipsam eos↵tempora quo necessitatibus↵dolor quam autem quasi↵reiciendis et nam sapiente accusantium',
'email': 'Eliseo@gardner.biz',
'id': 1,
'name': 'id labore ex et quam laborum',
'postId': 1 }).as('getComment')
cy.visit('https://example.cypress.io/commands/network-requests')
})
it('cy.route() - route responses to matching requests', () => {
let message = 'whoa, this comment does not exist'
cy.get('.network-btn').click()
cy.wait('@getComment').its('status').should('eq', 200)
cy.get('.network-post').click()
cy.wait('@postComment')
})
}) |
Unfortunately we'll have to close this issue if no reproducible example is provided. Can anyone provide a way to reproduce this? |
I am having a similar issue where the root describe isn't running with nesting describes describe("...", () => {
beforeEach(() => cy.setCookie(...));
describe("...", () => {
beforeEach(() => cy.route2("/foo", { delayMs: 2000 }).as("requestFoo"));
it("...", () => {
/* cy.setCookie() running and cy.route2() running */
/* trigger action to request Foo */
/* run asserts: OK */
// cy.wait('@requestFoo')
});
});
describe("...", () => {
beforeEach(() => something());
it("...", () => {
/* something() running */
/* cy.setCookie() not running */
});
});
}); When I uncomment the line // cy.wait('@requestFoo'), the issue disappears. |
@zeralight Can you provide an example we can run completely? There's a lot of things not included in your example that may be affecting the way it runs. You can edit our tests against our example.cypress.io website, which has some routing. Repo to fork: https://github.com/cypress-io/cypress-example-kitchensink And opening a new issue would probably be best than this older issue. |
Hello,
Current behavior:
I have 2 test files, both have their
beforeEach
with some commands that stub xhr requests.However, only one of the 2
beforeEach
sections gets run. I do see it in the "Routes" section, but with 0 hits.When I run both files in isolation, they work as expected.
Example command:
Desired behavior:
Each
beforeEach
section should allow commands to be fired to intercept xhr requests.Steps to reproduce:
Create 2 test files. Each of them have a
beforeEach
section that call a command, that stubs a XHR request.It works fine when I remove my command calls from every
beforeEach
section and duplicate them across every test.Versions
2.1.0
66
Thanks
The text was updated successfully, but these errors were encountered: