最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'use_env_variable' of undefined - Stack Overflow

programmeradmin2浏览0评论

I am using 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

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

I am using https://github./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./hhimanshu/contactz

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

Share Improve this question asked Sep 28, 2017 at 1:56 daydreamerdaydreamer 92.2k204 gold badges473 silver badges750 bronze badges 1
  • maybe there is a importing or path problem – AVI Commented Sep 28, 2017 at 2:09
Add a ment  | 

5 Answers 5

Reset to default 3

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

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"
    }
  }

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

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.

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

发布评论

评论列表(0)

  1. 暂无评论