标签: jestjs

Travis / Jest:TypeError:无法分配给对象“#<process>”的只读属性“ Symbol(Symbol.toStringTag)”

我的所有jest typescript测试都在travis管道中失败,并引发以下错误:

TypeError: Cannot assign to read only property 'Symbol(Symbol.toStringTag)' of object '#<process>'
Run Code Online (Sandbox Code Playgroud)

突然发生了,我没有更改代码中的任何特定内容。在本地,一切正常。

任何想法会发生什么?

javascript travis-ci typescript reactjs jestjs

47
推荐指数
2
解决办法
4731
查看次数

安装 jest 时未定义 describe

jest v24.7.1在我的项目中安装了:

npm install jest -D
Run Code Online (Sandbox Code Playgroud)

然后我开始编写一些测试文件,但是我收到了这些 eslint 错误:

'describe' is not defined. eslint (no-undef)
'it' is not defined. eslint (no-undef)
'expect' is not defined. eslint (no-undef)
Run Code Online (Sandbox Code Playgroud)

eslintrc.js:

module.exports = {


env: {
    browser: true,
    es6: true
  },
  extends: ["airbnb", "prettier", "prettier/react"],
  globals: {
    Atomics: "readonly",
    SharedArrayBuffer: "readonly"
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true
    },
    ecmaVersion: 2018,
    sourceType: "module"
  },
  plugins: ["react"],
  rules: {}
};
Run Code Online (Sandbox Code Playgroud)

我应该添加另一个规则或插件来解决这个问题吗?

eslint jestjs eslint-config-airbnb eslintrc

47
推荐指数
2
解决办法
2万
查看次数

编写使用Flow验证的Jest测试的正确方法是什么?

我想人们通常会一起使用Flow和Jest(和React),但Flow似乎并不知道Jest(或Jasmine)全局变量.当我添加// @flow到我的测试时,我得到像这样的Flow错误:

src/__tests__/Thing-test.js:3
  3: jest.unmock('../Thing')
     ^^^^ identifier `jest`. Could not resolve name

src/__tests__/Thing-test.js:7
  7: describe('Thing', () => {
     ^^^^^^^^ identifier `describe`. Could not resolve name

src/__tests__/Thing-test.js:8
  8:   it('does stuff', () => {
       ^^ identifier `it`. Could not resolve name
Run Code Online (Sandbox Code Playgroud)

我可以为Jest/Jasmine编写一个Flow接口,但这似乎很冗长,就像我必须遗漏一些东西.让流程node_modules/jest-cli似乎没有帮助.

jestjs flowtype

46
推荐指数
3
解决办法
1万
查看次数

如何运行覆盖单个文件的 Jest 测试

我只想获得我目前正在处理的一个文件的覆盖率报告。

拥有整个应用程序文件的完整覆盖表,然后搜索我需要的文件,有点让人不知所措。

我尝试的是对一个文件运行测试并添加--coverage. 但它显示了所有文件的覆盖范围:

包.json

...
"test": "react-scripts test",
...
Run Code Online (Sandbox Code Playgroud)

我的命令

npm test my-component.test --coverage

是否可以向此命令添加一个选项以仅显示my-component.tsx覆盖范围?

typescript reactjs jestjs react-scripts

46
推荐指数
1
解决办法
7万
查看次数

在描述和开玩笑之间有什么区别?

在使用describe()时,在jest或jasmine中编写单元测试时?你什么时候用它()?

我经常这样做

describe('App Name', function () {
    it('test ....', function () {
    })'
})
Run Code Online (Sandbox Code Playgroud)

什么时候进行新的describe()?还是一个新的()?

jasmine jestjs

45
推荐指数
2
解决办法
2万
查看次数

用Jest测试process.env

我有一个应用程序依赖于环境变量,如:

const APP_PORT = process.env.APP_PORT || 8080;
Run Code Online (Sandbox Code Playgroud)

我想测试一下例如:

  • APP_PORT可以由节点env变量设置.
  • 或者express应用程序正在设置的端口上运行process.env.APP_PORT

我怎样才能通过Jest实现这一目标?我可以process.env在每次测试之前设置这些变量,还是应该以某种方式模拟它?

testing environment-variables node.js jestjs

45
推荐指数
11
解决办法
4万
查看次数

无法初始化监视插件“node_modules/jest-watch-typeahead/filename.js”:

我在项目中使用 React with TypeScript,并且正在进行快照测试,但是当我在 vscode 终端中运行 npm test 命令时,出现错误,我已将其附加在附件中。您能否让我知道我应该做什么才能使所有测试用例成功运行或我需要配置的任何其他内容

在此输入图像描述

这是我的 package.json

{
  "name": "vcc-collaboration-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fluentui/react": "^8.42.1",
    "@fluentui/react-file-type-icons": "^8.5.6",
    "@testing-library/jest-dom": "^5.15.0",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "@types/react-test-renderer": "^17.0.1",
    "ag-grid-community": "^26.2.0",
    "ag-grid-react": "^26.2.0",
    "axios": "^0.24.0",
    "babel-jest": "^26.6.0",
    "jest": "^26.6.0",
    "jest-watch-typeahead": "^1.0.0",
    "moment": "^2.29.1",
    "node-sass": "^6.0.1",
    "react": "^17.0.2",
    "react-bootstrap": "^2.0.2",
    "react-dom": "^17.0.2",
    "react-dropzone": "^11.4.2",
    "react-grid": "^4.0.4",
    "react-icons": "^4.3.1",
    "react-router-dom": "^5.0.0",
    "react-scripts": "4.0.3",
    "react-test-renderer": "^17.0.2",
    "vcc-ui": "^2.11.0",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": …
Run Code Online (Sandbox Code Playgroud)

types unit-testing reactjs jestjs

45
推荐指数
3
解决办法
3万
查看次数

观察并重新运行Jest JS测试

玩笑文档建议使用npm test来执行测试.

有没有办法观察您的源和测试,以便在相关文件更改后自动重新运行Jest测试?

npm jestjs

44
推荐指数
3
解决办法
3万
查看次数

Jest:在单元测试中禁用控制台的更好方法

我不知道是否有更好的方法来禁用错误控制台 里面一个特定的玩笑测试(即,恢复原来的控制台前/每次测试后).

这是我目前的做法:

describe("Some description", () => {
  let consoleSpy;

  beforeEach(() => {
    if (typeof consoleSpy === "function") {
      consoleSpy.mockRestore();
    }
  });

  test("Some test that should not output errors to jest console", () => {
    expect.assertions(2);

    consoleSpy = jest.spyOn(console, "error").mockImplementation();

    // some function that uses console error
    expect(someFunction).toBe("X");
    expect(consoleSpy).toHaveBeenCalled();
  });

  test("Test that has console available", () => {
    // shows up during jest watch test, just as intended
    console.error("test");
  });
});
Run Code Online (Sandbox Code Playgroud)

是否有更简洁的方法来完成同样的事情?我想避免spyOn …

javascript unit-testing mocking jestjs

44
推荐指数
8
解决办法
2万
查看次数

Jest中resetAllMocks、resetModules、resetModuleRegistry、restoreAllMocks的区别

我试图在 Jest 中围绕以下内容:

resetAllMocks, resetModules,resetModuleRegistryrestoreAllMocks

我发现这很难。

我阅读了 jest 文档,但不太清楚。如果有人可以向我提供上述工作方式的示例,并且它们彼此不同,我将不胜感激。

javascript unit-testing mocking jestjs

43
推荐指数
2
解决办法
2万
查看次数