A fetch mock that launches express as a worker_thread
npm install expressively-mocked-fetch
> A fetch mock that launches express as a worker_thread to help testing.
NOTE: This library uses worker_threads, a nodejs builtin that was released
with node v13.
``bash`
$ npm i -D expressively-mocked-fetch
Mocking fetch is annoying. I
saw msw. But it didn't fit my use case. So I ended up writing a wrapper around express.js.
1. Download this repo and install the dependencies
2. Run npm run test
`js
// with ava
const test = require("ava");
const createWorker = require("expressively-mocked-fetch");
test("if module is loaded and executed", async (t) => {
const worker = await createWorker(
// regular express.js code
app.get('/', function (req, res) {
res.send("hello world");
});
);
const res = await fetch(http://localhost:${worker.port});`
const text = await res.text();
t.assert(test === "hello world");
});
- string needs to be valid Node.js JavaScript{ requestCount: Number, port: Number }
- object is of shape , where:
- requestCount (optional, default: 1) is the amount of times a serverport
should respond before automatically shutting itself.
- (optional, default: 0) is the desired port the server should0
launch at. For dynamic allocation by the OS, use the default value .pauseMilliseconds
- (optional, default: 0) the time in milliseconds a
server must delay sending an answer using
connect-pause.
- Introduce optional option pauseMilliseconds that allows simulating a
server's delayed response-ability.
- Allow template's app to be overwritten by using let.
- (Bug fix): options allows now to set individual keys without forgetting
all default options.
- Refactor createWorker function signature to allow an options object.port
- Add to options that allows a user to define a port before
launching a worker.
- Using node.js's worker_threads now allows expressively-mocked-fetch to
spawn threads without the usage of temporary files. Interface remains the
same.
- Add support for Content-Type: text/calendar and potentially other types.
- Allow accepting text/plain and application/json.
- Allow defaultCount` option allows a worker to exist for more than 1 request.
- Bug fix: Create new module file for each worker to allow running in parallel.
- Initial release
See LICENSE file.