0

Trying to unit test firebase cloud functions using ts-jest, but having trouble with a simple test.

I've installed jest for typescript, typings, and ts-jest config but I'm getting an error that says @types/jest isn't installed.

main.test.ts

/// <reference types="jest" />

import { database } from '../src/firestore';

test('Firestore is initialized', () => {
    expect(database).toBeDefined();
});

error message: Cannot find name 'test'. Do you need to install type definitions for a test runner? Try npm i @types/jest or npm i @types/mocha.ts(2582)

version: @types/jest@24.0.19

Carrie M.
  • 95
  • 1
  • 12

1 Answers1

0

It's probably that the configuration in your project is not finished at all, I followed the quickstart for firebase functions, deployed to my project and then installed the dependencies using:

npm install jest @types/jest firebase-functions-test ts-jest -D

Added the test script to my package.json

"test": "jest --watchAll --verbose=false"

and finally configured jest (jest.config.js) in my project (at my functions directory)

// this config includes typescript specific settings
// and if you're not using typescript, you should remove `transform` property
module.exports = {
    transform: {
        '^.+\\.tsx?$': 'ts-jest',
    },
    testRegex: 'src(/testing/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
    testPathIgnorePatterns: ['lib/', 'node_modules/'],
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    testEnvironment: 'node',
    rootDir: 'src',
}

After all of this, I created a testing directory inside function/src folder and wrote a simple test like this inside testing

test("should pass", () => {
    // test test lol
    expect(1).toBe(1);
});

Finally, you can take a look at my package.json file if you have any other doubts about the dependencies I used.

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "test": "jest --watchAll --verbose=false"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0"
  },
  "devDependencies": {
    "@types/jest": "^24.0.19",
    "firebase-functions-test": "^0.1.6",
    "jest": "^24.9.0",
    "ts-jest": "^24.1.0",
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },
  "private": true
}
chinoche
  • 335
  • 1
  • 8
  • Hi @chinoche, my project configuration matches the above and I'm still getting the same typescript error ‍♀️ – Carrie M. Oct 25 '19 at 07:05
  • Uhm, I'm not quite sure what could be causing this error in your dev environment, I've created a [small repo](https://github.com/chinoche/fjestest), you could clone it and test it in your environment to discard if this is an environment issue or if this is related with the project's configuration – chinoche Oct 31 '19 at 03:12