Hey meteor community,
we are developing a meteor app with integration tests that follows the testing guide from meteor, but we are now struggling with an unfortunate specific testing issue after migrating our code from a meteors HTTP-package to the fetch-package:
How is it possible to stub meteors fetch package with chai spies / sandbox in integration tests?
If we try to setup the test mocks the same way as the HTTP.call method we end up in an error, that chai can’t mock the fetch es6 module, cause it’s not like a real JS object and the "hasOwnProperty function is missing. Here is the exact error message, while running a test:
TypeError: object.hasOwnProperty is not a function
Here is a minimal code sample how we do the integration test and causing this error:
a server-side module using the fetch.package
“services/fetch-something.js”:
import { fetch } from 'meteor/fetch';
const fetchSomething = (url) => {
return fetch(url,
{
method:'GET'
...fancy authorization headers and stuff
}
)
}
export default fetchSomething ;
and now our integration test module for fetchSomething (e.g. testing of correct headers or correct server errors, whatever)
“services/fetch-something.spec.js”:
import * as fetchModule from 'meteor/fetch'; // using this kind of import to avoid "read-only" exception
import chai from 'chai';
import spies from 'chai-spies';
import fetchSomething from './fetch.something.js';
chai.use(spies);
const sandbox = chai.spy.sandbox();
const mockFetch = (mock) => {
sandbox.on(fetchModule , 'fetch', mockFn);
}
describe('FetchSomething Testing', () => {
if('simple mocking test', () => {
mockFetch(() => console.log); // <--- throws exception
fetchSomething('https://does.not.get/to/this/line-anyway');
});
});
It’s clear for us, that this particular code sample is also testable in a simple unit test where mocking is setup pretty easy (e.g with Jest). But in our real use case the fetch-call is just a part of a whole communication protocol that is required to be tested together with a database system (so be part in the integration testing).
We appreciate any advice or discussion link how we can handle this sample use case of an import-function-mocking.
Looking forward to here your thoughts!
Cheers,
Fabian