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

Hoa*_*Son 5 e2e-testing angular cypress angular7

我正在尝试使用 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 - Snapshot test', () => {
        cy.wait(['@getUser']);
        cy.document().toMatchSnapshot()
    });
})

Run Code Online (Sandbox Code Playgroud)

ioa*_*nb7 2

刚刚有同样的问题。你的代码应该是:

expect(cy.document()).toMatchSnapshot()
Run Code Online (Sandbox Code Playgroud)

请注意expect