首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jest TypeError:无法读取未定义的属性“”bind“”

Jest TypeError:无法读取未定义的属性“”bind“”
EN

Stack Overflow用户
提问于 2021-09-10 23:19:25
回答 1查看 178关注 0票数 0

我尝试在一个测试中执行以下代码:

代码语言:javascript
运行
复制
import Airtable from "airtable";

const base: string = 'xxx'
const getClient = () => new Airtable({apiKey: 'xxx'});

export async function getAirtableData(): Promise<string[]> {
    return []
}

我的测试:

代码语言:javascript
运行
复制
test('Airtable to ElasticSearch', async () => {
  expect(await getAirtableData()).toBe([])
});

但是当我运行测试(使用Jest)时,我得到以下错误:

代码语言:javascript
运行
复制
TypeError: Cannot read property 'bind' of undefined

编辑:在不使用Jest的情况下使用ts-node手动运行代码很好。

Google只返回与React相关的问题,我没有使用它。可能的问题是什么?

EN

回答 1

Stack Overflow用户

发布于 2021-09-12 13:12:53

我建议你看看你的typescript配置,

下面的方法对我很有效:

tsconfig.json

代码语言:javascript
运行
复制
  "compilerOptions": {
    "lib": ["ES2019"],
    "target": "ES2019",
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

package.json

代码语言:javascript
运行
复制
{
  "devDependencies": {
    "@types/jest": "27.0.1",
    "airtable": "^0.10.1",
    "jest": "27.1.1",
    "ts-node": "10.2.1",
    "typescript": "4.4.3",
    "ts-jest": "27.0.5"
  }
}

jest.config.js

代码语言:javascript
运行
复制
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

现在使用您的代码示例,我的main.ts

代码语言:javascript
运行
复制
import Airtable from "airtable";

const base: string = 'xxx'
const getClient = () => new Airtable({apiKey: 'xxx'});

export async function getAirtableData(): Promise<string[]> {
    return []
}

main.test.ts

代码语言:javascript
运行
复制
import {getAirtableData} from './main'

test('Airtable to ElasticSearch', async () => {
  expect(await getAirtableData()).toEqual([])
});

它的工作方式与预期一致:

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

https://stackoverflow.com/questions/69138834

复制
相关文章

相似问题

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