How to correctly set up test.js in config.env

26 views Asked by At

I followed the steps below, and now I'm working and running my test files correctly:

  • Installed Mocha, Chai, Sinon, Supertest, and NYC.

  • Set up the testing directory with the lifecycle.test.js file and mocha.opts.

  • Configured the test script in my package.json file as follows:

      "test": "nyc node ./node_modules/mocha/bin/mocha test/lifecycle.test.js test/**/*.test.js --timeout 10000"
    

I didn't create a test.js module in config.env, and that's because when I did it, I tried to run the tests using NODE_ENV=test, and the compiler didn't recognize this command. Is it necessary for me to do that? How can I run my tests using config.env.test.js?

Some of my tests fail to connect with external services. Of course, those services have credentials to connect with them. Should I import those credentials into my test module? Should I mock all the service responses?

This is a test of one of my services, in this case BlobStorageService:

//getBlob correctly test case
describe('BlobService - getBlob correctly', function () {
    it('should get a blob', async function () {
        //Mock the BlobServiceClient
        const fromConnectionStringStub = sinon.stub(BlobServiceClient, 'fromConnectionString');
        fromConnectionStringStub.returns({
            //Setup the mock to return an object similar to the original
            getContainerClient: () => {
                return {
                    getBlockBlobClient: () => {
                        return {                                
                            //Simulate the download method, which is in charge of getting the blob
                            download: () => {
                            return Promise.resolve('download-success');
                           }
                        };
                    }
                };
            }
        });
        //Call the function we are testing and check that the result is the expected
        const response = await AzureBlobService.getBlob('test.png');
        expect(response).to.equal('download-success');

        //Restore the stub so it doesn't affect other tests
        fromConnectionStringStub.restore();
    });
});

Everything should be fine, but the test doesn't pass and shows the following message:

 BlobService - getBlob correctly
   should get a blob:
 AssertionError: expected undefined to equal 'download-success'
0

There are 0 answers