javascriptreactjsmiragejs

Miragejs seeds function not called


I'm trying to set-up a miragejs server, but for some reason, the seeds function is not called. The other server function, routes, is called and the routes are mapped correctly.

export default function makeServer({ environment = 'test' } = {}) {
  return new Server({
    environment,

    models: {
      user: Model,
      diary: Model,
    },

    seeds(server) {
      debugger; // not hit
      seedUsers(server);
      seedDiaries(server);
    },

    routes() {
      debugger; // hit
      this.namespace = 'api';
      this.get('/users', (schema) => schema.users.all());
      this.get('/diaries', (schema) => {
        debugger; // hit
        schema.diaries.all();
      });
    },
  });
}

I've tried copy-pasting the provided example, but the seeds method is still not called.

The miragejs package is installed as a dev package and the used version is 0.1.40. The makeServer function is called in the index.jsx file like this:

if (process.env.NODE_ENV === 'development') {
  makeServer();
}

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <App />
    </ConnectedRouter>
  </Provider>,
  // eslint-disable-next-line no-undef
  document.getElementById('root'),
);


Solution

  • Changing the makeServer function's environment parameter to development or removing it entirely (it defaults to development) solves the issue. It seems that when using the test environment, the seeds method is ignored (source).