mochajs / mocha

β˜•οΈ simple, flexible, fun javascript test framework for node.js & the browser

Home Page:https://mochajs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ› Bug: Unit test cases taking too much time to execute

Nice-Makarand opened this issue Β· comments

Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have 'smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this

Expected

Expecting typescript test cases to execute faster or at usual pace.

Actual

Only typescript Unit test cases are taking time to execute which is not the case with javascript unit test cases.

Minimal, Reproducible Example

Typescript test case

import { expect } from 'chai';
import { SinonStub, stub } from 'sinon';
import { getItemFromDynamoDB } from './yourDynamoDBFunctions';
import { DynamoDB } from 'aws-sdk';

describe('getItemFromDynamoDB', () => {
  let getStub: SinonStub;

  beforeEach(() => {
    getStub = stub(DynamoDB.DocumentClient.prototype, 'get');
  });

  afterEach(() => {
    getStub.restore();
  });

  it('should retrieve an item from DynamoDB based on primary key', async () => {
    const mockItem = { id: '123', name: 'Test Item' };
    const primaryKey = { Key: { id: '123' }, TableName: 'YourTableName' };

    getStub.returns({
      promise: () => Promise.resolve({ Item: mockItem }),
    });

    const result = await getItemFromDynamoDB(primaryKey);

    expect(result).to.deep.equal(mockItem);
    expect(getStub.calledOnceWithExactly(primaryKey)).to.be.true;
  });
});
 

Javascript test case

const { expect } = require('chai');
const sinon = require('sinon');
const { getItemFromDynamoDB } = require('./yourDynamoDBFunctions');
const AWS = require('aws-sdk');

describe('getItemFromDynamoDB', () => {
  let getStub;

  beforeEach(() => {
    getStub = sinon.stub(AWS.DynamoDB.DocumentClient.prototype, 'get');
  });

  afterEach(() => {
    getStub.restore();
  });

  it('should retrieve an item from DynamoDB based on primary key', async () => {
    const mockItem = { id: '123', name: 'Test Item' };
    const primaryKey = { Key: { id: '123' }, TableName: 'YourTableName' };

    getStub.returns({
      promise: () => Promise.resolve({ Item: mockItem }),
    });

    const result = await getItemFromDynamoDB(primaryKey);

    expect(result).to.deep.equal(mockItem);
    expect(getStub.calledOnceWithExactly(primaryKey)).to.be.true;
  });
});

The problem here is, actual test case execution time is similar, however,
time taken after command

_mocha -r ts-node/register test.ts file path

is too much, which is not the case in JS.

Versions

"mocha": "10.0.0",
"typescript": "4.5.2"
"ts-node": "10.4.0"
"sinon": "11.1.2"
"@types/mocha": "9.0.0"
"@types/sinon": "10.0.14"

node version > 18

Additional Info

It has been observed that there is no problem with mocking. Because test case execution happens in no time, it is heavy loading before the execution that is bothering me.
Average time taken to execute entire TS test cases is 3 min, in which 1 second goes for execution.

Looks like ts-node might be running TypeScript type checking as part of your unit tests. https://typestrong.org/ts-node/docs/options/#typechecking has more info around options in that area. This is probably an issue with how you're using ts-node.

If you've spotted a bug in Mocha that you can reproduce without integrations, please do post back. But I'm going to close this for now as external. Thanks!