0

Here is my code:

babel.config.cjs
module.exports = {
    plugins: ['@babel/plugin-transform-modules-commonjs'],
    presets: [['@babel/preset-env', { targets: { node: 'current' } }]]
  }


jest.config.cjs
module.exports = {
  globals: {
    "ts-jest": {
      isolatedModules: true,
    },
  },
};


Setup.ts
import awilix from 'awilix';
import express from 'express';
import CognitoSC from './Service/Cognito.js'
import authController from './Controller/Auth.js';

const app = express();
const container = awilix.createContainer({
  injectionMode: awilix.InjectionMode.CLASSIC
});


auth.test.ts
import request from 'supertest';
import app from '../Setup.js';

describe('Testing Authentication/Authorization', function() {
    it('responds with json', async function() {
      const response = await request(app)
        .post('/signin')
        .send('username=Test')
        .send('password=Testtest123$')
      expect(response.status).toEqual(200);
      expect(response.body.data.success).toEqual(true);
    });
  });

and I build it with tsc ("module": "es2022"). When I run npx jest I get an error saying

TypeError: Cannot read properties of undefined (reading 'createContainer')

>  8 | const container = awilix.createContainer({

Interesting thing that I noticed is that if I open Setup.js file which is generated by tsc and change the code from

import awilix from 'awilix';

to

const awilix = require('awilix');

and run npx jest, the tests pass without a problem. I'm little lost and can't figure out what is the problem. I also checked inside Setup.js and express is imported without a problem using ES6 syntax but why it can't do the same with awilix?

Jakomo
  • 71
  • 6

1 Answers1

0

It doesn't appear that awilix supports modules.

From their readme they say to use const awilix = require('awilix') .

See here for more information about import/require/ES modules/commonJS.

Sam Dean
  • 433
  • 7
  • 22