Search code examples
typescriptjestjsbabeljsts-jest

Jest + TypeScript + Babel Unknown option: .0


I've written a single integration test for some of my Fauna DB operations using Jest. Both the source code I'm trying to test (fauna.ts) and the test itself (fauna.test.ts) are TypeScript files. I've followed the official Jest "Getting Started" documentation for initial installation, babel setup, and TypeScript support.

My test source code:

import { describe, expect, test } from '@jest/globals'
import {
  createUser,
  updateUserPaymentTypeByNetlifyID,
  getNetlifyIdByStripeID,
  getStripeIDByNetlifyID,
  getNetlifyIDByEmail,
  deleteUserFauna
} from './fauna'
import PaymentType from '../enums/PaymentType'

const testNetlifyID = 'myNetlifyID'
const testStripeID = 'myStripeID'
const testEmail = '[email protected]'

describe('createUser function', () => {
  test('creates a user without error', () => {
    expect(createUser(testNetlifyID, testStripeID, testEmail)).not.toThrow()
  })
})

describe('updateUserPaymentTypeByNetlifyID function', () => {
  test("updates a users payment type to 'paypal' without error", () => {
    expect(updateUserPaymentTypeByNetlifyID(testNetlifyID, PaymentType.PAYPAL)).not.toThrow()
  })
})


// more tests...

My babel.config.js file:

module.exports = {
  presets: [[['@babel/preset-env', { targets: { node: 'current' } }]], '@babel/preset-typescript']
}

However, when I issue npm test (just calls jest), I get the following error:

% npm test                            

> [email protected] test
> jest

 FAIL  src/utils/fauna.test.ts
  ● Test suite failed to run

    [BABEL] /Users/chris/enterprise/wheelscreener/wheelscreener.com/functions/src/utils/fauna.test.ts: Unknown option: .0. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.

      at transformSync (node_modules/@babel/core/src/transform.ts:66:52)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:545:31)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:674:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:726:19)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.468 s
Ran all test suites.

Obviously it's an error relating to the babel transformation, but I haven't found any solutions anywhere else, specifically with this error "Unknown option: .0."


Solution

  • Babel alone doesn't seem to play well with transformations. Jest even mentions in their TypeScript docs that there are many caveats to using Babel with TypeScript.

    In my case, the solution was just installing ts-jest with an initial config:

    npx ts-jest config:init
    

    after issuing that command, my npm test command ran without problems!