Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking with jest doesn't work #713

Open
bennyk8y opened this issue May 29, 2023 · 9 comments
Open

Mocking with jest doesn't work #713

bennyk8y opened this issue May 29, 2023 · 9 comments

Comments

@bennyk8y
Copy link

bennyk8y commented May 29, 2023

Hi, this is jest.config.template.js:

module.exports = {
  testEnvironment: 'node',
  testMatch: ['**/*.test.ts', '**/**/**/*.test.ts', '**/**/**/*.spec.ts'],
  transform: {
    '^.+\\.ts?$': [
      '@swc-node/jest',
      {
        sourcemap: true,
        experimentalDecorators: true,
        emitDecoratorMetadata: true,
        keepClassNames: true,
      },
    ],
  },
};

this is the test file:

import { getBankTransactionOrFail } from '../../models/queries/getBankTransaction';

jest.mock('../../models/queries/getBankTransaction');

describe('fetchRelatedTransactionsForClassification', () => {

  it('no related entities', async () => {
    (getBankTransactionOrFail as jest.Mock).mockResolvedValueOnce(makeBankTransaction());
    expect(1).toBe(1);
});

result an error of TypeError: _getBankTransaction.getBankTransactionOrFail.mockResolvedValueOnce is not a function.

using ts-jest preset works as expected

versions:
"@swc-node/jest": "1.6.5",
"jest": "28.1.3"

@rosmcmahon
Copy link

similar issues with Sinon stub/spy:
TypeError: Descriptor for property myFunctionName is non-configurable and non-writable

@meskill
Copy link
Contributor

meskill commented Jun 15, 2023

The issue is related to #700

In previous major version @swc-node/jest@1.5.5 mocks are working right

I'm expecting that #714 will fix the problem

@meskill
Copy link
Contributor

meskill commented Jun 27, 2023

Please, check the latest version

@rosmcmahon
Copy link

no change for Sinon

@fatso83
Copy link

fatso83 commented Aug 9, 2023

@rosmcmahon I wrote up a small answer on SO on how you can generally use Sinon alongside Quibble (or any other similar tool, like Proxyquire) to mock modules when running SWC. Assumes your target module type is "commonjs".

@rosmcmahon
Copy link

yeah, catching up on this again. basically sinon recognising that swc is building the module exports in a more correct way, but that it's out of scope for their project to support this.

@fatso83
Copy link

fatso83 commented Aug 11, 2023

The problem is that Jest is a combination of many things:

  • test runner (a la Mocha)
  • stubs and spies (a la Sinon)
  • module interception (a la Proxyquire)

By itself, nether Jest nor Sinon can know that an object is a module with exports and what you want to do with it 🤷 Both require the user to know what they are looking at.

@rosmcmahon
Copy link

thanks @fatso83, sorry for the delay.
yeah, i was adding swc to an older codebase of mine. the solution was to remove swc, and revert to transpileOnly with type-checking step.
IMO if i'm using proxyquire then i'm doing something wrong as i should write my code with testing in mind. i even avoid stubs these days

@fatso83
Copy link

fatso83 commented Sep 26, 2023

In the article I have out for review I discuss three different approaches to exactly this issue, one of which is the tool independent route of exposing ways of injecting dependencies. I guess you are doing something similar to what I propose there?

Would really like some extra set of eyes on this, so if you could spare five minutes I can perhaps help quite a few people in avoiding the trouble you experienced 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants