feat(test runner): allow comparing binary files to strings (#9315)

Co-authored-by: Max Schmitt <max@schmitt.mx>
This commit is contained in:
Joel Einbinder 2021-10-11 15:57:59 -04:00 committed by GitHub
parent 76142a33ae
commit d34b53a0d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View file

@ -37,15 +37,17 @@ const extensionToMimeType: { [key: string]: string } = {
};
const GoldenComparators: { [key: string]: any } = {
'application/octet-string': compareBuffers,
'application/octet-string': compareBuffersOrStrings,
'image/png': compareImages,
'image/jpeg': compareImages,
'text/plain': compareText,
};
function compareBuffers(actualBuffer: Buffer | string, expectedBuffer: Buffer, mimeType: string): { diff?: object; errorMessage?: string; } | null {
function compareBuffersOrStrings(actualBuffer: Buffer | string, expectedBuffer: Buffer, mimeType: string): { diff?: object; errorMessage?: string; } | null {
if (typeof actualBuffer === 'string')
return compareText(actualBuffer, expectedBuffer);
if (!actualBuffer || !(actualBuffer instanceof Buffer))
return { errorMessage: 'Actual result should be Buffer.' };
return { errorMessage: 'Actual result should be a Buffer or a string.' };
if (Buffer.compare(actualBuffer, expectedBuffer))
return { errorMessage: 'Buffers differ' };
return null;
@ -53,7 +55,7 @@ function compareBuffers(actualBuffer: Buffer | string, expectedBuffer: Buffer, m
function compareImages(actualBuffer: Buffer | string, expectedBuffer: Buffer, mimeType: string, options = {}): { diff?: object; errorMessage?: string; } | null {
if (!actualBuffer || !(actualBuffer instanceof Buffer))
return { errorMessage: 'Actual result should be Buffer.' };
return { errorMessage: 'Actual result should be a Buffer.' };
const actual = mimeType === 'image/png' ? PNG.sync.read(actualBuffer) : jpeg.decode(actualBuffer);
const expected = mimeType === 'image/png' ? PNG.sync.read(expectedBuffer) : jpeg.decode(expectedBuffer);
@ -69,7 +71,7 @@ function compareImages(actualBuffer: Buffer | string, expectedBuffer: Buffer, mi
function compareText(actual: Buffer | string, expectedBuffer: Buffer): { diff?: object; errorMessage?: string; diffExtension?: string; } | null {
if (typeof actual !== 'string')
return { errorMessage: 'Actual result should be string' };
return { errorMessage: 'Actual result should be a string' };
const expected = expectedBuffer.toString('utf-8');
if (expected === actual)
return null;

View file

@ -654,3 +654,17 @@ test('should fail with missing expectations and retries', async ({ runInlineTest
const data = fs.readFileSync(snapshotOutputPath);
expect(data.toString()).toBe('Hello world');
});
test('should allow comparing text with text without file extension', async ({ runInlineTest }) => {
const result = await runInlineTest({
...files,
'a.spec.js-snapshots/snapshot-no-extension': `Hello world`,
'a.spec.js': `
const { test } = require('./helper');
test('is a test', ({}) => {
expect('Hello world').toMatchSnapshot('snapshot-no-extension');
});
`
});
expect(result.exitCode).toBe(0);
});