xyz83242
xyz83242

Reputation: 824

JEST test on node and typescript returns: TypeError: is not a function

I am trying to run a simple function test with JEST on the Node running Typescript. I am getting not a function error and I don't understand why.

test.spec.js:

const x = require('../functions/x');

describe("Running test", () => {
  test('Should return results', async() => {
    const result = x(1,2)
    expect(result).toBe(3)
  });
});

x.ts:

export function x(a: number, b: number) {
  return a + b
}

result:

  Running test
    ✕ Should return results (2 ms)

  ● Running test › Should return results

    TypeError: x is not a function

      4 | describe("Running test", () => {
      5 |   test('Should return results', async() => {
    > 6 |     const result = x(1,2)
        |                    ^
      7 |     expect(result).toBe(3)
      8 |     /*
      9 |     const result = await brokerStatsMessagesCount()

Upvotes: 0

Views: 4713

Answers (1)

leal32b
leal32b

Reputation: 394

As you are running your project with typescript, your test files should be in .ts too (maybe you'll need to adjust jest config to make it run properly).

Try to export the x function this way in x.ts:

export const x = (a: number, b: number) => {
  return a + b
}

Change test.spec.js to test.spec.ts and import x this way:

import { x } from '../functions/x'

Upvotes: 1

Related Questions