小编Hoa*_*Son的帖子

如何使用Jest + Vuejs模拟window.location.href?

目前,我正在为我的项目实施单元测试,并且其中包含一个文件window.location.href

我想对此进行测试,这是我的示例代码:

it("method A should work correctly", () => {
      const url = "http://dummy.com";
      Object.defineProperty(window.location, "href", {
        value: url,
        writable: true
      });
      const data = {
        id: "123",
        name: null
      };
      window.location.href = url;
      wrapper.vm.methodA(data);
      expect(window.location.href).toEqual(url);
    });
Run Code Online (Sandbox Code Playgroud)

但是我得到这个错误:

TypeError: Cannot redefine property: href
        at Function.defineProperty (<anonymous>)
Run Code Online (Sandbox Code Playgroud)

我尝试了一些解决方案,但没有解决。我需要一些提示来帮助我摆脱困境。请帮助。

unit-testing jasmine vue.js vuejs2 vue-test-utils

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

如何使用 Jest 监视 Vuejs 应用程序上的 window.location.assign?

我需要对 window.location.assign 进行单元测试。但是当我运行测试时,我收到此错误。

Cannot spy the assign property because it is not a function; undefined given instead

这是我的代码:

jest.spyOn(window.location, "assign");
Run Code Online (Sandbox Code Playgroud)

有人能给我一些关于这个案例的提示或解决方案吗?

unit-testing jestjs vuejs2

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

如何对提供/注入的 VUE 组件进行单元测试?

我正在编写一些单元测试,但我在使用具有inject属性的组件时遇到了麻烦。

我正在使用shallowMount. 我对此做了一些研究。并且有办法为provide https://vue-test-utils.vuejs.org/api/options.html#provide创建假数据。但是,我没有看到任何有关inject.

所以,我需要一些关于如何inject在 Vuejs 中进行单元测试的建议?

javascript jestjs vuejs2 vue-test-utils

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

fixture.debugElement.componentInstance 和 fixture.nativeElement 有什么区别?

在这个示例测试文件中,我看到了两种不同的语法

一个是const app = fixture.debugElement.componentInstance;,另一个是const compiled = fixture.nativeElement;我不知道这两种语法有什么不同?

我对角度测试完全陌生,我正在将它应用到我的项目中,但对此我有点困惑。

