-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add integration testing with server-side rendering #4123
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
76ab9be
chore: add integration testing with server-side rendering
xiaoiver 5410567
chore: add integration test cases with JSDOM
xiaoiver c87e36b
chore: lint no-undef in integration test case
xiaoiver 3626ed0
chore: add more integration testcases for interval
xiaoiver fc6673d
chore: use cjs instead of esm in integration testing
xiaoiver eba7321
test: update snapshot test process
pearmini d33a19a
test: remove generate file
pearmini 3f689e5
chore(ci): update node version from 12 to 14
pearmini 4391db8
docs: add integration test guide
pearmini 38e5057
feat(test): throw error in svg test
pearmini File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as fs from 'fs'; | ||
import { PNG } from 'pngjs'; | ||
import { createCanvas } from 'canvas'; | ||
import pixelmatch from 'pixelmatch'; | ||
import { Canvas } from '@antv/g'; | ||
import { Renderer } from '@antv/g-canvas'; | ||
import { render, G2Spec } from '../../src'; | ||
|
||
export async function renderCanvas( | ||
options: G2Spec, | ||
filename: string, | ||
defaultWidth = 640, | ||
defaultHeight = 480, | ||
) { | ||
const { width = defaultWidth, height = defaultHeight } = options; | ||
const [canvas, nodeCanvas] = createGCanvas(width, height); | ||
await new Promise<void>((resolve) => { | ||
render(options, { canvas }, resolve); | ||
}); | ||
// Wait for the next tick. | ||
await sleep(20); | ||
await writePNG(nodeCanvas, filename); | ||
return canvas; | ||
} | ||
|
||
/** | ||
* diff between PNGs | ||
*/ | ||
export function diff(src: string, target: string) { | ||
const img1 = PNG.sync.read(fs.readFileSync(src)); | ||
const img2 = PNG.sync.read(fs.readFileSync(target)); | ||
const { width, height } = img1; | ||
return pixelmatch(img1.data, img2.data, null, width, height, { | ||
threshold: 0.1, | ||
}); | ||
} | ||
|
||
function createGCanvas(width: number, height: number) { | ||
// Create a node-canvas instead of HTMLCanvasElement | ||
const nodeCanvas = createCanvas(width, height); | ||
// A standalone offscreen canvas for text metrics | ||
const offscreenNodeCanvas = createCanvas(1, 1); | ||
|
||
// Create a renderer, unregister plugin relative to DOM. | ||
const renderer = new Renderer(); | ||
const domInteractionPlugin = renderer.getPlugin('dom-interaction'); | ||
renderer.unregisterPlugin(domInteractionPlugin); | ||
|
||
return [ | ||
new Canvas({ | ||
width, | ||
height, | ||
canvas: nodeCanvas as any, | ||
renderer, | ||
offscreenCanvas: offscreenNodeCanvas as any, | ||
}), | ||
nodeCanvas, | ||
] as const; | ||
} | ||
|
||
function sleep(n: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, n); | ||
}); | ||
} | ||
|
||
function writePNG(nodeCanvas, path) { | ||
return new Promise<void>((resolve, reject) => { | ||
const out = fs.createWriteStream(path); | ||
const stream = nodeCanvas.createPNGStream(); | ||
stream.pipe(out); | ||
out.on('finish', resolve).on('error', reject); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { salesBasicInterval } from './sales-basic-interval'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { data } from '../data/sales'; | ||
|
||
export function salesBasicInterval() { | ||
return { | ||
type: 'interval', | ||
data: data, | ||
encode: { | ||
x: 'genre', | ||
y: 'sold', | ||
color: 'genre', | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const data = [ | ||
{ genre: 'Sports', sold: 275 }, | ||
{ genre: 'Strategy', sold: 115 }, | ||
{ genre: 'Action', sold: 120 }, | ||
{ genre: 'Shooter', sold: 350 }, | ||
{ genre: 'Other', sold: 150 }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as fs from 'fs'; | ||
import * as tests from './charts'; | ||
import { renderCanvas, diff } from './canvas'; | ||
import { renderSVG } from './svg'; | ||
|
||
describe('integration', () => { | ||
for (const [name, generateOptions] of Object.entries(tests)) { | ||
it(`[Canvas]: ${name}`, async () => { | ||
let canvas; | ||
try { | ||
const actualPath = `${__dirname}/snapshots/${name}-diff.png`; | ||
const expectedPath = `${__dirname}/snapshots/${name}.png`; | ||
const options = await generateOptions(); | ||
|
||
// Generate golden png if not exists. | ||
if (!fs.existsSync(expectedPath)) { | ||
console.warn(`! generate ${name}`); | ||
canvas = await renderCanvas(options, expectedPath); | ||
} else { | ||
canvas = await renderCanvas(options, actualPath); | ||
expect(diff(actualPath, expectedPath)).toBe(0); | ||
|
||
// Persevere the diff image if do not pass the test. | ||
fs.unlinkSync(actualPath); | ||
} | ||
} finally { | ||
canvas.destroy(); | ||
} | ||
}); | ||
} | ||
|
||
for (const [name, generateOptions] of Object.entries(tests)) { | ||
it(`[SVG]: ${name}`, async () => { | ||
let canvas; | ||
let actual; | ||
try { | ||
const expectedPath = `${__dirname}/snapshots/${name}.svg`; | ||
const options = await generateOptions(); | ||
[canvas, actual] = await renderSVG(options); | ||
|
||
// Generate golden svg if not exists. | ||
if (!fs.existsSync(expectedPath)) { | ||
console.warn(`! generate ${name}`); | ||
fs.writeFileSync(expectedPath, actual); | ||
} else { | ||
const expected = fs.readFileSync(expectedPath, { | ||
encoding: 'utf8', | ||
flag: 'r', | ||
}); | ||
expect(expected).toBe(actual); | ||
} | ||
} catch (error) { | ||
// Generate error svg to compare. | ||
console.warn(`! generate ${name}`); | ||
const diffPath = `${__dirname}/snapshots/${name}-diff.svg`; | ||
fs.writeFileSync(diffPath, actual); | ||
throw error; | ||
} finally { | ||
canvas.destroy(); | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import util from 'util'; | ||
|
||
// @see https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom | ||
// @see https://github.com/jsdom/jsdom/issues/2524 | ||
if (global.window) { | ||
// eslint-disable-next-line no-undef | ||
Object.defineProperty(global.window, 'TextEncoder', { | ||
writable: true, | ||
value: util.TextEncoder, | ||
}); | ||
// eslint-disable-next-line no-undef | ||
Object.defineProperty(global.window, 'TextDecoder', { | ||
writable: true, | ||
value: util.TextDecoder, | ||
}); | ||
} | ||
|
||
export { JSDOM } from 'jsdom'; |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import xmlserializer from 'xmlserializer'; | ||
import { Canvas } from '@antv/g'; | ||
import { Renderer } from '@antv/g-svg'; | ||
import { createCanvas } from 'canvas'; | ||
import { G2Spec, render } from '../../src'; | ||
import { JSDOM } from './jsdom'; | ||
|
||
export async function renderSVG( | ||
options: G2Spec, | ||
defaultWidth = 640, | ||
defaultHeight = 480, | ||
) { | ||
const { width = defaultWidth, height = defaultHeight } = options; | ||
const [canvas, dom] = createGCanvas(width, height); | ||
await new Promise<void>((resolve) => { | ||
render(options, { canvas }, resolve); | ||
}); | ||
|
||
// Wait for the next tick. | ||
await sleep(20); | ||
|
||
const svg = xmlserializer.serializeToString( | ||
//@ts-ignore | ||
dom.window.document.getElementById('container').children[0], | ||
); | ||
return [canvas, svg] as const; | ||
} | ||
|
||
function createGCanvas(width: number, height: number) { | ||
const dom = new JSDOM(` | ||
<div id="container"> | ||
</div> | ||
`); | ||
// @ts-ignore | ||
global.window = dom.window; | ||
// @ts-ignore | ||
global.document = dom.window.document; | ||
|
||
// A standalone offscreen canvas for text metrics | ||
const offscreenNodeCanvas = createCanvas(1, 1); | ||
|
||
// Create a renderer, unregister plugin relative to DOM. | ||
const renderer = new Renderer(); | ||
const domInteractionPlugin = renderer.getPlugin('dom-interaction'); | ||
renderer.unregisterPlugin(domInteractionPlugin); | ||
|
||
return [ | ||
new Canvas({ | ||
container: 'container', | ||
width, | ||
height, | ||
renderer, | ||
// @ts-ignore | ||
document: dom.window.document, | ||
offscreenCanvas: offscreenNodeCanvas as any, | ||
requestAnimationFrame: dom.window.requestAnimationFrame, | ||
cancelAnimationFrame: dom.window.cancelAnimationFrame, | ||
}), | ||
dom, | ||
] as const; | ||
} | ||
|
||
export function sleep(n: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, n); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Installing third-party modules by tnpm or cnpm will name modules with underscore as prefix. | ||
// In this case _{module} is also necessary. | ||
const esm = ['internmap', 'd3-*', 'lodash-es'] | ||
.map((d) => `_${d}|${d}`) | ||
.join('|'); | ||
|
||
module.exports = { | ||
testTimeout: 30000, | ||
preset: 'ts-jest/presets/js-with-ts', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: { | ||
target: 'es6', | ||
allowJs: true, | ||
sourceMap: true, | ||
}, | ||
}, | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'], | ||
collectCoverage: false, | ||
testRegex: '(/__tests__/integration/.*\\.(test|spec))\\.(ts|tsx|js)$', | ||
// Transform esm to cjs. | ||
transformIgnorePatterns: [`<rootDir>/node_modules/(?!(${esm}))`], | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests utils 中有 delay 方法