简体   繁体   中英

How to test Sequelize in Jasmine?

I initialize a sequelize db in the database/index.ts file with the following code:

import { Sequelize } from 'sequelize';

const {
  DATABASE_DIALECT,
  DATABASE_HOST,
  DATABASE_PORT,
  DATABASE_USER_NAME,
  DATABASE_USER_PASSWORD,
  DATABASE_NAME,
  ENVIRONMENT
} = process.env;

const sequelize = new Sequelize({
  host: DATABASE_HOST,
  port: +(DATABASE_PORT as string),
  dialect: DATABASE_DIALECT as 'postgres',
  database: DATABASE_NAME,
  username: DATABASE_USER_NAME,
  password: DATABASE_USER_PASSWORD,
  logging:
    (ENVIRONMENT as string) === 'production' || (ENVIRONMENT as string) === 'acceptance'
      ? false
      : console.log,
  query: {
    raw: true // Receive a plain object/array as a response from Sequelize (default response contains a lot of Sequelize metadata)
  }
});


export default sequelize;

I need to test the logging property cause it determines by the ternary operator that checks ENV. To test this, I need to check arguments with whose new Sequelize(...) is called.

How this could be done in Jasmine? I read a lot of articles and looks like we can't spy on the Sequelize class itself to check call parameters.

I've not used Sequelize a great deal, but the value passed for logging seems to end up under sequelize.options.logging (based on https://github.com/sequelize/sequelize/blob/main/lib/sequelize.js#L277 ). So referring to sequelize.options.logging in tests might be the quickest approach.


Another approach might be to store the object literal (that's passed to the Sequelize constructor) in a variable. Then export the variable (to make it available in tests) and also pass the variable to Sequelize (to continue with the instantiation). Something like:

export const sequelizeOptions = {
  host: DATABASE_HOST,
  port: +(DATABASE_PORT as string),
  dialect: DATABASE_DIALECT as 'postgres',
  database: DATABASE_NAME,
  username: DATABASE_USER_NAME,
  password: DATABASE_USER_PASSWORD,
  logging:
    (ENVIRONMENT as string) === 'production' || (ENVIRONMENT as string) === 'acceptance'
      ? false
      : console.log,
  query: {
    raw: true // Receive a plain object/array as a response from Sequelize (default response contains a lot of Sequelize metadata)
  }
};

const sequelize = new Sequelize(sequelizeOptions);

export default sequelize;

In theory, the above should work the same as what you have now. Only difference it captures and exposes the object.


Another approach might be to instead create a thin wrapper around console.log so that you can spy on the wrapper -- and then pass the wrapper as part of the constructor options:

export const logger = (...args: any[]) => console.log(...args);

const sequelize = new Sequelize({
  host: DATABASE_HOST,
  port: +(DATABASE_PORT as string),
  dialect: DATABASE_DIALECT as 'postgres',
  database: DATABASE_NAME,
  username: DATABASE_USER_NAME,
  password: DATABASE_USER_PASSWORD,
  logging:
    (ENVIRONMENT as string) === 'production' || (ENVIRONMENT as string) === 'acceptance'
      ? false
      : logger,
  query: {
    raw: true // Receive a plain object/array as a response from Sequelize (default response contains a lot of Sequelize metadata)
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM