Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test-runner): handle trailing newline differences in text snapshot comparisons (#43277)

Alok Pandey784dcea45bd6
diff --git a/packages/utils/comparators.ts b/packages/utils/comparators.ts
index 8fa72296dd..67aac944ce 100644
--- a/packages/utils/comparators.ts
+++ b/packages/utils/comparators.ts
@@ -126,13 +126,13 @@ function compareText(actual: Buffer | string, expectedBuffer: Buffer): Comparato
if (typeof actual !== 'string')
return { errorMessage: 'Actual result should be a string' };
let expected = expectedBuffer.toString('utf-8');
- if (expected === actual)
- return null;
// Eliminate '\\ No newline at end of file'
if (!actual.endsWith('\n'))
actual += '\n';
if (!expected.endsWith('\n'))
expected += '\n';
+ if (expected === actual)
+ return null;
const lines = diff.createPatch('file', expected, actual, undefined, undefined, { context: 5 }).split('\n');
const coloredLines = lines.slice(4).map(line => {
diff --git a/tests/playwright-test/golden.spec.ts b/tests/playwright-test/golden.spec.ts
index 9ce9f0a9ea..20da8174ea 100644
--- a/tests/playwright-test/golden.spec.ts
+++ b/tests/playwright-test/golden.spec.ts
@@ -46,6 +46,22 @@ test('should support golden', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0);
});
+test('should match text snapshot with trailing newline difference', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ ...files,
+ 'a.spec.js-snapshots/snapshot1.txt': 'Hello world\n',
+ 'a.spec.js-snapshots/snapshot2.txt': 'Hello world',
+ 'a.spec.js': `
+ const { test, expect } = require('./helper');
+ test('is a test', ({}) => {
+ expect('Hello world').toMatchSnapshot('snapshot1.txt');
+ expect('Hello world\\n').toMatchSnapshot('snapshot2.txt');
+ });
+ `
+ });
+ expect(result.exitCode).toBe(0);
+});
+
test('should work with non-txt extensions', async ({ runInlineTest }) => {
const result = await runInlineTest({
...files,