首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Jest测试包含未定义函数的代码

使用Jest测试包含未定义函数的代码
EN

Stack Overflow用户
提问于 2020-05-31 15:57:31
回答 1查看 522关注 0票数 0

我正在尝试使用Jest为我拥有的一些JavaScript代码编写单元测试。问题是代码文件包含未定义或未导入的函数,因此当我尝试导入要测试的文件时,Jest抛出有关未定义函数的错误。有什么办法可以绕过这个问题吗?例如,只导入我想要测试的函数?

下面是我要测试的代码的文件片段:

代码语言:javascript
运行
复制
// run any data migrations
on("sheet:opened", () => {
    sheetMigration();

    getAttrs(["btatow_sheet_version"], ({
        btatow_sheet_version
    }) => {
        if (btatow_sheet_version >= 3) {
            recalculateSkills();
        }
    });
});

...

// calculate stat values when XP amount changes
on("change:strength_xp change:body_xp change:reflex_xp change:dexterity_xp change:intelligence_xp change:will_xp change:charisma_xp change:edge_xp", calculateAbilityScore)

const calculateLinkedAttributeValue = attribute => {
    if (attribute > 10) {
        return Math.floor(attribute / 3);
    } else {
        if (attribute < 1)
            return -4;
        else if (attribute < 2)
            return -2;
        else if (attribute < 4)
            return -1;
        else if (attribute < 7)
            return 0;
        else if (attribute < 10)
            return 1;
        else
            return 2;
    }
}

...

// exports for testing
module.exports = calculateLinkedAttributeValue

以下是测试文件中的代码:

代码语言:javascript
运行
复制
const calculateLinkedAttributeValue = require('./sheet-worker')

test('should calculate linked attribute value for attribute value of 0', () => {
    expect(calculateLinkedAttributeValue(0)).toBe(-4)
})

我设置了一个package.json文件,并引入了Jest作为依赖项,如下所示:

代码语言:javascript
运行
复制
{
  "name": "battletech-a-time-of-war",
  "version": "1.0.0",
  "description": "Character sheet for Roll20 for the A Time of War TTRPG system.",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jest": "^26.0.1"
  },
  "type": "module",
  "sourceType": "module"
}

尝试通过命令行运行测试会产生以下输出:

代码语言:javascript
运行
复制
C:\Stuff\Development\roll20-character-sheets\BattleTech-A-Time-of-War\development>npm run test

> battletech-a-time-of-war@1.0.0 test C:\Stuff\Development\roll20-character-sheets\BattleTech-A-Time-of-War
> jest

 FAIL  development/sheet-worker.test.js
  ● Test suite failed to run

    ReferenceError: on is not defined

      1 | // run any data migrations
    > 2 | on("sheet:opened", () => {
        | ^
      3 |     sheetMigration();
      4 |
      5 |     getAttrs(["btatow_sheet_version"], ({

      at Object.<anonymous> (development/sheet-worker.js:2:1)
      at Object.<anonymous> (development/sheet-worker.test.js:1:39)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.226 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! battletech-a-time-of-war@1.0.0 test: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the battletech-a-time-of-war@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\<User>\AppData\Roaming\npm-cache\_logs\2020-06-01T09_59_15_484Z-debug.log

编辑:添加示例并删除指向GitHub源代码的链接。

EN

回答 1

Stack Overflow用户

发布于 2020-06-05 20:58:23

在互联网上搜索了一段时间并尝试了不同的方法后,我能够确定问题的原因。它实际上与Jest无关,它是Node require()函数。我从this StackOverflow comment中发现,Node require()函数基本上运行您要导入的文件中的所有代码。因此,它试图运行未定义的函数,但出现了错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62113191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档