我尝试在一个测试中执行以下代码:
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编辑:在不使用Jest的情况下使用ts-node手动运行代码很好。
Google只返回与React相关的问题,我没有使用它。可能的问题是什么?
发布于 2021-09-12 13:12:53
我建议你看看你的typescript配置,
下面的方法对我很有效:
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([])
});它的工作方式与预期一致:

https://stackoverflow.com/questions/69138834
复制相似问题