mardi 2 juin 2020

fetch-mock "doesn't have a 'get' method."

I'm trying to use fetch-mock to mock fetches in my client pure JS code, but when I require fetch-mock, I don't get an object that has any mocking functions.

This is my MWE:

const chai = require('chai');
const chaiFetchMock = require('chai-fetch-mock');
const fetchMock = require('fetch-mock');

chai.use(chaiFetchMock);

describe('fetch-mock', () => {

    it('doesn\'t have a \'get\' method.', () => {
        fetchMock.get('http://httpbin.org/my-url', { hello: 'world' }, {
          delay: 1000, // fake a slow network
          headers: {
            user: 'me' // only match requests with certain headers
          }
        });
    });
});

This gives me:

...

> mocha

(node:18) [DEP0016] DeprecationWarning: 'GLOBAL' is deprecated, use 'global'


  fetch-mock
    1) doesn't have a 'get' method.


  0 passing (3ms)
  1 failing

  1) fetch-mock
       doesn't have a 'get' method.:
     TypeError: fetchMock.get is not a function
      at Context.<anonymous> (test/test-test.js:10:19)
      at processImmediate (internal/timers.js:456:21)

For the MWE, I'm running this in a Docker container like so:

FROM node

RUN npm install --global mocha chai mock-fetch

COPY package.json /code/
COPY test /code/test

WORKDIR /code

RUN npm install
RUN npm test

With the following package.js file:

{
  "name": "test",
  "version": "1.0.0",
  "main": "main.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "chai": "^4.2.0",
    "mocha": "^7.0.1",
    "fetch-mock": "^1.0.0",
    "node-fetch": "^2.6.0",
    "chai-fetch-mock": "^2.0.0"
  },
  "description": ""
}

Aucun commentaire:

Enregistrer un commentaire