describe('AppComponent (initial CLI version)', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
  it(`should have as title 'app'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app');
  }));
  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges(); …
Run Code Online (Sandbox Code Playgroud)

testing unit-testing angular-cli angular

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

如何使用 Angular Material 构建常春藤?

我正在将 Ivy 与 Angular Material 一起使用,但是当我运行时,我遇到ng build了以下问题:

ERROR in Tried to overwrite D:/Subin/PROJECTS/angular-ivy-sample/node_modules/@angular/material/table/typings/table.d.ts.__ivy_ngcc_bak with an ngcc back up file, which is disallowed.
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

有什么办法解决它吗?

我正在使用Angular 8.0.0Angular 材料 8.0.1

我找到了一些有用的链接,但似乎没有解决方案:

  1. https://github.com/angular/angular/issues/29703
  2. https://github.com/Teradata/covalent/issues/1400

frontend angular angular8

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

找不到转换选项中的模块&lt;rootDir&gt; / node_modules / vue-jest

我遇到了错误,需要您的帮助。感谢您收看这个问题。

我的情况:我正在为我的项目配置Drone CI工具,当我在drone.yml上运行单元测试时得到了这个。

Validation Error:

  Module <rootDir>/node_modules/vue-jest in the transform option was not found.

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html
Run Code Online (Sandbox Code Playgroud)

这是我的 jest.conf.js

transform: {
    "^.+\\.js$": "babel-jest",
    ".*\\.vue$": "<rootDir>/node_modules/vue-jest"
  },
Run Code Online (Sandbox Code Playgroud)

我尝试过的

  1. 删除<rootDir>/node_modules/。但是我又犯了一个错误Module vue-jest in the transform option was not found.。所以我认为这不是正确的解决方案

unit-testing vue.js jestjs babel-jest vuejs2

6
推荐指数
2
解决办法
829
查看次数

如何修复无法在“HTMLInputElement”上设置“value”属性以进行反应?

我正在研究 Nextjs 并尝试上传单个文件。每次附加新文件时,都会在控制台日志中收到此错误。

在控制台,我看到这个错误:

Warning: A component is changing an uncontrolled input of type file to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.
Run Code Online (Sandbox Code Playgroud)

然后,第二个是:

react-dom.development.js:2592 Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
Run Code Online (Sandbox Code Playgroud)

这是我的示例代码:

  1. 渲染函数处的 HTML 路径
Warning: A component …
Run Code Online (Sandbox Code Playgroud)

reactjs next.js

6
推荐指数
1
解决办法
6232
查看次数

如何修复错误:未实现:导航(哈希更改除外)

我正在为包含的文件实施单元测试,window.location.href需要对其进行检查。

我开玩笑的版本是22.0.4。在节点版本> = 10上运行测试时,一切都很好

但是我在运行它时收到此错误 v8.9.3

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Not implemented: navigation (except hash changes)
Run Code Online (Sandbox Code Playgroud)

我对此一无所知。我在许多页面上进行了搜索,以找出解决方案或有关此问题的任何提示,以了解此处发生的情况。

[更新]-我对源代码进行了深入研究,我认为此错误来自jsdom。

at module.exports (webapp/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
at navigateFetch (webapp/node_modules/jsdom/lib/jsdom/living/window/navigation.js:74:3)
Run Code Online (Sandbox Code Playgroud)

navigation.js文件

exports.evaluateJavaScriptURL = (window, urlRecord) => {
  const urlString = whatwgURL.serializeURL(urlRecord);
  const scriptSource = whatwgURL.percentDecode(Buffer.from(urlString)).toString();
  if (window._runScripts === "dangerously") {
    try {
      return window.eval(scriptSource);
    } catch (e) {
      reportException(window, e, urlString);
    }
  }
  return undefined;
};
exports.navigate = (window, newURL, flags) => {
  // This is NOT a spec-compliant implementation of navigation in …
Run Code Online (Sandbox Code Playgroud)

unit-testing jestjs vuejs2

5
推荐指数
5
解决办法
3837
查看次数

如何修复 TypeError: cy.document(...).toMatchSnapshot 不是函数?

我正在尝试使用 Cypress 和cypress-plugin-snapshots在我的页面上进行 e2e 测试的快照测试。

但我得到了这个错误TypeError: cy.document(...).toMatchSnapshot is not a function

这是我的配置:

插件/index.js

const { initPlugin } = require('cypress-plugin-snapshots/plugin');

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  initPlugin(on, config);
  return config;
}
Run Code Online (Sandbox Code Playgroud)

支持/index.js

import 'cypress-plugin-snapshots/src/commands';
Run Code Online (Sandbox Code Playgroud)

赛普拉斯.json

{
    "ignoreTestFiles": [
        "**/__snapshots__/*",
        "**/__image_snapshots__/*"
    ]
}
Run Code Online (Sandbox Code Playgroud)

home.spec.js

context('Querying', () => {
    beforeEach(() => {
        cy.server();
        cy.fixture('user.json').as('userJSON');
        cy.route('GET', 'users?id=*', '@userJSON').as('getUser');
        cy.visit('http://localhost:4200/1');
    })

    it.only('LANDING - …
Run Code Online (Sandbox Code Playgroud)

e2e-testing angular cypress angular7

5
推荐指数
1
解决办法
3065
查看次数

我无法正确使用 Expo 实例中的 Audio.Sound。我无法暂停或更新播放状态

当我尝试暂停或更新由 创建的声音对象的状态时Audio.Sound(),出现错误指示声音未加载。

Error : Cannot complete operation because sound is not loaded.

我不明白,因为我从加载文件并播放它的同一对象调用了该方法。

这是我的代码:

    import React, { useState, useEffect } from "react";
    import { Feather } from "@expo/vector-icons";
    import {
      StyleSheet,
      Text,
      View,
      Button,
      FlatList,
      TouchableOpacity,
      Image
    } from "react-native";
    import { Audio } from "expo-av";

    const imageLocation = "./../assets/bibliotheque";
    const soundLocation = "./../sounds/bibliotheque";

    export default BibliothequeScreen = ({ navigation }) => {
      const [playing, setPlaying] = useState(false);
      const [soundPlaying, setSoundPlaying] = useState(false);

      const soundObject = new Audio.Sound(); …
Run Code Online (Sandbox Code Playgroud)

audio reactjs react-native expo

5
推荐指数
1
解决办法
5843
查看次数

如何从html字符串获取值?

我有一个这样的html字符串,它来自API:

const response = `
<html>
  <head>
    <title></title>
  </head>
  <body>
     <code>Value I want to get<br></code>
  </body>
</html>
`;
Run Code Online (Sandbox Code Playgroud)

因此,内容可能是动态的,但标签<code>是唯一的。我希望对此有更多解决方案,以了解最佳解决方案。

html javascript

2
推荐指数
1
解决办法
53
查看次数

如何构建一个大型 Angular 库

我正在创建一个非常大的 Angular 库。因为我有很多类和组件,所以我将它们结构化为层次结构。例如,我有:

  • 地图
    • 面板
      • 工具栏
      • 主面板
      • ETC。
    • 层数
      • 必应层
      • WMS层
      • WMTS层
    • 几何形状
      • 观点
      • 线
      • 多边形等

在 中public_api.ts,我有许多导出,每个component/module/service我想在库上公开的导出都有一个,但这会扁平化我的层次结构。

我发现维护层次结构的唯一方法是为层次结构树上的每个组创建一个库。例如,面板库(Panels-lib)、图层库(Layers-lib)等,然后复制文件系统上的层次结构,将每个库放在单独的文件夹中:面板文件夹、图层文件夹ETC。

这样,在主应用程序中我可以通过这种方式导入组件:

import {ToolBar} from "MyMap/Panels/Panels-lib";
import {Line} from "MyMap/Geometries/Geometries-lib";
Run Code Online (Sandbox Code Playgroud)

有更好的方法来实现这一目标吗?先感谢您。

angular-cli angular ng-packagr

1
推荐指数
1
解决办法
4282
查看次数