我只是在做一个虚拟的Jest测试,它总是说:接收:未定义。
示例:
/src/断开-set/index.ts
export default class DisjointSet {
public foo() {
return true;
}
}/src/disjoint-set/tests
import DisjointSet from '..';
describe('It tests Disjoint DataScture', () => {
it('Should create a Disjoint Set', () => {
const disSet = new DisjointSet();
expect(disSet).toBeTruthy();
expect(disSet.foo()).toBe(true);
});
});我的控制台输出:
测试不相交的src/data-structures/disjoint-set/tests/disjoint-set.test.ts
✕应该创建一个不相交的集合(5ms)
它测试不相交的DataScture >应该创建一个不相交的集合
预期(接收).toBe(预期) //预期的Object.is等式: true接收:未定义的7维expect(disSet).toBeTruthy();8欧元>9#expect(disSet.foo()).toBe(真);\x^ at Object。(src/data-structures/disjoint-set/__tests__/disjoint-set.test.ts:9:26)
我的package.json
{
"name": "leetcode",
"version": "1.0.0",
"description": "This is to test my data structure and algorithms in JavaScript",
"main": "index.js",
"scripts": {
"test": "jest",
"build": "tsc -p tsconfig.build.json"
},
"repository": {
"type": "git",
"url": ""
},
"keywords": [
"leetcode",
"javascript"
],
"author": "",
"license": "MIT",
"bugs": {
"url": ""
},
"homepage": "",
"devDependencies": {
"@types/jest": "^27.4.1",
"jest": "^27.5.1",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3"
},
"dependencies": {
"node-fetch": "^2.6.7",
"ts-node": "^10.7.0"
}
}我的tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"types": ["node", "jest"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"baseUrl": ".",
"noFallthroughCasesInSwitch": true,
"outDir": "./dist",
"jsx": "preserve"
},
"compileOnSave": true,
"include": ["."]
}我的jest.config.ts
// jest.config.ts
import type { Config } from "@jest/types"
const config = {
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
automock: true,
testPathIgnorePatterns: ['/dist']
}
export default config;发布于 2022-04-23 02:43:34
在您的class DisjointSet中,public foo() {}应该是foo() {}。
class DisjointSet {
foo() {
return true;
}
}
const obj = new DisjointSet();
console.log(obj.foo());
https://stackoverflow.com/questions/71976283
复制相似问题