Skip to content

Instantly share code, notes, and snippets.

@ThatJoeMoore
Created July 8, 2020 19:27
Show Gist options
  • Save ThatJoeMoore/a9d7d3ea7bd8fd6a784a2b9e2061dd12 to your computer and use it in GitHub Desktop.
Save ThatJoeMoore/a9d7d3ea7bd8fd6a784a2b9e2061dd12 to your computer and use it in GitHub Desktop.
MSW-Jest-JSDOM-Axios Issue

MSW-Jest-JSDOM-Axios issue

First, run npm i.

To see the issue, run npm test. We expect the test to succeed, instead it will error with:

TypeError: Protocol "http:" not supported. Expected "https:"TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
 at Object.proxiedOriginalRequest (node_modules/node-request-interceptor/lib/http/override.js:53:36)
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
import axios from 'axios';
export default async function makeCall(url) {
return (await axios.get(url)).data;
}
import {rest} from 'msw';
import {setupServer} from 'msw/node'
import makeCall from './main.js';
setupServer(
rest.get('https://www.thatjoemoore.com/data.json', (req, res, ctx) => {
return res(
ctx.json({
happy: true
})
)
})
);
it('should see the mocked response', async () => {
await makeCall('https://www.thatjoemoore.com/data.json');
});
{
"name": "msw-axios-jest",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^26.1.0",
"msw": "^0.19.5"
},
"dependencies": {
"@babel/preset-env": "^7.10.4",
"axios": "^0.19.2",
"babel-jest": "^26.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment