Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matcher is not working #80

Open
jrdn91 opened this issue Apr 10, 2019 · 1 comment
Open

matcher is not working #80

jrdn91 opened this issue Apr 10, 2019 · 1 comment

Comments

@jrdn91
Copy link

jrdn91 commented Apr 10, 2019

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

@fdubost
Copy link
Member

fdubost commented Apr 11, 2019

What's your pattern regexp? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants