Supertest backend, permission error while initialization

Hello,

I’m trying to test one of my backend route using jest and supertest but an error occured and i don’t know why.

Error: [forest] 🌳🌳🌳 Permissions error: Cannot retrieve the data from the Forest server. Can you check that you properly copied the Forest envSecret in the Liana initializer?

I already check in process.env if all are ok and the value of FOREST_ENV_SECRET is good. Also when i start my server in dev mode (yarn start instead of yarn test) all works well while my FOREST_ENV_SECRET is the same

router.test.js :

const models = require('../../../models');
const { last } = require('lodash');
const request = require('supertest');

const app = require('../../../app.js');
const { initFactories, clearDatabase } = require('../../../test-utils/helper');
const { getUserToken } = require('../../../test-utils/auth');

const {
  fields: {
    DISTANCE_OPTIONS,
    DISTANCE_OPTION_INPUT_FIELD,
    DISTANCE_XX_INPUT_FIELD,
    DISTANCE_YY_INPUT_FIELD,
  },
} = require('../../utils/filter-care-giver/ui');

describe('Router > Announce', () => {
  let factories;

  beforeAll(() => {
    factories = initFactories(models);
  });

  afterEach(async () => {
    await clearDatabase(models);
  });

  it('POST /forest/announce/actions/filter-care-giver', async () => {
    const { id } = await factories.institution.createOne({});

    const form = {
      [DISTANCE_OPTION_INPUT_FIELD]: last(DISTANCE_OPTIONS).field,
      [DISTANCE_XX_INPUT_FIELD]: 7,
      [DISTANCE_YY_INPUT_FIELD]: 1024,
    };

    const body = { body: { data: { attributes: { ids: [id], values: form } } } };
    await request(app)
      .post('/forest/announce/actions/filter-care-giver')
      .set('Authorization', `Bearer ${getUserToken()}`)
      .send(body)
      .expect(200);
  });
});

Ok I found an other solution based on start-server-and-test. I use .env files to setup my test environment.

I add theses scripts in my package.json :

    "start:test": "node -r dotenv/config ./server.js dotenv_config_path=.env.test",
    "test": "start-server-and-test 'yarn start:test' http://localhost:3310 'yarn test:run'",
    "test:run": "jest --watch"

And so i don’t have to import my app.js in test file

2 Likes