I can’t unit test my Google Cloud Functions in online mode (as defined in the instructions that I followed: https://firebase.google.com/docs/functions/unit-testing): if I navigate to the functions
folder and run npm run test
, I get:
> test
> mocha --reporter spec
Error: No test files found
The problem could be with the fact that I’m using writing both test and production code in Typescript, while the documentation I followed only focuses on Javascript.
Here is part of my project structure, if it makes sense:
I can call functions just fine from my UIKit app, even using the emulator suite.
Here is an example function in Typescript:
import { Request, Response } from 'express';
export const helloWorld = (req: Request, res: Response) => {
res.send('Hello, World!');
};
And here is an example test in Typescript:
// use your actual details
const test = require('firebase-functions-test')({
databaseURL: 'https://my-project.firebaseio.com',
storageBucket: 'my-project.appspot.com',
projectId: 'my-project',
}, 'path/to/serviceAccountKey.json');
import * as mocha from "mocha";
import * as chai from "chai";
// be sure to only do this after initializing firebase-functions-test, and mocking config values.
import * as functions from "../src/index";
import { Request, Response } from 'express';
describe('helloWorld function', () => {
it('should send "Hello, World!"', () => {
const req = {} as Request;
const res = {
send: (message: string) => {
chai.expect(message).to.equal('Hello, World!');
}
} as Response;
functions.helloWorld(req, res);
});
});
These posts only gave rise to other problems for me: mocha Error: No test files found: “test/” npm ERR! Test failed.
No test files found: “test” when testing security rules using Mocha using Typescript