cypress-image-stretch-detector
v1.0.3
Published
Cypress command to assert images are not visually stretched (threshold fixed at 0).
Maintainers
Readme
cypress-image-stretch-detector
Detect visually stretched <img> tags in your Cypress tests — fast, simple, and zero-config by default.
What it does? Adds a Cypress command
cy.checkImageNotStretched()that verifies an image’s displayed aspect ratio matches its natural aspect ratio.
Why build this package?
Responsive sites resize images via CSS. If a container or rule is slightly off, images get squashed (horizontally or vertically). It looks unprofessional and often slips into production. Visual diff tools can miss this when content changes. This plugin gives you a repeatable, code-level assertion for stretching so you can gate it in CI.
Package Key Features
- 1-line assertion:
cy.get('img.selector').checkImageNotStretched() - Respects
object-fitby default (cover/contain/scale-downare not flagged) - Actionable logging: shows natural vs displayed sizes, ARs, and Δ%
- Works inside Cypress AUT iframe (uses
ownerDocument.defaultView) - TypeScript types with Cypress
Chainableaugmentation - Great for single-image smoke checks and layout/template spot checks
How it works (mental model)
Waits for image load (avoids
naturalWidth = 0).Reads natural size (
naturalWidth/naturalHeight) →naturalAR.Reads displayed size (
getBoundingClientRect()) →displayedAR.Computes relative aspect delta:
If
object-fitexists and is not'fill', the check passes (cropping, not stretching).
Otherwise, any non-zero Δ is considered stretched. Default threshold = 0.
Why threshold 0?
The default is intentionally strict to catch subtle squashing. (You can make it looser later.)
- remove the below part
How can you integratte with your E2E cypress test/Tiny example (from stretch-test.cy.ts)
/// <reference types="cypress" />
describe('Image stretch detector (single image, threshold = 0)', () => {
it('checks one car slider image on renty.ae', () => {
// 1) go to the site you want
cy.visit('URL');
// 2) select exactly one <img> and assert
cy.get('Image className').first().checkImageNotStretched(); // strict, respects object-fit
});
});Install & Integration
1) Install the package
If published on npm (recommended):
npm i -D cypress-image-stretch-detector
# or: pnpm add -D cypress-image-stretch-detectorLocal development (before publish): choose ONE
A) npm link (fastest while developing)
# in the package folder
npm run build
npm link
# in your Cypress app
npm link cypress-image-stretch-detectorNote:
npm linkcan sometimes cause duplicate deps. If Cypress acts weird, use option B.
B) file: dependency (simple, reliable)
In your Cypress app’s package.json:
{
"devDependencies": {
"cypress-image-stretch-detector": "file:../cypress-image-stretch-detector"
}
}Then install:
npm i2) Register the command in Cypress support
Add ONE of the following to your Cypress support file.
Auto-register (just import)cypress/support/e2e.{js,ts} or cypress/support/commands.{js,ts}:
import 'cypress-image-stretch-detector';Explicit register (named import)
import { registerImageStretchCommand } from 'cypress-image-stretch-detector';
registerImageStretchCommand();The plugin augments
Cypress.Chainabletypes automatically (via the package’s bundledindex.d.ts), socy.checkImageNotStretched()is fully typed.
