Home > Enterprise >  Defining variable within function in jest
Defining variable within function in jest

Time:08-26

So I just started using jest and I am trying to figure out a difficult problem.

I am working on a Strapi plugin and I am trying to test this function

module.exports = {
  async index(ctx) {
    let verification = {}
    // Checks if there is a captcha provider
    console.log('strapi'   strapi)
    if (!(strapi.config.get('plugin.ezforms.captchaProvider.name') === 'none') && (strapi.config.get('plugin.ezforms.captchaProvider.name'))) {
      verification = await strapi.plugin('ezforms').service(strapi.config.get('plugin.ezforms.captchaProvider.name')).validate(ctx.request.body.token)
      //throws error if invalid
      if (!verification.valid) {
...

The issue is that the strapi object is injected when running strapi and I want to know how I can inject a fake variable into this function.

I've already implemented a fake strapi object in my test and it looks something like this

  test('should return captcha error', async function () {
    strapi.plugin().service().validate = jest.fn(function () {
      return {
        error: {
          valid: false,
          message: 'Unable to verify captcha',
          code: 500
        }
      }
    })
    let result = await submitController.index(ctx)

    expect(result).toEqual(500)

My issue right now is that when running index() it doesn't have a reference to the Strapi object

describe('Submit Controller', function () {
  let strapi
  beforeAll(async function () {
    strapi = {
      ...
      plugin: function () {
        return {
          service: function () {
            return {
              validate: function () {
                return {
                  error: {
                    valid: false,
                    message: 'Unable to verify captcha',
                    code: 500
                  }
                }
              }
            }
          }
        }
      }
    }
  })```

I reference in the test file but the `index()` function doesn't have access

[![screenshot][1]][1]

How can I inject my fake Strapi object into the index() function
 


  [1]: https://i.stack.imgur.com/rCc7N.png

CodePudding user response:

Right now it seems that strapi is a free variable for index() method. Due to lexical scope index() method has an access to free variables which were in a scope when the method is defined.

I do not know a structure of your project but as a solution I would recommend to explicitly pass a strapi object as a parameter in index method

module.exports = {
  async index(ctx, strapi) {
    let verification = {}
    // Checks if there is a captcha provider
    console.log('strapi'   strapi)
    if (!(strapi.config.get('plugin.ezforms.captchaProvider.name') === 'none') && (strapi.config.get('plugin.ezforms.captchaProvider.name'))) {
      verification = await strapi.plugin('ezforms').service(strapi.config.get('plugin.ezforms.captchaProvider.name')).validate(ctx.request.body.token)
      //throws error if invalid
      if (!verification.valid) {
...

CodePudding user response:

Turns out you can pass the strapi object in the root of the module export.

module.exports = ({ strapi }) => ({
  async index(ctx) {
...

Then in your test you can do

let result = await submitController({strapi}).index(ctx)
  • Related