Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'),
);

like image 366
mccuna Avatar asked Jan 24 '26 11:01

mccuna


1 Answers

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).

like image 187
mccuna Avatar answered Jan 27 '26 01:01

mccuna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!