Skip to content

matcher is not working #80

@jrdn91

Description

@jrdn91

It seems the matcher is not working, here is my config

export default [
  {
    /**
     * regular expression of URL
     */
    pattern: process.env.REACT_APP_API_ENDPOINT,

    /**
     * returns the data
     *
     * @param match array Result of the resolution of the regular expression
     * @param params object sent by 'send' function
     * @param headers object set by 'set' function
     * @param context object the context of running the fixtures function
     */
    fixtures: function(match, params, headers, context) {
      if (/\/events\/[\d|\w]+$/.test(match[1])) {
        return {
          title: "Test"
        }
      }
    },

    /**
     * returns the result of the GET request
     *
     * @param match array Result of the resolution of the regular expression
     * @param data  mixed Data returns by `fixtures` attribute
     */
    get: function(match, data) {
      return {
        body: data
      }
    },

    /**
     * returns the result of the POST request
     *
     * @param match array Result of the resolution of the regular expression
     * @param data  mixed Data returns by `fixtures` attribute
     */
    post: function(match, data) {
      return {
        status: 201
      }
    }
  }
]

The node env is equal to

https://example.com/api

I'm using a server setup like so

import request from "superagent"
import su from "superagent-use"
import prefix from "superagent-prefix"
import config from "../config/superagent-mock"
import superagentMock from "superagent-mock"

const agent = su(request)
agent.use(prefix(process.env.REACT_APP_API_ENDPOINT))

const logger = function(log) {
  console.log("superagent call", log)
}

export const setupMockServer = () => {
  return superagentMock(agent, config, logger)
}

And then I later in the code I'm testing make a request like so

return API.get(`/events/${eventUrl}`)
.then(response => {
  if (!response.ok) {
    throw Error(response.statusText)
  }
  return response
})
.then(response => response.body)

API here is a reference to a different file that the whole app uses

import request from "superagent"
import prefix from "superagent-prefix"

export default request.agent().use(prefix(process.env.REACT_APP_API_ENDPOINT))

An example full URL might behttps://example.com/api/events/1234

When I log out the match argument I see this in my jest terminal output

console.log('match', match)
...
match [ 'https://example.com/api',
        index: 0,
        input: 'https://example.com/api/events/1234' ]

It seems the implementation suggested in the config is not working

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions