daydreamer
daydreamer

Reputation: 91949

Cannot read property 'use_env_variable' of undefined

I am using https://github.com/sequelize/sequelize to learn using the database migrations. This works fine.

As a next step, I would like to be able to run tests. My test is very simple (not using anything related to Sequelize) and looks like

import request from 'supertest';
import app from '../src/app.js';

describe('GET /', () => {
  it('should render properly', async () => {
    await request(app).get('/').expect(200);
  });
});

describe('GET /404', () => {
  it('should return 404 for non-existent URLs', async () => {
    await request(app).get('/404').expect(404);
    await request(app).get('/notfound').expect(404);
  });
});

When I run this as npm test, I get error as

➜  contactz git:(master) ✗ npm test

> [email protected] test /Users/harit/bl/sources/webs/q2/contactz
> jest

 FAIL  test/routes.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'use_env_variable' of undefined

      at Object.<anonymous> (db/models/index.js:11:11)
      at Object.<anonymous> (src/routes.js:3:14)
      at Object.<anonymous> (src/app.js:4:15)
      at Object.<anonymous> (test/routes.test.js:2:12)
          at Generator.next (<anonymous>)
          at Promise (<anonymous>)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.702s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
➜  contactz git:(master) ✗

I am new to NodeJS, Express so not sure what is going wrong here.
The code is available at https://github.com/hhimanshu/contactz

Could someone please help me know what's wrong and how to fix it?

Upvotes: 2

Views: 7604

Answers (5)

Kaddu Livingstone
Kaddu Livingstone

Reputation: 1594

Export your config.js file for example check this below

module.exports ={
    "development": {
      "username": "root",
      "password": null,
      "database": "database_development",
      "host": "127.0.0.1",
      "dialect": "mysql"
    },
    "test": {
      "username": "root",
      "password": null,
      "database": "database_test",
      "host": "127.0.0.1",
      "dialect": "mysql"
    },
    "production": {
      "username": "root",
      "password": null,
      "database": "database_production",
      "host": "127.0.0.1",
      "dialect": "mysql"
    }
  }

Upvotes: 1

Stefani
Stefani

Reputation: 162

I got this error today and fixed it.

When running jest, by default it will set the NODE_ENV (process.env.NODE_ENV) to "test".

If you generated basic sequelize files using sequelize-cli, in your sequelize config.json file there should be (by default) three types of config options used which are: "development", "test", and "production". However I see that you are using your own config.js file and there is no "test" option there, only development and production https://github.com/hhimanshu/contactz/blob/master/db/config.js

You are receiving this error as the model/index.js file is looking for said "test" option but finds it undefined instead. Specifically, at line 12 of https://github.com/hhimanshu/contactz/blob/master/db/models/index.js it will find the config as undefined, and throw error looking for undefined's use_env_variable property.

The solution is to add "test" config to your config.js file. Just copy the config you used for development and it should run.

*note that you should probably set a different db for each of the different types of config, but this is another topic.

Upvotes: 3

Glenn Posadas
Glenn Posadas

Reputation: 13283

I'm a newbie in server stuff, specifically heroku + node express + sequelize. So in my case, my config var on Heroku settings was wrong.

NODE_ENV was set to a wrong value like live instead of production.

The correct value production must be equal to your config.js, assuming you're using Sequelize too.

Upvotes: 0

Agent
Agent

Reputation: 1395

I simply used javascript trim()

  • before
 process.env.NODE_ENV()                // return    "development "   with space
  • After
 process.env.NODE_ENV.**trim()**                // "development"  no space

Here is my solution

Upvotes: 0

Tarik Moustaid
Tarik Moustaid

Reputation: 9

instead of npm test, can you try this command ? jest yourfile.js --env=node --runInBand

Upvotes: -1

Related Questions