UNPKG

squint-cli

Version:

Squint makes visual reviews of web app releases easy

195 lines (194 loc) 10.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEBUG_PUPPETEER = void 0; const path_1 = __importDefault(require("path")); const glob_1 = __importDefault(require("glob")); const sharp_1 = __importDefault(require("sharp")); const util_1 = require("util"); const utils_1 = require("./utils"); const utils_2 = require("../src/utils"); const lodash_1 = __importDefault(require("lodash")); // This is a separate flag exports.DEBUG_PUPPETEER = process.env.DEBUG_PUPPETEER === 'true'; const IS_WINDOWS = process.platform === 'win32'; const globAsync = util_1.promisify(glob_1.default); const squint = `ts-node src/index.ts --after-page "page => page.setDefaultNavigationTimeout(60000)"${exports.DEBUG_PUPPETEER ? ' --puppeteer-launch-options "{ headless: false }"' : ''}`; const baseUrl1 = `http://localhost:${utils_1.PORT_1}`; const baseUrl2 = `http://localhost:${utils_1.PORT_2}`; // Many cases run a long time jest.setTimeout(5 * 60 * 1000); describe('squint', () => { describe('case1', () => { let server; beforeAll(async () => { server = await utils_1.startFileServer(utils_1.getResourcePath('case1/'), utils_1.PORT_1); }); afterAll(() => { server.close(); }); it('screenshot with default save location', async () => { await utils_1.exec(`${squint} screenshot ${baseUrl1}/old`); const { diff, result } = await utils_1.blinkDiff(utils_1.getResourcePath('case1/img/screenshot/old.png'), '.squint/screenshot.png'); expect(diff.hasPassed(result.code)).toStrictEqual(true); }); it('screenshot with -o location', async () => { const tmpPath = utils_1.getTmpPath('nonexistent/shot.png'); // Test that --out-file works and creates the path if necessary await utils_1.exec(`${squint} screenshot ${baseUrl1}/new -o ${tmpPath}`); const { diff, result } = await utils_1.blinkDiff(utils_1.getResourcePath('case1/img/screenshot/new.png'), tmpPath); expect(diff.hasPassed(result.code)).toStrictEqual(true); }); it('screenshot --out-file', async () => { const tmpFile = utils_1.getRandomTmpPath('nonexistent/shot.png'); await utils_1.exec(`${squint} screenshot ${baseUrl1}/old --out-file ${tmpFile}`); const { diff, result } = await utils_1.blinkDiff(utils_1.getResourcePath('case1/img/screenshot/old.png'), tmpFile); expect(diff.hasPassed(result.code)).toStrictEqual(true); }); it('screenshot --selector', async () => { const tmpFile = utils_1.getRandomTmpPath('nonexistent/shot.png'); await utils_1.exec(`${squint} screenshot ${baseUrl1}/old --selector "h1" --out-file ${tmpFile}`); const { diff, result } = await utils_1.blinkDiff(utils_1.getResourcePath('case1/img/screenshot/old-h1.png'), tmpFile); expect(diff.hasPassed(result.code)).toStrictEqual(true); }); it('screenshot --selector-js', async () => { const tmpFile = utils_1.getRandomTmpPath('nonexistent/shot.png'); await utils_1.exec(IS_WINDOWS ? `${squint} screenshot ${baseUrl1}/old --selector-js "(page) => page.$('h1')" --out-file ${tmpFile}` : `${squint} screenshot ${baseUrl1}/old --selector-js '(page) => page.$("h1")' --out-file ${tmpFile}`); const { diff, result } = await utils_1.blinkDiff(utils_1.getResourcePath('case1/img/screenshot/old-h1-js.png'), tmpFile); expect(diff.hasPassed(result.code)).toStrictEqual(true); }); it('screenshot -w and -h', async () => { await utils_1.exec(`${squint} screenshot ${baseUrl1}/old -w 100 -h 301`); const metadata = await sharp_1.default('.squint/screenshot.png').metadata(); expect(metadata.width).toStrictEqual(100); expect(metadata.height).toStrictEqual(301); }); it('compare default save location', async () => { await utils_1.exec(`${squint} compare ${baseUrl1}/old ${baseUrl1}/new --single-page`); const { diff, result } = await utils_1.blinkDiff(utils_1.getResourcePath('case1/img/compare/diff.png'), '.squint/diff.png'); expect(diff.hasPassed(result.code)).toStrictEqual(true); }); }); describe('case2', () => { let server; let server2; beforeAll(async () => { server = await utils_1.startFileServer(utils_1.getResourcePath('case2/old/'), utils_1.PORT_1); server2 = await utils_1.startFileServer(utils_1.getResourcePath('case2/new/'), utils_1.PORT_2); }); afterAll(() => { server.close(); server2.close(); }); it('compare', async () => { const tmpDir = utils_1.getRandomTmpPath('output/'); await utils_1.exec(`${squint} compare ${baseUrl1} ${baseUrl2} --out-dir ${tmpDir}`); const files = await globAsync(`${tmpDir}/*.png`); await utils_2.promiseEachSeries(files, async (file) => { const basename = path_1.default.basename(file); const resourcePath = utils_1.getResourcePath(`case2/img/compare/${basename}`); const { diff, result } = await utils_1.blinkDiff(resourcePath, file); expect(diff.hasPassed(result.code)).toStrictEqual(true); }); }); it('crawl', async () => { const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl2}`); const outPaths = lodash_1.default.sortBy(stdout.trim().split('\n')); expect(outPaths).toStrictEqual(lodash_1.default.sortBy(['/', '/about-us', '/company', '/blog'])); }); it('crawl --include-hash', async () => { const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl2} --include-hash`); const outPaths = lodash_1.default.sortBy(stdout.trim().split('\n')); expect(outPaths).toStrictEqual(lodash_1.default.sortBy(['/', '/about-us', '/company', '/blog', '/company#testhash'])); }); it('crawl --include-search-query', async () => { const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl2} --include-search-query`); const outPaths = lodash_1.default.sortBy(stdout.trim().split('\n')); expect(outPaths).toStrictEqual(lodash_1.default.sortBy([ '/', '/about-us', '/company', '/blog', '/?query-should-be-ignored=1&test=true', ])); }); }); describe('case3', () => { let server; beforeAll(async () => { server = await utils_1.startFileServer(utils_1.getResourcePath('case3/'), utils_1.PORT_1); }); afterAll(() => { server.close(); }); it('crawl --max-depth 3', async () => { const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl1}/0 --max-depth 3`); // These should be in order const paths = ['/0', '/1', '/2', '/3']; expect(stdout).toStrictEqual(`${paths.join('\n')}\n`); }); it('crawl --should-visit', async () => { // Achieve same as --max-depth 3, using --should-visit const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl1}/0 --should-visit "(urlToVisit, _, visited) => visited.size <= 3"`); // These should be in order const paths = ['/0', '/1', '/2', '/3']; expect(stdout).toStrictEqual(`${paths.join('\n')}\n`); }); }); describe('case4', () => { let server; beforeAll(async () => { server = await utils_1.startFileServer(utils_1.getResourcePath('case4/'), utils_1.PORT_1, { // Set exact control over paths '/': utils_1.getResourcePath('case4/index.html'), '/test': utils_1.getResourcePath('case4/test.html'), '/test/': utils_1.getResourcePath('case4/test.html'), }); }); afterAll(() => { server.close(); }); it('crawl --trailing-slash-mode preserve', async () => { const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl1} --trailing-slash-mode preserve`); const outPaths = lodash_1.default.sortBy(stdout.trim().split('\n')); expect(outPaths).toStrictEqual(lodash_1.default.sortBy(['/', '/test', '/test/'])); }); it('crawl --trailing-slash-mode add', async () => { const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl1}/ --trailing-slash-mode add`); const outPaths = lodash_1.default.sortBy(stdout.trim().split('\n')); expect(outPaths).toStrictEqual(lodash_1.default.sortBy(['/', '/test/'])); }); it('crawl --trailing-slash-mode remove', async () => { const { stdout } = await utils_1.exec(`${squint} crawl ${baseUrl1}/ --trailing-slash-mode remove`); const outPaths = lodash_1.default.sortBy(stdout.trim().split('\n')); expect(outPaths).toStrictEqual(lodash_1.default.sortBy(['/', '/test'])); }); }); describe('incorrect usage', () => { let server; beforeAll(async () => { server = await utils_1.startFileServer(utils_1.getResourcePath('case4/'), utils_1.PORT_1); }); afterAll(() => { server.close(); }); it('--trailing-slash-mode', async () => { await expect(utils_1.exec(`${squint} crawl http://example.com --trailing-slash-mode delete`)).rejects.toThrow(); }); it('--puppeteer-launch-mode', async () => { await expect(utils_1.exec(`${squint} crawl http://example.com --puppeteer-launch-mode launsh`)).rejects.toThrow(); }); it('--puppeteer-launch-options syntax', async () => { await expect(utils_1.exec(IS_WINDOWS ? `${squint} screenshot ${baseUrl1} --puppeteer-launch-options "{\`"baseURL: \`"http://localhost:9000\`" }"` : `${squint} screenshot ${baseUrl1} --puppeteer-launch-options '{"baseURL: "http://localhost:9000" }'`)).rejects.toThrow(); }); it('--non-existing', async () => { await expect(utils_1.exec(`${squint} screenshot ${baseUrl1} --non-existing'`)).rejects.toThrow(); }); }); });