Created
March 3, 2022 05:55
-
-
Save crazy4groovy/73b2a08ebb05ea476e3487e839e9834f to your computer and use it in GitHub Desktop.
Use talkback as a wiremock/api proxy for consistent API testing, in a Worker thread! (NodeJS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Worker } from 'worker_threads'; | |
import MyClient from './my-client.js'; | |
const proxyAPI = new Worker(__dirname + "/server-worker.js"); | |
define('Module1', () => { | |
let client; | |
beforeEach(() => { | |
client = new MyClient("http://localhost:5544"); | |
}); | |
after(() => { | |
proxyAPI.terminate(); | |
}); | |
it('should contact the proxyAPI in the client', () => { | |
// client.get('foo') etc | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { isMainThread } from 'worker_threads'; | |
// import talkback from "talkback/es6"; | |
if (!isMainThread) { | |
const talkback = require("talkback"); | |
const opts = { | |
host: "https://api.myapp.com/foo", | |
port: 5544, // be sure to reference http://localhost:5544 in your client! | |
record: talkback.Options.RecordMode.NEW, | |
path: __dirname + "/my-api-response-tapes", | |
silent: true | |
}; | |
const server = talkback(opts); | |
server.start(() => console.log("Talkback Started")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also MSW (Mock Service Worker): https://mswjs.io/docs/getting-started/integrate/node