mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.63k stars 3.02k forks source link

🐛 Bug: Unit test cases taking too much time to execute #5133

Closed Nice-Makarand closed 7 months ago

Nice-Makarand commented 8 months ago

Bug Report Checklist

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.

JoshuaKGoldberg commented 7 months ago

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!