typescript - Jest TypeError:无法读取未定义的属性“绑定”
问题描述
我正在尝试在测试中执行以下代码:
import Airtable from "airtable";
const base: string = 'xxx'
const getClient = () => new Airtable({apiKey: 'xxx'});
export async function getAirtableData(): Promise<string[]> {
return []
}
我的测试:
test('Airtable to ElasticSearch', async () => {
expect(await getAirtableData()).toBe([])
});
但是当我运行测试(使用 Jest)时,出现以下错误:
TypeError: Cannot read property 'bind' of undefined
ts-node
编辑:使用不使用 Jest手动运行代码就可以了。
Google 只返回与我没有使用的 React 相关的问题。可能是什么问题?
解决方案
我建议你看看你的打字稿配置,
以下对我有用:
tsconfig.json
:
"compilerOptions": {
"lib": ["ES2019"],
"target": "ES2019",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
package.json
:
{
"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
:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
现在使用您的代码示例,我的main.ts
:
import Airtable from "airtable";
const base: string = 'xxx'
const getClient = () => new Airtable({apiKey: 'xxx'});
export async function getAirtableData(): Promise<string[]> {
return []
}
和main.test.ts
:
import {getAirtableData} from './main'
test('Airtable to ElasticSearch', async () => {
expect(await getAirtableData()).toEqual([])
});
它按预期工作:
推荐阅读
- javascript - 安装后运行计算函数 - VUE
- socket.io - 使用 VMware Fusion 无法连接羽毛聊天远程访问
- c# - 使用接收 2 个参数的实体框架更新记录
- json - Angular 6 - 如何停止生成规范文件
- couchbase - Couchbase 索引和 N1QL 查询
- macos - 如何删除某个目录(在 Mac 终端中)-unix 中文件名的最后 n 个字符?
- java - 无法从 Jenkins 将工件部署到 Nexus 存储库
- python - 计算for循环中的迭代次数
- google-cloud-platform - Spark HBase 到 Google Dataproc 和 Bigtable 的迁移
- c++ - 如何通过指向非易失性的指针接收易失性?