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

时间:2019-05-27 02:33:21

标签: angular angular7 e2e-testing cypress

我正在尝试使用赛普拉斯和cypress-plugin-snapshots对页面上的e2e测试进行快照测试。

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

这是我的配置:

plugins / 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;
}

support / index.js

import 'cypress-plugin-snapshots/src/commands';

cypress.json

{
    "ignoreTestFiles": [
        "**/__snapshots__/*",
        "**/__image_snapshots__/*"
    ]
}

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()
    });
})

2 个答案:

答案 0 :(得分:1)

只是有同样的问题。您的代码应为:

expect(cy.document()).toMatchSnapshot()

请注意expect

答案 1 :(得分:0)

您导入错误。应该是:

import 'cypress-plugin-snapshots/commands';

代替

import 'cypress-plugin-snapshots/src/commands';