-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
How to postpone a request on page load until worker.use
is called
#326
Comments
Hey, @dheeraj-jn. That's a good question.
Does this mean that |
Yes, it appears so. Feel free to close this issue if it's not fixable. |
Could you please test this scenario on other CI (CircleCI, TravisCI)? I don't think there's much to be done on the library's side, but if we can find a reliable reproduction scenario for this, I'd be glad to take a look at what's going on. Thanks. |
It may also be helpful to perform any 2 side-effects: one in the describe('...', () => {
before(() => console.log('one'))
test('...', () => console.log('two'))
}) See how that behaves on GitHub actions. |
Unfortunately GitHub Actions does not display console log messages in the run console. Anyway, I worked around the problem by introducing a delay of 2 seconds in a |
I definitely look forward for a better solution to avoid flakiness and to speed up the tests. |
All msw/src/setupWorker/setupWorker.ts Lines 110 to 112 in de0f583
msw/src/utils/requestHandlerUtils.ts Lines 3 to 8 in de0f583
This operation is synchronous, so it cannot introduce any race conditions on its own. Since it's the client-side of the library that references the handlers in the context, and not the worker, it's not an update issue either. The fact that it's reproducible only on CI seems to be the only viable investigation clue. Also, maybe this is not the correct API to get the window and ensure the before(() => {
cy.visit('/login')
cy.window().then((window) => {
const { worker, rest } = window.msw
worker.use(
// Adds a "POST /login" runtime handler
// for all tests in this suite.
rest.post('/login', (req, res, ctx) => {
return res(ctx.json({ success: true }))
}),
)
})
})
@dheeraj-jn, can you try creating this simple custom command to retrieve the mocks and let me know what you see? // your/mocks
import { setupWorker, rest } from 'msw'
const worker = setupWorker(...)
// Export from the module instead of assigning to `window`.
export { worker, rest }
// cypress/commands/index.js
const { worker, rest } = require('../from/your/mocks)
Cypress.Commands.add('getMocks', () => {
return { worker, rest }
})
// cypress/integration/test-suite.test.js
describe('Test suite', () => {
before(() => {
const { worker, rest } = cy.getMocks()
worker.use(rest.get('/user', resolver))
})
}) |
Hey @dheeraj-jn! I created a repro of this targeting GitHub Actions and CircleCI earlier. What I noticed in my testing is that there are a few things that can possibly go wrong depending on your configuration.
I think in your case, you should try approach #1, and just compare your configuration to #2. I'm curious to hear what works for you! If you don't mind, please give us an update when you get a chance 😄 |
The log will only show up in the browser console. To get the log out in node, you'll need something like: // cypress/support/commands.js
Cypress.Commands.add('nodeLog', (message) => cy.task('nodeLog', message) // cypress/plugins/index.js
module.exports = (on, config) => {
codeCoverage(on, config)
on('task', {
nodeLog(message) {
console.log(message)
return null
},
})
} Then |
Closing due to inactivity. Please refer to the Matt's comment if you experience a similar issue. Thanks. |
Is your feature request related to a problem? Please describe.
I'm using Cypress as the test runner and followed the example in the documentation. It works fine for requests that are made upon an action like a button click, but for requests that are made on page load, there's a race condition between the actual request on page load and the call to
worker.use()
. It works fine on my local machine but fails in GitHub Actions CI.Describe the solution you'd like
Not quite sure, but perhaps a way to postpone requests with a promise.
Describe alternatives you've considered
Delay the actual request in user land.
Additional context
None
The text was updated successfully, but these errors were encountered: