简体   繁体   English

ts-jest 单元测试的麻烦

[英]ts-jest trouble with unit testing

Trying to unit test firebase cloud functions using ts-jest , but having trouble with a simple test.尝试使用ts-jest对 firebase 云功能进行单元测试,但在进行简单测试时遇到了问题。

I've installed jest for typescript, typings, and ts-jest config but I'm getting an error that says @types/jest isn't installed.我已经为 typescript、typings 和 ts-jest 配置安装了 jest,但我收到一条错误消息,指出未安装@types/jest

main.test.ts 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)尝试npm i @types/jestnpm i @types/mocha .ts(2582)

version: @types/jest@24.0.19版本: @types/jest@24.0.19

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:可能是您项目中的配置根本没有完成,我按照firebase 功能的快速入门,部署到我的项目,然后使用以下命令安装依赖项:

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

Added the test script to my package.json将测试脚本添加到我的 package.json

"test": "jest --watchAll --verbose=false" “测试”:“开玩笑--watchAll--verbose=false”

and finally configured jest (jest.config.js) in my project (at my functions directory)最后在我的项目(在我的函数目录)中配置了 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',
}

After all of this, I created a testing directory inside function/src folder and wrote a simple test like this inside testing毕竟,我在 function/src 文件夹中创建了一个测试目录,并在 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.最后,如果您对我使用的依赖项有任何疑问,可以查看我的 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM