首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于ES6导入类方法,Jest总是未定义

对于ES6导入类方法,Jest总是未定义
EN

Stack Overflow用户
提问于 2022-04-23 02:12:29
回答 1查看 150关注 0票数 1

我只是在做一个虚拟的Jest测试,它总是说:接收:未定义。

示例:

/src/断开-set/index.ts

代码语言:javascript
复制
export default class DisjointSet {
  
  public foo() {
    return true;
  }
}

/src/disjoint-set/tests

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
// 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;
EN

回答 1

Stack Overflow用户

发布于 2022-04-23 02:43:34

在您的class DisjointSet中,public foo() {}应该是foo() {}

代码语言:javascript
复制
class DisjointSet {
  foo() {
    return true;
  }
}

const obj = new DisjointSet();
console.log(obj.foo());

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

https://stackoverflow.com/questions/71976283

复制
相关文章

相似问题

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