From: Eduardo San Martin Morote Date: Mon, 6 May 2019 10:09:16 +0000 (+0200) Subject: test: better mocha snapshot matching X-Git-Tag: v4.0.0-alpha.0~395 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91f376acfb657cc3b7efac49b08111e3d83209c5;p=thirdparty%2Fvuejs%2Frouter.git test: better mocha snapshot matching --- diff --git a/__tests__/helper.js b/__tests__/helper.js index 9f46366f..97c3ac1b 100644 --- a/__tests__/helper.js +++ b/__tests__/helper.js @@ -6,15 +6,30 @@ if (typeof global !== 'undefined' && typeof global.beforeAll === 'undefined') { const mocks = require('jest-mock') global.jest = mocks + const INITIAL_WS_RE = /^\s+/ + const expect = require('expect') // monkey patch jest snapshots expect.extend({ toMatchInlineSnapshot(received, snapshot) { const text = `[${received.toString()}]` - const pass = text === snapshot + + const match = INITIAL_WS_RE.exec(snapshot) + let expected = snapshot + if (match) { + // remove the initial linefeed + const pad = match[0].replace(/^\n/, '') + expected = snapshot + .split('\n') + .map(chunk => chunk.slice(pad.length)) + .join('\n') + .trim() + } + const pass = text === expected return { pass, - message: () => 'Snapshot not maching: ' + text, + message: () => + `Snapshot not maching.\nExpected:\n${expected}\nReceived:\n${text}`, } }, }) diff --git a/__tests__/matcher.spec.js b/__tests__/matcher.spec.js index 4a21698e..f9209839 100644 --- a/__tests__/matcher.spec.js +++ b/__tests__/matcher.spec.js @@ -262,10 +262,6 @@ describe('Router Matcher', () => { return resolved } - // FIXME: refactor the tests into the function, probably use a common set of routes - // tests named routes and relatives routes - // move to different folder - it('resolves a redirect string', () => { const records = [ { path: '/home', component },