Record network requests? #1060
Replies: 3 comments 12 replies
-
Hey, @elliot-nelson. Currently, we don't have a designated API for recording outgoing requests but you can use the import fs from 'fs'
import path from 'path'
import { setupServer } from './node'
const requests = new Map()
const transactions = new Set()
const server = setupServer()
server.events.on('request:start', (request) => {
// Record every dispatched request.
requests.set(request.id, request)
})
// Since we're not providing any request handlers
// to the "setupServer" call, all responses will be
// bypassed (performed as-is). This will allow us to
// collect the actual responses.
server.events.on('response:bypass', (response, requestId) => {
const request = requests.get(requestId)
transactions.add([request, response])
})
// Add a callback to when the process exits (CTRL+C).
// Alternatively, add a callback to when your test runner
// has finished.
process.on('SIGINT', () => {
const json = []
for (const [request, response] of transactions.entries()) {
json.push({ request, response })
}
// Write the transactions to the disk.
fs.writeFileSync(
path.resolve(__dirname, 'traffic.json'),
JSON.stringify(json, null, 2),
)
}) We are working on a library that'd make it easier to work with request handlers, including runtime traffic introspection and handlers generation (the concept is to generate handlers from various sources: runtime traffic, HAR, OpenAPI). If this feature is crucial for you, please consider raising a matter of sponsoring us in your company, becoming our first golden sponsor. That'd give us enough resources to develop the said library faster. |
Beta Was this translation helpful? Give feedback.
-
Giving an update on this, we're in the final stage of the implementation for a new library called Source. It will allow generating request handlers out of network archives (HAR) and Open API specification documents. This makes actual network snapshots (and specifications) to be the source of truth. I can't give any ETAs on when it will be released but I'd expect it to release this year. Stay tuned and follow our announcements on Twitter when this happens. Thanks. |
Beta Was this translation helpful? Give feedback.
-
hi @kettanaito, based on this discussion here I understand that the project you were working on didn't kick off as released in the end? |
Beta Was this translation helpful? Give feedback.
-
I've been using msw for nodejs unit tests and it's great. But, more recently, we were investigating using it for some larger integration tests.
What would be great is if I could run a test suite with msw enabled, but passing through all requests to the real internet; then, write out a complete set of "recorded" requests and responses that occurred, and check them in. Then I could activate msw for real, passing in the set, and the tests could run even in walled off environments with no internet access.
Is this possible today and/or a possible feature?
Beta Was this translation helpful? Give feedback.
All reactions