Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Created March 3, 2022 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/73b2a08ebb05ea476e3487e839e9834f to your computer and use it in GitHub Desktop.
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)
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
});
});
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"));
}
@crazy4groovy
Copy link
Author

crazy4groovy commented Apr 18, 2022

See also MSW (Mock Service Worker): https://mswjs.io/docs/getting-started/integrate/node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment