- Testing: Switch to Cypress with code coverage

- npm: Add peerDeps
master
Brett Zamir 2019-11-23 21:02:51 +08:00
parent e1671cc372
commit 3736fddb7f
17 changed files with 4957 additions and 2608 deletions

View File

@ -1,6 +1,7 @@
node_modules
ignore
coverage
dist
docs/jsdoc

View File

@ -1,8 +1,5 @@
module.exports = {
extends: [
"ash-nazg/sauron-node",
"plugin:qunit/recommended", "plugin:testcafe/recommended"
],
extends: ["ash-nazg/sauron-node"],
parserOptions: {
sourceType: "module"
},
@ -127,6 +124,7 @@ module.exports = {
"node/no-missing-import": ["off"],
"no-multi-spaces": "off",
"sonarjs/no-all-duplicated-branches": "off",
'node/no-unpublished-import': ['error', {allowModules: ['@cypress/fiddle']}],
"no-alert": "off",
// Disable until may fix https://github.com/gajus/eslint-plugin-jsdoc/issues/211
"indent": "off"
@ -145,6 +143,7 @@ module.exports = {
},
// We want console in tests!
{
extends: ["plugin:qunit/recommended"],
files: ["test/**"],
rules: {
"no-console": ["off"]
@ -184,6 +183,16 @@ module.exports = {
"import/no-commonjs": "off",
"strict": "off"
}
},
{
files: ["cypress/**"],
extends: ["plugin:cypress/recommended"],
env: {
node: true
},
rules: {
'import/unambiguous': 0,
}
}
],
rules: {

8
.gitignore vendored
View File

@ -6,3 +6,11 @@ build/
svgedit-custom.css
docs/jsdoc
cypress/screenshots
cypress/videos
cypress.env.json
coverage/**
instrumented/**
.nyc_output

View File

@ -5,3 +5,10 @@ test
.github/ISSUE_TEMPLATE/bug_report.md
build
lgtm.yml
cypress/**
cypress.env.json
coverage/**
.nyc_output
instrumented/**

3
cypress.json Normal file
View File

@ -0,0 +1,3 @@
{
"baseUrl": "http://localhost:8000"
}

View File

@ -0,0 +1,30 @@
describe('Accessibility', function () {
beforeEach(() => {
cy.visit('/editor/svg-editor-es.html');
cy.injectAxe();
});
// https://www.npmjs.com/package/cypress-axe
it('Has no detectable a11y violations on load', () => {
// Configure aXe and test the page at initial load
cy.configureAxe({
// Todo: Reenable when have time to fix
// See https://www.deque.com/axe/axe-for-web/documentation/api-documentation/#user-content-parameters-1
rules: [{
id: 'meta-viewport',
enabled: false
}]
/*
branding: {
brand: String,
application: String
},
reporter: 'option',
checks: [Object],
rules: [Object],
locale: Object
*/
});
cy.checkA11y();
});
});

50
cypress/integration/ui.js Normal file
View File

@ -0,0 +1,50 @@
import {
approveStorage, openMainMenu,
openEditorPreferences
} from '../support/ui-test-helper.js';
describe('UI tests', function () {
beforeEach(() => {
cy.visit('/editor/svg-editor.html');
// Ensure we test against English regardless of the original locale
approveStorage();
openEditorPreferences();
cy.get('#lang_select').select('en');
cy.get('#tool_prefs_save').click();
});
it('Editor - No parameters: Has export button', () => {
openMainMenu();
cy.get('#tool_export');
});
it('Editor - No parameters: Export button clicking; dialog opens', () => {
openMainMenu();
cy.get('#tool_export').click();
cy.get('#dialog_content select');
});
it('Editor - No parameters: Drag control point of arc path', () => {
const randomOffset = () => 2 + Math.round(10 + Math.random() * 40);
cy.get('#tool_source').click();
cy.get('#svg_source_textarea')
.type(`<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
<g class="layer">
<title>Layer 1</title>
<path d="m187,194a114,62 0 1 0 219,2" fill="#FF0000" stroke="#000000" stroke-width="5"/>
</g>
</svg>`);
cy.get('#tool_source_save').click();
cy.get('#svg_1').click().click();
cy.get('#pathpointgrip_0').trigger('mousedown', {which: 1})
.trigger('mousemove', randomOffset(), randomOffset())
.trigger('mouseup', {force: true});
cy.get('#pathpointgrip_1').trigger('mousedown', {which: 1})
.trigger('mousemove', randomOffset(), randomOffset())
.trigger('mouseup', {force: true});
cy.get('#svg_1[d]').should('not.contain', 'NaN');
});
});

28
cypress/plugins/index.js Normal file
View File

@ -0,0 +1,28 @@
/* globals module, require */
/* eslint-disable import/no-commonjs */
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************
// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
const codeCoverageTask = require('@cypress/code-coverage/task.js');
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
// https://docs.cypress.io/guides/tooling/code-coverage.html#Install-the-plugin
on('task', codeCoverageTask);
};

View File

@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

44
cypress/support/index.js Normal file
View File

@ -0,0 +1,44 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
// Import commands.js using ES2015 syntax:
import './commands.js';
// Alternatively you can use CommonJS syntax:
// require('./commands')
/**
* FIDDLE
* Adds "cy.runExample()" custom command
* Accets test object property (or array thereof):
* Required: `test`
* Optional: `html`, `name`, `description`
* With `testExamples` only: `skip` and `only`
* @see https://github.com/cypress-io/cypress-fiddle
* @example import {testExamples} from '@cypress/fiddle';
*/
import '@cypress/fiddle';
/**
* COVERAGE
* @see https://docs.cypress.io/guides/tooling/code-coverage.html#Install-the-plugin
*/
import '@cypress/code-coverage/support.js';
/**
* ACCESSIBILITY
* @see https://www.npmjs.com/package/cypress-axe
*/
import 'cypress-axe';

View File

@ -0,0 +1,13 @@
export const approveStorage = () => {
return cy.get('#dialog_buttons > input[type=button][data-ok]')
.click();
};
export const openMainMenu = () => {
return cy.get('#main_icon').click();
};
export const openEditorPreferences = () => {
openMainMenu();
return cy.get('#tool_prefs_option').click();
};

6
jsconfig.json Normal file
View File

@ -0,0 +1,6 @@
{
"include": [
"./node_modules/cypress",
"cypress/**/*.js"
]
}

7180
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -27,9 +27,20 @@
"rollup": "rollup -c",
"start-embedded": "echo \"Open file to http://localhost:8000/editor/embedapi.html\" && static -p 8000 | static -p 8001 -H '{\"Access-Control-Allow-Origin\": \"*\"}'",
"start": "echo \"Open file to http://localhost:8000/test/all_tests.html\" && static -p 8000",
"cypress:open": "cypress open",
"cypress:run": "cypress run",
"open-report": "open http://localhost:3000/coverage/",
"start-open-report": "run-p start open-report",
"instrument": "npx nyc instrument app/public instrumented",
"open-cov": "npm-run-all instrument --parallel --race start cypress:open",
"open": "run-p start cypress:open",
"report": "npx nyc report",
"report-summary": "npx nyc report --reporter=text-summary",
"test-cov": "npm-run-all instrument --parallel --race start cypress:run ; npm run report-summary",
"test-cov-open": "npm-run-all instrument --parallel --race start cypress:run",
"test-no-build": "npm run eslint && npm run build-html && npm run build-by-config && open-cli http://localhost:8000/test/all_tests.html && static -p 8000",
"test-prep": "npm run eslint && npm run build-html && npm run rollup && npm run build-by-config",
"test": "testcafe chrome test/ui-tests/**/*.js --skip-js-errors",
"test": "run-p start cypress:run",
"browser-test": "npm run test-prep && open-cli http://localhost:8000/test/all_tests.html && static -p 8000"
},
"repository": {
@ -82,16 +93,21 @@
"@babel/plugin-transform-modules-commonjs": "^7.7.4",
"@babel/plugin-transform-named-capturing-groups-regex": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@cypress/code-coverage": "^1.10.2",
"@cypress/fiddle": "^1.4.0",
"@mysticatea/eslint-plugin": "^13.0.0",
"axe-core": "^3.4.0",
"axe-testcafe": "^3.0.0",
"babel-plugin-transform-object-rest-spread": "^7.0.0-beta.3",
"coffeescript": "^2.4.1",
"core-js-bundle": "^3.4.2",
"cypress": "^3.6.1",
"cypress-axe": "^0.5.1",
"eslint": "6.7.0",
"eslint-config-ash-nazg": "12.0.0",
"eslint-config-standard": "14.1.0",
"eslint-plugin-array-func": "^3.1.3",
"eslint-plugin-compat": "^3.3.0",
"eslint-plugin-cypress": "^2.7.0",
"eslint-plugin-eslint-comments": "^3.1.2",
"eslint-plugin-html": "^6.0.0",
"eslint-plugin-import": "2.18.2",
@ -103,13 +119,14 @@
"eslint-plugin-qunit": "^4.0.0",
"eslint-plugin-sonarjs": "^0.5.0",
"eslint-plugin-standard": "4.0.1",
"eslint-plugin-testcafe": "^0.2.1",
"eslint-plugin-unicorn": "^13.0.0",
"imageoptim-cli": "^3.0.2",
"jamilih": "^0.46.0",
"jsdoc": "^3.6.3",
"load-stylesheets": "^0.9.0",
"node-static": "^0.7.11",
"npm-run-all": "^4.1.5",
"nyc": "^14.1.1",
"open-cli": "^5.0.0",
"promise-fs": "^2.1.1",
"qr-manipulation": "https://github.com/brettz9/qr-manipulation",
@ -125,6 +142,6 @@
"sinon": "^7.5.0",
"sinon-test": "^2.4.0",
"stackblur-canvas": "^2.2.0",
"testcafe": "^1.7.0"
"typescript": "^3.7.2"
}
}

View File

@ -1,12 +0,0 @@
export const approveStorage = (t) => {
return t
.click('#dialog_buttons > input[type=button][data-ok]');
};
export const openMainMenu = (t) => {
return t.click('#main_icon');
};
export const openEditorPreferences = (t) => {
return openMainMenu(t).click('#tool_prefs_option');
};

View File

@ -1,70 +0,0 @@
// https://github.com/DevExpress/testcafe
// https://devexpress.github.io/testcafe/documentation/test-api/
// https://github.com/helen-dikareva/axe-testcafe
import {axeCheck, createReport} from 'axe-testcafe';
/**
* @external AxeResult
*/
/**
* @external TestcafeTest
*/
/**
* @param {external.TestcafeTest} t
* @returns {Promise<external:AxeResult>}
*/
async function axeCheckWithConfig (t) {
const /* error, */ {violations} = await axeCheck(
t,
// context: https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#context-parameter
undefined,
// https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#options-parameter
{
rules: {
'meta-viewport': {enabled: false}
}
}
// , (err, results) {} // https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#results-object
);
await t.expect(violations.length === 0).ok(createReport(violations));
}
fixture`TestCafe Axe accessibility tests (Editor - no parameters)`
.page`http://localhost:8000/editor/svg-editor.html`;
test('Editor - no parameters', async (t) => {
await axeCheckWithConfig(t); // , axeContent, axeOptions: https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#api-name-axerun
});
fixture`TestCafe Axe accessibility tests (Editor - with all extensions)`
.page`http://localhost:8000/editor/svg-editor.html?extensions=ext-arrows.js,ext-closepath.js,ext-foreignobject.js,ext-helloworld.js,ext-mathjax.js,ext-php_savefile.js,ext-server_moinsave.js,ext-server_opensave.js,ext-webappfind.js,ext-xdomain-messaging.js`;
test('Editor - with all extensions', async (t) => {
await axeCheckWithConfig(t); // , axeContent, axeOptions: https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#api-name-axerun
});
/* eslint-disable qunit/no-commented-tests */
// Waiting for https://github.com/DevExpress/testcafe-hammerhead/issues/1725 (also https://github.com/DevExpress/testcafe/issues/2734 )
/**
fixture`TestCafe Axe accessibility tests (Editor ES - no parameters)`
.page`http://localhost:8000/editor/svg-editor-es.html`;
test('Editor ES - no parameters', async t => {
await axeCheckWithConfig(t); // , axeContent, axeOptions: https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#api-name-axerun
});
fixture`TestCafe Axe accessibility tests (Editor ES - with all extensions)`
.page`http://localhost:8000/editor/svg-editor-es.html?extensions=ext-arrows.js,ext-closepath.js,ext-foreignobject.js,ext-helloworld.js,ext-mathjax.js,ext-php_savefile.js,ext-server_moinsave.js,ext-server_opensave.js,ext-webappfind.js,ext-xdomain-messaging.js`;
test('Editor ES - with all extensions', async t => {
await axeCheckWithConfig(t); // , axeContent, axeOptions: https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#api-name-axerun
});
fixture`TestCafe Axe accessibility tests (Embedded - no parameters)`
.page`http://localhost:8000/editor/embedapi.html`;
test('Embedded - no parameters', async t => {
await axeCheckWithConfig(t); // , axeContent, axeOptions: https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#api-name-axerun
});
*/
/* eslint-enable qunit/no-commented-tests */

View File

@ -1,46 +0,0 @@
// https://github.com/DevExpress/testcafe
// https://devexpress.github.io/testcafe/documentation/test-api/
// https://github.com/helen-dikareva/axe-testcafe
import {Selector} from 'testcafe';
import {
approveStorage, openMainMenu,
openEditorPreferences
} from '../ui-test-helper.js';
fixture`TestCafe UI tests`
.page`http://localhost:8000/editor/svg-editor.html`
.beforeEach((t) => {
// Ensure we test against English regardless of the original locale
return openEditorPreferences(approveStorage(t))
.click('#lang_select').click('#lang_en').click('#tool_prefs_save');
});
test('Editor - No parameters: Export button', async (t) => {
await openMainMenu(t)
.expect(Selector('#tool_export')).ok('Has open button');
});
test('Editor - No parameters: Export button clicking', async (t) => {
await openMainMenu(t)
.click('#tool_export')
.expect(Selector('#dialog_content select')).ok('Export dialog opens');
});
test('Editor - No parameters: Drag control point of arc path', async (t) => {
const randomOffset = () => Math.round(10 + Math.random() * 40);
await t
.click('#tool_source')
.selectTextAreaContent('#svg_source_textarea')
.typeText('#svg_source_textarea', `<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
<g class="layer">
<title>Layer 1</title>
<path d="m187,194a114,62 0 1 0 219,2" fill="#FF0000" stroke="#000000" stroke-width="5"/>
</g>
</svg>`)
.click('#tool_source_save')
.click('#svg_1')
.click('#svg_1')
.drag('#pathpointgrip_0', randomOffset(), randomOffset(), {offsetX: 2, offsetY: 2})
.drag('#pathpointgrip_1', randomOffset(), randomOffset(), {offsetX: 2, offsetY: 2})
.expect(Selector('#svg_1').getAttribute('d')).notContains('NaN');
});