Skip to content

Commit

Permalink
💥Remove implicit controller (#83)
Browse files Browse the repository at this point in the history
* 💥Remove implicit controllers & use `ClientFunction` over eval

* 📝 Update README

* ♻️Use `eval` (with passing the `t` controller)

* ♻️Update missing test controller argument error message
  • Loading branch information
Robdel12 committed Mar 12, 2021
1 parent 900eb4e commit 1aff3c7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 52 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
```

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -9,24 +8,23 @@ 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');

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 */
let { domSnapshot, url } = await t.eval(() => ({
/* 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({
Expand Down
42 changes: 14 additions & 28 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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'],
Expand All @@ -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: '<html><head></head><body>Snapshot Me</body></html>',
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([
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 1aff3c7

Please sign in to comment.