Skip to content
Advertisement

Mocha – Cannot find module ‘./Collection/auth.js’ after adding the helper file in a nested folder

My folders structure is as below:

App
|__ test
    |__ Helpers
        |__ Collection
            |__ auth.js
        |__ index.js
    |__ Specs
        |__ login.js

That’s the content of the index file

'use strict';

module.exports = {
    auth: require('./Collection/auth'),
};

And this the content of the spec file

'use strict';

const { auth } = require('../Helpers');

describe('User can login successfully', () => {
    it('should return success response',async() => {
        console.log(auth);
        const response = await auth.login({
            phone: '01112223330',
            password: '123',
        });

       console.log(response.error);
    });
});

I’m not sure why I’m getting the below error, I see this is supposed to work.

rror: Cannot find module './Collection/auth.js'
Require stack:
- /x-app/test/Helpers/index.js
- /x-app/test/Specs/loginSpec.js
- /x-app/node_modules/mocha/lib/esm-utils.js
- /x-app/node_modules/mocha/lib/mocha.js
- /x-app/node_modules/mocha/lib/cli/one-and-dones.js
- /x-app/node_modules/mocha/lib/cli/options.js
- /x-app/node_modules/mocha/bin/mocha

In the specs file, it is shown that the required auth has the correct value enter image description here


Tests was working fine when the structure was

App
|__ test
    |__ Helpers
        |__ auth.js
        |__ index.js
    |__ Specs
        |__ login.js

and auth is required in index.js as auth:require('./auth'),

Advertisement

Answer

It was a problem in the docker file that wasn’t detecting the code changes. It isn’t a code-related problem.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement