diff --git a/README.md b/README.md index 3cae821..49963f4 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ fixture('MyFixture') test('Test1', async t => { await t.typeText('#developer-name', 'John Doe'); - await percySnapshot('TestCafe Example'); + await percySnapshot(t, 'TestCafe Example'); }); ``` @@ -68,10 +68,9 @@ $ percy exec -- testcafe chrome:headless tests ## Configuration -`percySnapshot(name[, options])` `percySnapshot(t, name[, options])` -- `t` - The test controller can be provided when it cannot be implicitly resolved +- `t`(**required**) - The test controller instance passed from `test` - `name` (**required**) - The snapshot name; must be unique to each snapshot - `options` - Additional snapshot options (overrides any project options) - `options.widths` - An array of widths to take screenshots at @@ -82,21 +81,6 @@ $ percy exec -- testcafe chrome:headless tests ## Upgrading -In previous versions of `@percy/testcafe`, the test controller (`t`) was a required argument for the -`percySnapshot` function. An [implicit test -controller](https://devexpress.github.io/testcafe/documentation/reference/test-api/testcontroller/#implicit-test-controller-use) -can now be used, however an explicit controller can still be provided when it cannot be implicitly -resolved. - -```javascript -// before -await percySnapshot(t, 'Snapshot Name', options); - -// after (both options are valid) -await percySnapshot('Snapshot Name', options); -await percySnapshot(t, 'Snapshot Name', options); -``` - ### Migrating Config If you have a previous Percy configuration file, migrate it to the newest version with the diff --git a/index.js b/index.js index f906009..e52ad4b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ const utils = require('@percy/sdk-utils'); -const { t: implicit } = require('testcafe'); // Collect client and environment information const sdkPkg = require('./package.json'); @@ -9,8 +8,7 @@ const ENV_INFO = `${testCafePkg.name}/${testCafePkg.version}`; // Take a DOM snapshot and post it to the snapshot endpoint module.exports = async function percySnapshot(t, name, options) { - // if name is the first arg, assume an implicit controller - if (!t || typeof t === 'string') [t, name, options] = [implicit, t, name]; + if (!t) throw new Error("The test function's `t` argument is required."); if (!name) throw new Error('The `name` argument is required.'); if (!(await utils.isPercyEnabled())) return; let log = utils.logger('testcafe'); @@ -18,7 +16,7 @@ module.exports = async function percySnapshot(t, name, options) { try { // Inject the DOM serialization script /* eslint-disable-next-line no-new-func */ - await t.eval(new Function(await utils.fetchPercyDOM())); + await t.eval(new Function(await utils.fetchPercyDOM()), { boundTestRun: t }); // Serialize and capture the DOM /* istanbul ignore next: no instrumenting injected code */ @@ -26,7 +24,7 @@ module.exports = async function percySnapshot(t, name, options) { /* eslint-disable-next-line no-undef */ domSnapshot: PercyDOM.serialize(options), url: document.URL - }), { dependencies: { options } }); + }), { boundTestRun: t, dependencies: { options } }); // Post the DOM to the snapshot endpoint with snapshot options and other info await utils.postSnapshot({ diff --git a/tests/index.test.js b/tests/index.test.js index f91a8d7..5fc0cdc 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -10,16 +10,21 @@ fixture('percySnapshot') .afterEach(() => sdk.teardown()) .after(() => sdk.testsite.close()); -test('throws an error when a name is not provided', async () => { +test('throws an error when a test is not provided', async () => { await expect(percySnapshot()) + .rejects.toThrow("The test function's `t` argument is required."); +}); + +test('throws an error when a name is not provided', async t => { + await expect(percySnapshot(t)) .rejects.toThrow('The `name` argument is required.'); }); -test('disables snapshots when the healthcheck fails', async () => { +test('disables snapshots when the healthcheck fails', async t => { sdk.test.failure('/percy/healthcheck'); - await percySnapshot('Snapshot 1'); - await percySnapshot('Snapshot 2'); + await percySnapshot(t, 'Snapshot 1'); + await percySnapshot(t, 'Snapshot 2'); expect(sdk.server.requests).toEqual([ ['/percy/healthcheck'] @@ -31,9 +36,9 @@ test('disables snapshots when the healthcheck fails', async () => { ]); }); -test('posts snapshots to the local percy server', async () => { - await percySnapshot('Snapshot 1'); - await percySnapshot('Snapshot 2'); +test('posts snapshots to the local percy server', async t => { + await percySnapshot(t, 'Snapshot 1'); + await percySnapshot(t, 'Snapshot 2'); expect(sdk.server.requests).toEqual([ ['/percy/healthcheck'], @@ -54,29 +59,10 @@ test('posts snapshots to the local percy server', async () => { expect(sdk.logger.stderr).toEqual([]); }); -test('can work with an explicit test controller', async t => { - await percySnapshot(t, 'Snapshot 1'); - - expect(sdk.server.requests).toEqual([ - ['/percy/healthcheck'], - ['/percy/dom.js'], - ['/percy/snapshot', { - name: 'Snapshot 1', - url: 'http://localhost:8000/', - domSnapshot: 'Snapshot Me', - clientInfo: expect.stringMatching(/@percy\/testcafe\/.+/), - environmentInfo: expect.stringMatching(/testcafe\/.+/) - }] - ]); - - expect(sdk.logger.stdout).toEqual([]); - expect(sdk.logger.stderr).toEqual([]); -}); - -test('handles snapshot errors', async () => { +test('handles snapshot errors', async t => { sdk.test.failure('/percy/snapshot', 'failure'); - await percySnapshot('Snapshot 1'); + await percySnapshot(t, 'Snapshot 1'); expect(sdk.logger.stdout).toEqual([]); expect(sdk.logger.stderr).toEqual([ diff --git a/yarn.lock b/yarn.lock index 9a6ad69..617f8b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -297,7 +297,7 @@ dependencies: dotenv "^8.2.0" -"@percy/logger@^1.0.0-beta.36", "@percy/logger@^1.0.0-beta.39": +"@percy/logger@^1.0.0-beta.39": version "1.0.0-beta.39" resolved "https://registry.yarnpkg.com/@percy/logger/-/logger-1.0.0-beta.39.tgz#dd5c03866d0d494157be191237602d86d43de5ff" integrity sha512-aEnKxCO3r9+bhxogIhJhxjh80QGImiIhbmik/oKCWNIc2KywATGRXsJvRmlF1DskA+RQcwzG/fENerSLNRycgQ==