@histoire/plugin-percy 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Guillaume Chau
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Histoire Screenshot with Percy for visual regression testing
2
+
3
+ You need the [Perci CLI](https://docs.percy.io/docs/cli-overview) installed to be able to send snapshots to Percy.
4
+
5
+
6
+ ```bash
7
+ pnpm add -D @histoire/plugin-percy
8
+ ```
9
+
10
+ Add the plugin in histoire config:
11
+
12
+ ```js
13
+ import { defineConfig } from 'histoire'
14
+ import { HstPercy } from '@histoire/plugin-percy'
15
+
16
+ export default defineConfig({
17
+ plugins: [
18
+ HstPercy({
19
+ // Options here
20
+ }),
21
+ ],
22
+ })
23
+ ```
24
+
25
+ Then use the Percy CLI
26
+
27
+ ```bash
28
+ # Replace `story:build` with the script to build the stories if you changed it
29
+ percy exec pnpm run story:build
30
+ ```
@@ -0,0 +1,21 @@
1
+ import type { Plugin } from 'histoire';
2
+ export interface PercyPluginOptions {
3
+ /**
4
+ * Ignored stories.
5
+ */
6
+ ignored?: (payload: {
7
+ file: string;
8
+ story: {
9
+ title: string;
10
+ };
11
+ variant: {
12
+ id: string;
13
+ title: string;
14
+ };
15
+ }) => boolean;
16
+ /**
17
+ * Percy options.
18
+ */
19
+ percyOptions?: any;
20
+ }
21
+ export declare function HstPercy(options?: PercyPluginOptions): Plugin;
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ import { defu } from 'defu';
2
+ import path from 'pathe';
3
+ import { fileURLToPath } from 'url';
4
+ import { createRequire } from 'module';
5
+ import { isPercyEnabled, fetchPercyDOM, postSnapshot } from '@percy/sdk-utils';
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const require = createRequire(import.meta.url);
8
+ const defaultOptions = {
9
+ percyOptions: {},
10
+ };
11
+ export function HstPercy(options = {}) {
12
+ const finalOptions = defu(options, defaultOptions);
13
+ return {
14
+ name: '@histoire/plugin-percy',
15
+ onBuild: async (api) => {
16
+ if (!await isPercyEnabled()) {
17
+ return;
18
+ }
19
+ const { default: puppeteer } = await import('puppeteer');
20
+ const browser = await puppeteer.launch();
21
+ // Collect client and env info
22
+ const sdkPkg = require(path.join(__dirname, '../package.json'));
23
+ const puppeteerPkg = require('puppeteer/package.json');
24
+ const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`;
25
+ const ENV_INFO = `${puppeteerPkg.name}/${puppeteerPkg.version}`;
26
+ api.onPreviewStory(async ({ file, story, variant, url }) => {
27
+ if (finalOptions.ignored?.({
28
+ file,
29
+ story: {
30
+ title: story.title,
31
+ },
32
+ variant: {
33
+ id: variant.id,
34
+ title: variant.title,
35
+ },
36
+ })) {
37
+ return;
38
+ }
39
+ const page = await browser.newPage();
40
+ await page.goto(url);
41
+ const name = `${story.title} > ${variant.title}`;
42
+ await page.evaluate(await fetchPercyDOM());
43
+ const domSnapshot = await page.evaluate((opts) => {
44
+ // @ts-expect-error window global var
45
+ return window.PercyDOM.serialize(opts);
46
+ }, finalOptions.percyOptions);
47
+ await postSnapshot({
48
+ ...finalOptions.percyOptions,
49
+ environmentInfo: ENV_INFO,
50
+ clientInfo: CLIENT_INFO,
51
+ url: page.url(),
52
+ domSnapshot,
53
+ name,
54
+ });
55
+ });
56
+ api.onBuildEnd(async () => {
57
+ await browser.close();
58
+ });
59
+ },
60
+ };
61
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@histoire/plugin-percy",
3
+ "version": "0.3.0",
4
+ "description": "Histoire plugin to take screenshots with Percy for visual regression testing",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Guillaume Chau"
8
+ },
9
+ "repository": {
10
+ "url": "https://github.com/Akryum/histoire.git",
11
+ "type": "git",
12
+ "directory": "packages/histoire-plugin-percy"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "type": "module",
18
+ "exports": {
19
+ ".": "./dist/index.js",
20
+ "./*": "./*"
21
+ },
22
+ "main": "./dist/index.js",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "dependencies": {
26
+ "@percy/sdk-utils": "^1.1.0",
27
+ "defu": "^6.0.0",
28
+ "fs-extra": "^10.1.0",
29
+ "pathe": "^0.2.0",
30
+ "puppeteer": "^13.7.0"
31
+ },
32
+ "devDependencies": {
33
+ "histoire": "0.3.0",
34
+ "typescript": "^4.6.3"
35
+ },
36
+ "peerDependencies": {
37
+ "histoire": "^0.3.0"
38
+ },
39
+ "scripts": {
40
+ "build": "rimraf dist && tsc -d",
41
+ "watch": "tsc -d -w --sourceMap"
42
+ },
43
+ "readme": "# Histoire Screenshot with Percy for visual regression testing\n\nYou need the [Perci CLI](https://docs.percy.io/docs/cli-overview) installed to be able to send snapshots to Percy.\n\n\n```bash\npnpm add -D @histoire/plugin-percy\n```\n\nAdd the plugin in histoire config:\n\n```js\nimport { defineConfig } from 'histoire'\nimport { HstPercy } from '@histoire/plugin-percy'\n\nexport default defineConfig({\n plugins: [\n HstPercy({\n // Options here\n }),\n ],\n})\n```\n\nThen use the Percy CLI\n\n```bash\n# Replace `story:build` with the script to build the stories if you changed it\npercy exec pnpm run story:build\n```\n"
44
+ }
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ import type { Plugin } from 'histoire'
2
+ import { defu } from 'defu'
3
+ import path from 'pathe'
4
+ import { fileURLToPath } from 'url'
5
+ import { createRequire } from 'module'
6
+ import { isPercyEnabled, fetchPercyDOM, postSnapshot } from '@percy/sdk-utils'
7
+
8
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
9
+ const require = createRequire(import.meta.url)
10
+
11
+ export interface PercyPluginOptions {
12
+ /**
13
+ * Ignored stories.
14
+ */
15
+ ignored?: (payload: { file: string, story: { title: string }, variant: { id: string, title: string } }) => boolean
16
+ /**
17
+ * Percy options.
18
+ */
19
+ percyOptions?: any
20
+ }
21
+
22
+ const defaultOptions: PercyPluginOptions = {
23
+ percyOptions: {},
24
+ }
25
+
26
+ export function HstPercy (options: PercyPluginOptions = {}): Plugin {
27
+ const finalOptions: PercyPluginOptions = defu(options, defaultOptions)
28
+ return {
29
+ name: '@histoire/plugin-percy',
30
+
31
+ onBuild: async api => {
32
+ if (!await isPercyEnabled()) {
33
+ return
34
+ }
35
+
36
+ const { default: puppeteer } = await import('puppeteer')
37
+ const browser = await puppeteer.launch()
38
+
39
+ // Collect client and env info
40
+ const sdkPkg = require(path.join(__dirname, '../package.json'))
41
+ const puppeteerPkg = require('puppeteer/package.json')
42
+ const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`
43
+ const ENV_INFO = `${puppeteerPkg.name}/${puppeteerPkg.version}`
44
+
45
+ api.onPreviewStory(async ({ file, story, variant, url }) => {
46
+ if (finalOptions.ignored?.({
47
+ file,
48
+ story: {
49
+ title: story.title,
50
+ },
51
+ variant: {
52
+ id: variant.id,
53
+ title: variant.title,
54
+ },
55
+ })) {
56
+ return
57
+ }
58
+
59
+ const page = await browser.newPage()
60
+ await page.goto(url)
61
+
62
+ const name = `${story.title} > ${variant.title}`
63
+ await page.evaluate(await fetchPercyDOM())
64
+ const domSnapshot = await page.evaluate((opts) => {
65
+ // @ts-expect-error window global var
66
+ return window.PercyDOM.serialize(opts)
67
+ }, finalOptions.percyOptions)
68
+ await postSnapshot({
69
+ ...finalOptions.percyOptions,
70
+ environmentInfo: ENV_INFO,
71
+ clientInfo: CLIENT_INFO,
72
+ url: page.url(),
73
+ domSnapshot,
74
+ name,
75
+ })
76
+ })
77
+
78
+ api.onBuildEnd(async () => {
79
+ await browser.close()
80
+ })
81
+ },
82
+ }
83
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "outDir": "dist",
7
+ "rootDir": "src",
8
+ "allowSyntheticDefaultImports": true,
9
+ "esModuleInterop": true,
10
+ "removeComments": false,
11
+ "resolveJsonModule": true,
12
+ "skipLibCheck": true,
13
+ "types": [
14
+ "node",
15
+ "@peeky/test"
16
+ ],
17
+ "lib": [
18
+ "ESNext",
19
+ "DOM"
20
+ ],
21
+ "sourceMap": false,
22
+ "preserveWatchOutput": true,
23
+ "preserveSymlinks": true,
24
+ // Strict
25
+ "noImplicitAny": false,
26
+ "noImplicitThis": true,
27
+ "alwaysStrict": true,
28
+ "strictBindCallApply": true,
29
+ "strictFunctionTypes": true,
30
+ // Volar
31
+ "jsx": "preserve",
32
+ },
33
+ "include": [
34
+ "src"
35
+ ],
36
+ "exclude": [
37
+ "node_modules",
38
+ "generated/**/*",
39
+ "dist/**/*",
40
+ "src/**/*.spec.ts"
41
+ ]
42
+ }