尝试使用ts-jest
对Firebase云功能进行单元测试,但是在进行简单测试时遇到了麻烦。
我已经为打字稿,打字和ts-jest配置安装了jest,但是却收到一条错误消息,提示未安装@types/jest
。
main.test.ts
/// <reference types="jest" />
import { database } from '../src/firestore';
test('Firestore is initialized', () => {
expect(database).toBeDefined();
});
错误消息::找不到名称“ test”。您是否需要为测试运行程序安装类型定义?尝试npm i @types/jest
或npm i @types/mocha
。ts(2582)
版本: @types/jest@24.0.19
答案 0 :(得分:0)
您的项目中的配置可能根本没有完成,我遵循quickstart for firebase functions,部署到我的项目中,然后使用以下命令安装了依赖项:
npm install jest @ types / jest firebase-functions-test ts-jest -D
将测试脚本添加到我的package.json
“ test”:“开玩笑--watchAll --verbose = false”
并最终在我的项目(在我的功能目录中)中配置了jest(jest.config.js)
// 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',
}
所有这些之后,我在function / src文件夹中创建了一个测试目录,并在测试中编写了一个像这样的简单测试
test("should pass", () => {
// test test lol
expect(1).toBe(1);
});
最后,如果您对我使用的依赖项还有其他疑问,可以看看我的package.json文件。
{
"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
}